Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce-如果特定优惠券代码或产品不存在或未删除,则从购物车中删除项目_Php_Wordpress_Woocommerce_Hook Woocommerce_Coupon - Fatal编程技术网

Php WooCommerce-如果特定优惠券代码或产品不存在或未删除,则从购物车中删除项目

Php WooCommerce-如果特定优惠券代码或产品不存在或未删除,则从购物车中删除项目,php,wordpress,woocommerce,hook-woocommerce,coupon,Php,Wordpress,Woocommerce,Hook Woocommerce,Coupon,我添加了一个功能,在输入某个优惠券代码时添加免费产品 我现在需要检查原始产品是否仍在购物车中,如果没有,请从购物车中删除优惠券和免费产品 add_action('woocommerce_applicated_优惠券','apply_product_on_优惠券'); 功能在优惠券()上应用产品{ 全球商业; $coupon_idcus='ccam0620'; $free_product_id=11259; 如果(在数组中($优惠券\u idcus,$woocommerce->cart->get\

我添加了一个功能,在输入某个优惠券代码时添加免费产品

我现在需要检查原始产品是否仍在购物车中,如果没有,请从购物车中删除优惠券和免费产品

add_action('woocommerce_applicated_优惠券','apply_product_on_优惠券');
功能在优惠券()上应用产品{
全球商业;
$coupon_idcus='ccam0620';
$free_product_id=11259;
如果(在数组中($优惠券\u idcus,$woocommerce->cart->get\u applicated\u优惠券()){
$woocommerce->cart->add_to_cart($free_product_id,2);
}
}

您需要另一个操作来检查优惠券是否已被删除

function action_woocommerce_removed_coupon( $coupon_code ) { 
   if($coupon_code == 'ccam0620') {

     $product_id = 11259;
     $product_cart_id = WC()->cart->generate_cart_id( $product_id );
     $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
     if ( $cart_item_key ) WC()->cart->remove_cart_item( $cart_item_key );
   }
}; 

// add the action 
add_action( 'woocommerce_removed_coupon', 'action_woocommerce_removed_coupon', 10, 1 );
这应该可以-我还没有测试过

资料来源1:


来源2:

很高兴我能帮忙:)