2 回答

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 文件中。

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
? ? ? ? }
? ? }
這是回答您問題的代碼片段。我只添加了一種重量條件。我相信你自己能夠滿足其他條件。
- 2 回答
- 0 關注
- 121 瀏覽
添加回答
舉報