P粉8934570262023-09-01 00:25:02
為購物車頁面新增一列(其值取決於購物車商品)的最簡單方法是覆寫 cart.php
範本。
從 WooCommerce 外掛程式中,複製 <代码>woocommerce/cart/cart.php 到yourTheme/woocommerce/cart/
。如果您沒有使用子主題,我建議您建立一個子主題並透過它覆蓋模板,這樣當您的主題更新時,您的模板變更就不會遺失。有關子主題的更多資訊。
從那裡您可以查看cart.php
,找到要插入折扣百分比標題的位置,並插入資料(在本例中為百分比折扣)。 p>
要取得表頭的標籤,很簡單。只需在表格的 thead
中新增標籤的 HTML 即可。在我的範例中,可以在 cart.php 第 51-59 行
中找到:
<thead> <tr> <th class="product-name" colspan="3"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th> <th class="product-price"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th> <th class="product-discount"><?php esc_html_e( 'Discount', 'woocommerce' ); ?></th> // added this line <th class="product-quantity"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></th> <th class="product-subtotal"><?php esc_html_e( 'Subtotal', 'woocommerce' ); ?></th> </tr> </thead>
要取得並顯示折扣百分比,您必須瀏覽範本並找到它的正確位置。在我的範例中,我將其放在價格和數量之間,直接在折扣標題下方。在cart.php
中,這將是第102行
。從那裡,您只需編寫 HTML 和 PHP 程式碼即可根據購物車商品的正常價格和促銷價計算百分比:
<td class="product-discount"> <?php if($_product->get_sale_price() != ''){ $reg_price = $_product->get_regular_price(); $sale_price = $_product->get_sale_price(); $percentage = ((($sale_price / $reg_price) - 1) * -1) * 100 . "%"; echo $percentage; } ?> </td>
您現在可以看到,在購物車頁面上,它顯示了基於購物車商品的折扣百分比. 在我的範例中,頂部產品正在促銷,底部產品不促銷。