Woocommerce - 在选定类别中每消费 X 金额即可添加免费礼物

问题描述 投票:0回答:1

在我们的 Woocommerce 商店中,我们希望为买家在选定类别中每消费 50 美元赠送一份免费礼物 - 以下是我们在functions.php 中的代码,它可以工作,但还不够好。

我们现在面临的问题是,当买家花费 50.50 美元或 51 美元或 52 美元时,Woocommerce 不会将免费礼品添加到购物车中。相反,Woocommerce 仅在金额稍多(例如 57 美元或更多)或有额外产品数量高于 50 美元目标时添加。

该代码当前的问题是,当达到 50 美元的目标支出时,它不会启动并添加 1 份免费礼物

请帮忙

```
// Free Gift - Auto Add To Cart //

add_action('woocommerce_before_calculate_totals' , 'freegift_q12024', 10, 1);

function freegift_q12024( $cart_object ){
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

$category_slug=array('categoryA', 'categoryB', 'categoryC', 'categoryD', 'categoryE', 'categoryF'); // Category slugs
$product_gifted_id=54657; // Gift Product ID
$gift_on_spent=50; // Every X Spend
$total_gift=0;
$total_gift_exits=0;
$total_sub_gift=0;
$cart_items_total = 0; // Initializing
foreach ($cart_object->get_cart() as $key => $cart_item) {
         if ( has_term( $category_slug, 'product_cat', $cart_item['product_id'] ) ) {
                if($cart_item['product_id']!=$product_gifted_id){
                    $total_sub_gift=$total_sub_gift+$cart_item['line_total'];
                }
                if($cart_item['product_id']=$product_gifted_id){
                    $total_gift_exits=$cart_item['quantity'];
                }
         }
}

if($total_sub_gift>0){
    $total_gift=floor(($total_sub_gift/$gift_on_spent));
}
if($total_gift_exits!=$total_gift){
    $product_gifted_cart_id = WC()->cart->generate_cart_id( $product_gifted_id );
    $product_gifted_in_cart = WC()->cart->find_product_in_cart( $product_gifted_cart_id );
    if ($product_gifted_in_cart ){ WC()->cart->remove_cart_item( $product_gifted_in_cart ); }
    $product_cart_id = WC()->cart->generate_cart_id( $product_gifted_id );
    if( ! WC()->cart->find_product_in_cart( $product_cart_id ) ){
        if ($total_gift>0){ WC()->cart->add_to_cart( $product_gifted_id,$total_gift);}
    }
}
if($total_gift>0){
    foreach ($cart_object->get_cart() as $key => $cart_item) {
        if($cart_item['product_id']==$product_gifted_id){
            $cart_item['data']->set_price(0); 
            $cart_item['data']->set_regular_price(0); 
        }
    }
}
}

// Free Gift - Disable remove from cart //

add_filter('woocommerce_cart_item_remove_link', 'freegift_q12024_noremovefromcart', 20, 2 );
function freegift_q12024_noremovefromcart( $button_link, $cart_item_key ){
//SET HERE your specific products IDs
$targeted_products_ids = array(54657);

// Get the current cart item
$cart_item = WC()->cart->get_cart()[$cart_item_key];

// If the targeted product is in cart we remove the button link
if( in_array($cart_item['data']->get_id(), $targeted_products_ids) )
    $button_link = '';

return $button_link;
}
```

上面的代码有效 - 它会自动添加免费礼物,但问题是它不会自动添加确切的 50 美元支出目标,并且需要更多支出或物品数量才能初始化(这就是问题)

php wordpress woocommerce hook-woocommerce
1个回答
0
投票

您在添加免费礼物的 WooCommerce 代码中遇到的问题似乎与支出总额的计算有关

这是你应该尝试的代码

if (is_admin() && !defined('DOING_AJAX'))
        return;

    $category_slug = array('categoryA', 'categoryB', 'categoryC', 'categoryD', 'categoryE', 'categoryF'); // Category slugs
    $product_gifted_id = 54657; // Gift Product ID
    $gift_on_spent = 50; // Every X Spend
    $total_gift = 0;
    $total_gift_exits = 0;
    $total_sub_gift = 0;

    foreach ($cart_object->get_cart() as $cart_item) {
        if (has_term($category_slug, 'product_cat', $cart_item['product_id'])) {
            if($cart_item['product_id'] != $product_gifted_id){
                $total_sub_gift += $cart_item['line_total'];
            }
            if($cart_item['product_id'] == $product_gifted_id){
                $total_gift_exits = $cart_item['quantity'];
            }
        }
    }

    if($total_sub_gift >= $gift_on_spent){ // Change from > to >= to include exact $50
        $total_gift = floor($total_sub_gift / $gift_on_spent);
    }
© www.soinside.com 2019 - 2024. All rights reserved.