장바구니 및 체크 아웃 페이지에서 중복 된 쿠폰 및 맞춤 추가 필드를 관리하는 데 문제가 있습니다.장바구니/체크 아웃에 대한 조건부 쿠폰 및 추가 비용 관리하기 Woocommerce
변형이있는 장바구니에 제품이 있고 할인이 두 번째 (또는 세 번째) 변형에만 적용되고 첫 번째 변형에는 적용되지 않는 경우에만 할인을 추가하는 함수를 만들었습니다. 내 최종 코드는 여기에서 찾으실 수 있습니다 Woocommerce conditional part cart discount on the same product present with > 1 variation
그러나 나는 더 까다로운 상황이 있기 때문에 계속해야했습니다. 일반적으로 카트에 다른 할인 (예 : 표준 할인 쿠폰)이있는 경우 위에서 설명한 조건을 적용 할 수 없습니다. 따라서 장바구니를받은 직후에이 두 줄을 추가했습니다.
$has_coupons = count(WC()->cart->applied_coupons)>0?true:false;
if(!$has_coupons) {
//here goes the function that I did
} else return;
모든 것이 잘 작동합니다. 그러나 장바구니에 쿠폰을 (%가 아닌) 적용해야만 특정 장바구니에 특정 금액을 줄이는 특별한 상황이 있습니다. 이 특정 쿠폰은 내가 작성한 함수에서 생성 된 할인과 함께 갈 수 있습니다!
/* get coupon code*/
function get_coupon_code() {
global $woocommerce;
if (!empty($woocommerce->cart->applied_coupons)) {
$my_coupon_code = $woocommerce->cart->get_coupons() ;
foreach($my_coupon_code as $coupon){
if ($post = get_post($coupon->id)) {
$coupon_code= $coupon->code;
}
}
}
return $coupon_code;
}
/* get coupon amount*/
function get_coupon_amount() {
global $woocommerce;
if (! empty($woocommerce->cart->applied_coupons)) {
$my_coupon_amount = $woocommerce->cart->get_coupons() ;
foreach($my_coupon_amount as $coupon){
$coupon_amount =$coupon->coupon_amount;
}
}
return $coupon_amount;
}
function cart_discount() {
if (is_admin() && ! defined('DOING_AJAX'))
return;
global $woocommerce;
//get the code and amount of the coupon applied
$coupon_code = get_coupon_code();
$coupon_amount = get_coupon_amount();
//check if the code corresponds to the coupon exception I want to accept
if ($coupon_code == 'wa-1'){
//if it is the code I need to apply, then I remove it from the cart
WC()->cart->remove_coupon($coupon_code);
//I use the amount to add it as additional fee at negative
$coupon_discount = -($coupon_amount);
//I add the ex-coupon amount to an additional field
$discount_agency_text = __('Detracted agency fee', 'woocommerce');
WC()->cart->add_fee($discount_agency_text, $coupon_discount, false);
}
//and following the function previously mentioned
$cart = WC()->cart->get_cart();
$has_coupons = count(WC()->cart->applied_coupons)>0?true:false;
//check if there are other coupons or if the coupon applied are the case I want to pass through my function
if(!$has_coupons || $coupon == 'wa-1') {
//here follows the strings to apply a 10% discount on the second (third) variation of the product and not on the first one
foreach($cart as $cart_item_key => $values) {
$product_id = $values['product_id']; // product ID
$variation_id = $values['variation_id']; // product quantity
$cart_lines_total = $values["line_total"];
$cart_lines_quantity = $values["quantity"];
//product id to be considered for this discount
//$product = 1394 = Spedizioni CSR eng
//$product = 1389 = Spedizioni IDP eng
//$product = 13888 = Spedizioni CSR ita
//$product = 13910 = Spedizioni IDP ita
if($product_id == '1394' || $product_id == '1389' || $product_id == '13888' || $product_id == '13910')
{
$cart_array []= array($product_id , $variation_id, $cart_lines_quantity, $cart_lines_total);
}
}
$conteggio = count($cart_array); //counts the number of items in teh cart
// percent is 5%
$percent = -0.10;
if ($conteggio < 3 && $cart_array[0][0] == $cart_array[1][0])
{
$discount = $percent * $cart_array[1][3];
$discount_text = __('10% discount on subsequent week(s)', 'woocommerce');
WC()->cart->add_fee($discount_text, $discount, false);
}
if ($conteggio < 4 && $cart_array[0][0] == $cart_array[1][0] && $cart_array[0][0] == $cart_array[2][0])
{
$discount = ($percent * $cart_array[1][3]) + ($percent * $cart_array[2][3]);
$discount_text = __('10% discount on subsequent week(s)', 'woocommerce');
WC()->cart->add_fee($discount_text, $discount, false);
}
if ($conteggio < 5 && $cart_array[0][0] == $cart_array[1][0] && $cart_array[0][0] == $cart_array[2][0] && $cart_array[0][0] == $cart_array[3][0])
{
$discount = ($percent * $cart_array[1][3]) + ($percent * $cart_array[2][3]) + ($percent * $cart_array[3][3]);
$discount_text = __('10% discount on subsequent week(s)', 'woocommerce');
WC()->cart->add_fee($discount_text, $discount, false);
}
} else return;
}
add_action('woocommerce_cart_calculate_fees','cart_discount');
내가 가진 문제는 부분적으로는 할인 쿠폰 관리가 작동하지 않는다는 것입니다. $ coupon_code 및 $ coupon_amount를 얻지 만 추가 필드 섹션에 추가하지 않습니다.
아무도 나를 도울 수 있습니까?
링크는 매우 답변에서 환영하지만, 링크가 변경할 수 있기 때문에 대답은 항상 링크에서 필수 정보를 제공해야한다. 고맙습니다! – Alex