實用的 WordPress ICP 備案許可管理器

WooCommerce 教程:顯示總體折扣/儲蓄在購物車頁面

如果您喜歡電子商務,就像銷售轉換率和減少購物車放棄一樣熱衷於電子商務,今天的代碼片段將派上用場。

此外,這正式是 Business Bloomer 的第一個訪客博客 (有想法?請在這裏發送您的建議)… 所以讓我正式介紹您今天的作者:Jamie Gill,英國布拉德福德的 WordPress 和 WooCommerce 愛好者。

WooCommerce PHP 代碼段:顯示總的折扣金額/總儲蓄 @購物車和結帳

WooCommerce 3.0+

 /** * @snippet Display Total Discount / Savings @ WooCommerce Cart/Checkout  * @sourcecode https://businessbloomer.com/?p=20362 * @author Rodolfo Melogli, Bülent Sakarya * @testedwith WooCommerce 3.0 */  function bbloomer_wc_discount_total_30() {      global $woocommerce;      $discount_total = 0;      foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {  	$_product = $values['data'];          if ( $_product->is_on_sale() ) {         $regular_price = $_product->get_regular_price();         $sale_price = $_product->get_sale_price();         $discount = ($regular_price - $sale_price) * $values['quantity'];         $discount_total += $discount;         }      }      if ( $discount_total > 0 ) {     echo '<tr class="cart-discount">     <th>'. __( 'You Saved', 'woocommerce' ) .'</th>     <td data-title=" '. __( 'You Saved', 'woocommerce' ) .' ">'     . wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>     </tr>';     }  }  // Hook our values to the Basket and Checkout pages  add_action( 'woocommerce_cart_totals_after_order_total', 'bbloomer_wc_discount_total_30', 99); add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_wc_discount_total_30', 99);  

WooCommerce 低於 3.0

 /** * @snippet Display Total Discount / Savings @ WooCommerce Cart/Checkout  * @sourcecode https://businessbloomer.com/?p=20362 * @author Jamie Gill, Rodolfo Melogli, Lubo Enev * @testedwith WooCommerce 2.6.14 */  function bbloomer_wc_discount_total() {      global $woocommerce;      $discount_total = 0;      foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {  	$_product = $values['data'];          if ( $_product->is_on_sale() ) {         $discount = ($_product->regular_price - $_product->sale_price) * $values['quantity'];         $discount_total += $discount;         }      }      if ( $discount_total > 0 ) {     echo '<tr class="cart-discount">     <th>'. __( 'You Saved', 'woocommerce' ) .'</th>     <td data-title=" '. __( 'You Saved', 'woocommerce' ) .' ">'     . wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>     </tr>';     }  }  // Hook our values to the Basket and Checkout pages  add_action( 'woocommerce_cart_totals_after_order_total', 'bbloomer_wc_discount_total', 99); add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_wc_discount_total', 99);  

如何添加此代碼?

1 、您可以將 PHP 代碼片段放置在主題或子主題的 functions.php 文件的底部 (如果是 CSS 代碼,請添加到子主題的 style.css 文件底部) 修改之前建議先備份原始文件,若出現錯誤請先刪除此代碼。

2 、 WordPress 4.9 後改進了主題編輯器,對於 CSS 代碼也可打開網站前台編輯器的 【自定義】,複製代碼添加到自定義 css 中。

此代碼是否可用?

如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 用户也希望您的參與。

文章沒看懂?代碼不會用?需要幫助您可以

園長 的頭像