Php 基于购物车重量应用累进折扣
我正在寻找一种基于购物车总重量应用折扣(百分比)的方法。 例如:Php 基于购物车重量应用累进折扣,php,wordpress,woocommerce,discount,Php,Wordpress,Woocommerce,Discount,我正在寻找一种基于购物车总重量应用折扣(百分比)的方法。 例如: 10至25公斤,设5%折扣 26至50公斤,设定7.5%的折扣 51至100公斤,设10%折扣 101至150公斤,设定12.5%的折扣 超过150公斤,可享受15%的折扣 当其中一条规则被应用时,应该会出现一条消息,类似于图中所示的付款折扣。 如果可能,还可以显示一个消息框,如“添加到购物车消息框”,该消息框显示客户必须订购多少才能获得第二条折扣规则中的第一条,如:订购**kg以上,并获得5%的折扣 我不知道如何做到这一点
- 10至25公斤,设5%折扣
- 26至50公斤,设定7.5%的折扣
- 51至100公斤,设10%折扣
- 101至150公斤,设定12.5%的折扣
- 超过150公斤,可享受15%的折扣
Vasco欢迎来到StackOverflow
$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
}
}
$applicated\u折扣=0;
添加过滤器('woocommerce\u calculated\u total','add\u conditional\u discount\u by\u weight',10,2);
函数按重量添加条件折扣($total,$cart){
全球美元折扣;
$total_weight=WC()->cart->get_cart_contents_weight();//获取购物车重量
如果($total_weight>=10 | |$total_weight 0){
$discount=WC()->cart->total*($applicated_折扣/100);
?>
欢迎来到StackOverflow
$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
}
}
$applicated\u折扣=0;
添加过滤器('woocommerce\u calculated\u total','add\u conditional\u discount\u by\u weight',10,2);
函数按重量添加条件折扣($total,$cart){
全球美元折扣;
$total_weight=WC()->cart->get_cart_contents_weight();//获取购物车重量
如果($total_weight>=10 | |$total_weight 0){
$discount=WC()->cart->total*($applicated_折扣/100);
?>
您可以使用负费用根据购物车重量进行累进折扣,如下所示:
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 );
}
}
add_action('woocommerce_cart_计算费用','shipping_weight_折扣',30,1);
功能发货\重量\折扣($cart){
if(定义了('DOING'uajax'))
返回;
$cart\u weight=$cart->get\u cart\u contents\u weight();
$cart\u subtotal=$cart->get\u subtotal();//或$cart->subtotal;
$percentage=0;
如果($cart\u weight>=10&&$cart\u weight 25&&$cart\u weight 50&&$cart\u weight 100&&$cart\u weight 150){
$percentage=15;
}
//根据重量应用计算出的折扣
如果($percentage>0){
$折扣=$购物车小计*$百分比/100;
$cart->add_-fee(sprintf(uuu('Weight%s折扣','woocommerce'),$percentage.%'),-$折扣);
}
}
代码进入活动子主题(或活动主题)的function.php文件
基于:您可以使用负费用根据购物车重量进行累进折扣,如下所示:
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 );
}
}
add_action('woocommerce_cart_计算费用','shipping_weight_折扣',30,1);
功能发货\重量\折扣($cart){
if(定义了('DOING'uajax'))
返回;
$cart\u weight=$cart->get\u cart\u contents\u weight();
$cart\u subtotal=$cart->get\u subtotal();//或$cart->subtotal;
$percentage=0;
如果($cart\u weight>=10&&$cart\u weight 25&&$cart\u weight 50&&$cart\u weight 100&&$cart\u weight 150){
$percentage=15;
}
//根据重量应用计算出的折扣
如果($percentage>0){
$折扣=$购物车小计*$百分比/100;
$cart->add_-fee(sprintf(uuu('Weight%s折扣','woocommerce'),$percentage.%'),-$折扣);
}
}
代码进入活动子主题(或活动主题)的function.php文件
基于:首先计算购物车中所有产品的重量。然后使用IF声明应用折扣等。StackOverFlow的规则是一次问一个问题……因此,如果不想结束此问题,请避免一次问多个问题。相反,您可以一个接一个地问多个问题。对不起,我只是这里是新的,所以我还需要学习。我必须为第二个问题创建一个新的线程,还是我可以在这里写下一个新的评论?首先你要计算购物车中所有产品的重量。然后用IF语句应用折扣等。StackOverFlow的规则是一次一个问题……所以避免一次问多个问题如果你不想结束这个问题,可以问一次。相反,你可以一个接一个地问多个问题。对不起,我只是新来的,所以我还需要学习。我必须为第二个问题创建一个新的线程,还是我可以在这里写一个新的评论?谢谢。这个选项有多安全?我的意思是,有人很容易操纵折扣吗?谢谢。这个选择有多安全?我是说,有人操纵折扣容易吗?