我在购物车和结帐页面上管理当前不同的优惠券和自定义附加字段时遇到了问题。
我创建了一个功能,只有当购物车中存在超过变体的商品并且折扣仅适用于第二个(或第三个)变体而不是第一个变体时才会添加折扣。您可以在此处找到我的最终代码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,但不会将其添加到其他字段部分。
有人可以帮我吗?
答案 0 :(得分:0)
我仍然被困在军队上,但我现在试着让它更简单。
我实际上可以简单地使用与我的功能一起使用的优惠券折扣(如果我有一个带有&gt;然后是变体的产品,它只增加10%折扣而不是第一个变化而不是第一个)。
问题在于,如果我在购物车上使用优惠券(= 80€),它无论如何都会分散在有害物品上,10%的折扣是以较低的价格(不是正常价格)计算的。
如果我可以在最终购物车总数上应用优惠券,而不是分散在单个商品上,我的问题就会解决。这可能吗?
举一个例子,在申请优惠券之前我的$ cart_array是
Array
(
[0] => Array
(
[0] => 1394 //product_id
[1] => 1851 //variation_id
[2] => 1 //quantity
[3] => 906 //regular price
)
[1] => Array
(
[0] => 1394 //product_id
[1] => 1852 //variation_id
[2] => 1 //quantity
[3] => 906 //regular price
)
)
如果此时我应用优惠券,则数组会改变:
Array
(
[0] => Array
(
[0] => 1394
[1] => 1851
[2] => 1
[3] => 866 //price after coupon
)
[1] => Array
(
[0] => 1394
[1] => 1852
[2] => 1
[3] => 866 //price after coupon
)
)
如你所见,第二项不再是906而是866,因此10%的折扣更少。 有没有办法将优惠券仅应用于阵列的第一项?或者在最终总数中应用它(在计算了额外费用之后)?
答案 1 :(得分:0)
很好地展示了!
此插件可以轻松解决问题,它允许您根据您在woocommerce商店中配置的多个条件规则的组合,在购物车中收取额外费用。
我希望它有所帮助!