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

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

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

WooCommerce 3.0+

  /**  * @snippet Display Total Discount / Savings @ WooCommerce Cart/Checkout  * @how-to Watch tutorial @ https://businessbloomer.com/?p=19055  * @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  * @how-to Watch tutorial @ https://businessbloomer.com/?p=19055  * @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);    


可以在哪裡新增此程式碼?

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


這段程式碼是否正常可用?

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


需要關於 WooCommerce 的幫助?

請觀看我們提供的免費影片教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。