我在管理購物車和結賬頁面上的同時存在的優惠券和自定義附加字段方面存在問題。在購物車/結帳時管理有條件的優惠券和額外費用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