我对Woocommerce优惠券有疑问。
如果我创建的优惠券名为“ test15”,且折扣类型=百分比,并且使用限制选项产品ID为(1,2),则仅适用于ID为1,2的选定商品。
我想将此优惠券应用于购物车中的所有产品,但我需要将这两个特定产品(id 1,id 2)添加到购物车中。
那么如何为购物车中的所有产品添加百分比优惠券,但是同时特定商品(id 1,id 2)必须在购物车中?
我正在尝试这样的事情:
function woo_in_cart($arr_product_id) {
global $woocommerce;
$cartarray=array();
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['product_id'];
array_push($cartarray,$_product);
}
if (!empty($cartarray)) {
$result = array_intersect($cartarray,$arr_product_id);
}
if (!empty($result)) {
return true;
} else {
return false;
};
}
add_filter('woocommerce_get_shop_coupon_data', 'changeCoupon', 10, 2);
function changeCoupon($unknown_param, $curr_coupon_code) {
if($curr_coupon_code == 'test15') {
$discount_type = 'percent';
$amount = 15;
if(!$discount_type || !$amount) return false;
$coupon = array(
'id' => 9999999999 . rand(2,9),
'amount' => $amount,
'individual_use' => false,
'product_ids' => array(),
'exclude_product_ids' => array(),
'usage_limit' => '',
'usage_limit_per_user' => '',
'limit_usage_to_x_items' => '',
'usage_count' => '',
'expiry_date' => '',
'apply_before_tax' => 'yes',
'free_shipping' => false,
'product_categories' => array(),
'exclude_product_categories' => array(),
'exclude_sale_items' => false,
'minimum_amount' => '',
'maximum_amount' => '',
'customer_email' => '',
'discount_type' => $discount_type,
);
$my_products_ids_array = array(1,2);
if (woo_in_cart($my_products_ids_array)) {
return $coupon;
}else {
return;
}
}