我正在尝试解决以下问题。 我一直在使用Woocommerce动态定价,以便对特定产品类别应用25%的折扣。 然后,如果运送选项为local_pickup,则需要取消该折扣。 我以为我可以调整以下代码。它某处缺少东西,所以我要向您寻求帮助。
function discount_table(){
// Set $cat_in_cart to false
$cat_in_cart = false;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'category_1', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}
$total = WC()->cart->subtotal;
$sconto_label = "";
if($total >= 300){
$sconto_label=15;
}elseif($total >= 220){
$sconto_label=10;
}elseif($total >= 150){
$sconto_label=8;
}elseif($total >= 100){
$sconto_label=4;
}
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = explode(':',$chosen_methods[0]);
if($chosen_shipping[0]=='local_pickup' && !$cat_in_cart){
$sconto_label=25;
}
else if ($chosen_shipping[0]=='local_pickup' && $cat_in_cart){
$sconto_label=25;
// there should be something here or not?
}
$sconto_cliente = (($total*$sconto_label)/100);
if($sconto_label!="")
$sconto_cliente_net = ($sconto_loison/1.1);
WC()->cart->add_fee( "Discount ($sconto_label%)", -$sconto_cliente_net, false );
}
add_action( 'woocommerce_cart_calculate_fees','discount_table' );
是否可以恢复购物车中已经打折的特定类别的一个或多个项目的原始价格?
答案 0 :(得分:1)
要根据产品类别和特定的选择的运输方式恢复购物车价格,请尝试以下(用于Woocommerce动态定价)
:add_filter('woocommerce_add_cart_item_data', 'add_default_price_as_cart_item_custom_data', 50, 3 );
function add_default_price_as_cart_item_custom_data( $cart_item_data, $product_id, $variation_id ){
// HERE define your product category(ies)
$categories = array('t-shirts');
if ( has_term( $categories, 'product_cat', $product_id ) ) {
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// The WC_Product Object
$product = wc_get_product($product_id);
// Get product default base price
$price = (float) $product->get_price();
// Set the Product default base price as custom cart item data
$cart_item_data['default_price'] = $price;
}
return $cart_item_data;
}
add_action( 'woocommerce_before_calculate_totals', 'restore_cart_item_price', 900, 1 );
function restore_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE set the targeted Chosen Shipping method
$targeted_shipping_method = 'local_pickup';
// Get the chosen shipping method
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_method_id = explode(':', reset($chosen_methods) );
$chosen_shipping_method = reset($chosen_shipping_method_id);
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if( $targeted_shipping_method == $chosen_shipping_method && isset($cart_item['default_price']) ){
// Set back the default cart item price
$cart_item['data']->set_price($cart_item['default_price']);
}
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。
注意:在使用负费用 (购物车折扣)时,始终会征税。
答案 1 :(得分:0)
我研究了一下您的代码并进行了一些更改。感谢超级开发者找到了错误!这是代码:
<body onload="rvalue()">
<div id="container">
<span name="values"></span>
<span name="values"></span>
<span name="values"></span>
</div>
</body>