亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據 Woocommerce 購物車重量應用漸進折扣

根據 Woocommerce 購物車重量應用漸進折扣

PHP
蠱毒傳說 2023-09-22 15:08:27
我正在尋找一種根據購物車總重量應用折扣(百分比)的方法。例子:10至25公斤,設置5%折扣26 至 50 公斤,設置 7.5% 折扣51至100公斤,設置10%折扣101 至 150 公斤,設置 12.5% 折扣超過150公斤,設置15%折扣當應用其中一項規則時,應該會出現一條消息,如圖所示的付款折扣。 付款方式折扣如果還可以顯示一個消息框,例如“添加到購物車消息框”,該消息框顯示客戶必須訂購多少才能獲得第二個折扣規則中的第一個,例如:訂購**公斤以上并獲得 5% 的折扣。我不知道如何實現這一目標,所以不幸的是我沒有嘗試任何東西。提前致謝。
查看完整描述

2 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

您可以使用負費用根據購物車重量進行漸進折扣,如下所示:


add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_discount', 30, 1 );

function shipping_weight_discount( $cart ) {

? ? if ( is_admin() && ! defined( 'DOING_AJAX' ) )

? ? ? ? return;


? ? $cart_weight? ?= $cart->get_cart_contents_weight();

? ? $cart_subtotal = $cart->get_subtotal(); // Or $cart->subtotal;

? ? $percentage? ? = 0;


? ? if ( $cart_weight >= 10 && $cart_weight <= 25 ) {

? ? ? ? $percentage = 5;

? ? } elseif ( $cart_weight > 25 && $cart_weight <= 50 ) {

? ? ? ? $percentage = 7.5;

? ? } elseif ( $cart_weight > 50 && $cart_weight <= 100 ) {

? ? ? ? $percentage = 10;

? ? } elseif ( $cart_weight > 100 && $cart_weight <= 150 ) {

? ? ? ? $percentage = 12.5;

? ? } elseif ( $cart_weight > 150 ) {

? ? ? ? $percentage = 15;

? ? }


? ? // Apply a calculated discount based on weight

? ? if( $percentage > 0 ) {

? ? ? ? $discount = $cart_subtotal * $percentage / 100;

? ? ? ? $cart->add_fee( sprintf( __( 'Weight %s discount', 'woocommerce' ), $percentage.'%'), -$discount );

? ? }

}

代碼位于活動子主題(或活動主題)的 function.php 文件中。


查看完整回答
反對 回復 2023-09-22
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

$applied_discount = 0;


? ? add_filter( 'woocommerce_calculated_total', 'add_conditional_discount_by_weight', 10, 2 );

? ? function add_conditional_discount_by_weight( $total, $cart ) {

? ? ? ??

? ? ? ? global $applied_discount;

? ? ? ??

? ? ? ? $total_weight = WC()->cart->get_cart_contents_weight(); // gets cart weight

? ? ? ??

? ? ? ? if($total_weight >= 10 || $total_weight <= 25){

? ? ? ? ? ??

? ? ? ? ? ? $applied_discount = 5;

? ? ? ? ? ? return $total*(1-($applied_discount/100));

? ? ? ? ? ??

? ? ? ? }

? ? }

? ??

? ? add_action( 'woocommerce_cart_totals_after_order_total', 'display_applied_discount' );

? ? function display_applied_discount() {

? ? ? ??

? ? ? ? global $applied_discount;

? ? ? ??

? ? ? ? if($applied_discount > 0){

? ? ? ? ? ? $discount = WC()->cart->total * ($applied_discount/100);

? ? ? ? ?>

? ? ? ? <tr class="order-total">

? ? ? ? ? ? <th><?php esc_html_e( "Applied Discount ($applied_discount%)", 'woocommerce' ); ?></th>

? ? ? ? ? ? <td><?php echo "$".$discount ?></td>

? ? ? ? </tr>

? ? ? ? <?php

? ? ? ? }

? ? }

這是回答您問題的代碼片段。我只添加了一種重量條件。我相信你自己能夠滿足其他條件。


查看完整回答
反對 回復 2023-09-22
  • 2 回答
  • 0 關注
  • 121 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號