[英]How to change the variable product price in cart also delete remove particular product form cart in Woocommerce
I my site i added one variable product in cart.At that time another variable product also added into that cart that product is gift product.Now i want to change the gift variable product price into 0 its working only at the time of condition meets the products that offers gifts in cart. 我的网站我在购物车中添加了一个变量产品。当时另一个变量产品也添加到该购物车中,该产品是礼品。现在我想将礼品变量产品价格改为0,仅在条件满足时工作在购物车中提供礼品的产品。 Also i want to remove both product form same by clicking products that offers gifts.Below my code is not working for me. 此外,我想通过点击提供礼物的产品删除两种产品形式。我的代码不适合我。
add_action( 'woocommerce_before_calculate_totals', 'change_custom_price' );
function change_custom_price( $cart_object ) {
$custom_price = 0; // This will be your custome price
$gift_variation_id = 2046;
foreach ( $cart_object->cart_contents as $value ) {
if ( $value['variation_id'] == $gift_variation_id ) {
$value['data']->price = $custom_price;
}
}
}
Gift item can be added as per this solution - Buy one get one in woocommerce with out coupon code . 可以根据此解决方案添加礼品 - 购买一件带有优惠券代码的woocommerce 。
You can add the following to your theme's 'functions.php' to remove the gift item added automatically by another product. 您可以将以下内容添加到主题的'functions.php'中,以删除其他产品自动添加的礼品。
function remove_gift_product($cart_item_key) {
global $woocommerce;
$cat_in_cart = false;
$coupon_in_cart = false;
$autocoupon = array( 123411 ); // variation ids of products that offers gifts
$freecoupon = array( 2046 ); // variation ids of products that are gift coupons
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
if( in_array( $values['variation_id'], $autocoupon ) ) {
$cat_in_cart = true;
}
}
if ( !$cat_in_cart ) {
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if ( in_array( $cart_item['variation_id'], $freecoupon )) {
$woocommerce->cart->remove_cart_item($cart_item_key);
}
}
}
}
add_action( 'woocommerce_cart_item_removed', 'remove_gift_product' );
Add this if you want reduced price for your gift item. 如果您想降低礼品价格,请添加此商品。
function add_discount_price( $cart_object ) {
global $woocommerce;
$cat_in_cart = false;
$autocoupon = array( 123411 ); // variation ids of products that offers gifts
$freecoupon = array( 2046 ); // variation ids of products that are gift coupons
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
if( in_array( $values['variation_id'], $autocoupon ) ) {
$cat_in_cart = true;
}
}
if ( $cat_in_cart ) {
$custom_price = 0; // This will be your custome price
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if ( in_array( $cart_item['variation_id'], $freecoupon )) {
$cart_item['data']->set_price($custom_price);
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_discount_price' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.