제품 ID 및 수량 조건에 따라 WooCommerce 저장소에 쿠폰을 자동으로 적용하려고합니다. 최종 목표는 원하는 제품이 카트에 2 개 추가되면 특정 쿠폰이 자동으로 적용되고, 원하는 제품이 3 개 추가 될 때마다 자동으로 다른 쿠폰이 적용됩니다. 제품의 단일 수량에는 할인이 없어야합니다. 지금 작동 코드의 수정 된 버전입니다 :특정 제품 ID 및 수량에 대해 조건부로 쿠폰을 자동 적용합니다.
add_action('woocommerce_before_cart', 'conditional_auto_add_coupons');
function conditional_auto_add_coupons() {
if (!WC()->cart->is_empty()){
// Define HERE your Targeted Product ID and coupons codes
$target_pid = 103;
$coupon1 = 'soccer-sibling-2';
$coupon2 = 'soccer-sibling-3';
// First cart loop: Counting number of subactegory items in cart
foreach (WC()->cart->get_cart() as $cart_item){
if($target_pid == $cart_item['data']->id){
// Removes any coupons in the cart already
WC()->cart->remove_coupons();
if(2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount($coupon1)){
WC()->cart->remove_coupons();
WC()->cart->add_discount($coupon1);
wc_add_notice(__('The multiple sibling discount has been applied.', 'theme_domain'), 'success');
} elseif(3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount($coupon2)){
WC()->cart->remove_coupons();
WC()->cart->add_discount($coupon2);
wc_add_notice(__('The multiple sibling discount has been applied.', 'theme_domain'), 'success');
}
// Recalculates Cart Totals to show correct price
WC()->cart->calculate_totals();
}
}
}
}
어떤 오류 메시지가 표시됩니까? –
글쎄, 내 붙여 넣은 코드에 구문 오류가 있음을 발견했습니다. 그것을 바로 잡았습니다. 내가 디버깅 할 때 얻는 에러는 "Notice : 333 행의 /home/...functions.php에있는 non-object의 속성을 얻으려고하는 것"입니다. 해당 라인은 조건부 'if ($ _product-> id == $ product_id)'입니다. – GDailey