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 关注
- 93 浏览
添加回答
举报