WooCommerce - 自动添加或自动从购物车中删除免费赠品

时间:2022-10-25 09:57:05

I'm operating a WooCommerce shop, where we'd like to give every customer a freebie (e-book) that will show in the basket, after you have added a product to the basket obvious.

我正在经营一家WooCommerce商店,我们希望为每位客户提供免费赠品(电子书),这些商品将在购物篮中显示,并且明显地将产品添加到购物篮中。

Example:
You add "product1" to the basket, and the basket will now show 2 products. the "product1" and the "freebie". When you remove the product from the basket, the freebie will be removed again.

示例:您将“product1”添加到购物篮中,购物篮现在将显示2个产品。 “product1”和“freebie”。当您从篮子中取出产品时,将再次移除免费赠品。

I got this code for now:

我现在得到这个代码:

add_action( 'woocommerce_add_to_cart', 'check_freebie_exists', 10, 6 );
function check_freebie_exists($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    if($product_id == 1234) { // obviously replace this with the product that triggers the freebie
        /* or you could use
        if(has_term($cat_id, 'product_cat', $product_id)) { to check if a product is in a particular product_cat
        or you could check anything else related to the product
        */
        $hasfreebie = false;
        // loop through the cart to check the freebie is not already there
        global $woocommerce;
        $cart = $woocommerce->cart->get_cart();
        foreach($cart as $key => $values) {
            if($values['data']->id == $your_freebie_product_id) {
                $hasfreebie = true;
                break;
            }
        }
        if(!$hasfreebie) {
            $woocommerce->cart->add_to_cart($your_freebie_product_id);
        }
    }
}

add_action( 'woocommerce_cart_item_removed', 'remove_freebie', 10, 2 );
function remove_freebie( $cart_item_key, $cart ) {
    $hasmaster = false;
    $freebiekey = NULL;
    foreach($cart as $key => $values) {
        if($values['data']->id == 1234) { // check that we have the product that should trigger the freebie
            $hasmaster = true;
        } elseif($values['data']->id == $your_freebie_product_id) {
            $freebiekey = $key;
        }
    }
    if(!$hasmaster && $freebiekey) {
        $cart->remove_cart_item($freebiekey);
    }
}

But it doesn't seem to be working quite yet.

但它似乎还没有起作用。

What I am doing wrong?

我做错了什么?

Any help will be really appreciated.

任何帮助将非常感激。

Thanks.

谢谢。

1 个解决方案

#1


3  

— Updated — (tested and working for all products)

- 更新 - (已测试并适用于所有产品)

I have updated your code, to make it work. This code is tested and fully functional:

我已更新您的代码,以使其工作。此代码经过测试且功能齐全:

add_action( 'woocommerce_add_to_cart', 'check_add_freebie', 10, 6 );

function check_add_freebie($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    $item_special = $product_id; 
    $freebie_item_id = 99; // the freebie product

    // just for our special product
    if($product_id == $item_special) {
        $cart_items = WC()->cart->get_cart();
        // Iterating through each item in cart
        foreach ($cart_items as $cart_item ){
            // If freebie product is in cart
            if( $cart_item['data']->id == $freebie_item_id ) {
                $has_freebie = true;
                break;
            }
            // If freebie product is NOT in cart
            else
                $has_freebie = false;
        }
        // If freebie product is NOT in cart, we add it.
        if(!$has_freebie)
            WC()->cart->add_to_cart($freebie_item_id);
    }
}

add_action( 'woocommerce_cart_item_removed', 'remove_freebie', 10, 2 );

function remove_freebie( $cart_item_key, $product_id) {
    $item_special = $product_id;
    $freebie_item_id = 99; // the freebie product
    $hasmaster = false;
    $freebiekey = NULL;
    // Iterating through each item in cart
    foreach( WC()->cart->get_cart() as $item_key => $item ) {
        if ( $item['data']->id == $item_special )
            $hasmaster = true; // Special product is in cart
        elseif ( $item['data']->id == $freebie_item_id ) 
            $freebiekey = $item_key; // Freebie product is in cart
    }
    // AS freebie product is in cart, it's removed too
    if( !$hasmaster && !empty($freebiekey) )
        WC()->cart->remove_cart_item($freebiekey);
}

the code goes in function.php file of your active child theme (or theme) or also in any plugin file.

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

This code is tested and works.

此代码经过测试并可以使用。

#1


3  

— Updated — (tested and working for all products)

- 更新 - (已测试并适用于所有产品)

I have updated your code, to make it work. This code is tested and fully functional:

我已更新您的代码,以使其工作。此代码经过测试且功能齐全:

add_action( 'woocommerce_add_to_cart', 'check_add_freebie', 10, 6 );

function check_add_freebie($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    $item_special = $product_id; 
    $freebie_item_id = 99; // the freebie product

    // just for our special product
    if($product_id == $item_special) {
        $cart_items = WC()->cart->get_cart();
        // Iterating through each item in cart
        foreach ($cart_items as $cart_item ){
            // If freebie product is in cart
            if( $cart_item['data']->id == $freebie_item_id ) {
                $has_freebie = true;
                break;
            }
            // If freebie product is NOT in cart
            else
                $has_freebie = false;
        }
        // If freebie product is NOT in cart, we add it.
        if(!$has_freebie)
            WC()->cart->add_to_cart($freebie_item_id);
    }
}

add_action( 'woocommerce_cart_item_removed', 'remove_freebie', 10, 2 );

function remove_freebie( $cart_item_key, $product_id) {
    $item_special = $product_id;
    $freebie_item_id = 99; // the freebie product
    $hasmaster = false;
    $freebiekey = NULL;
    // Iterating through each item in cart
    foreach( WC()->cart->get_cart() as $item_key => $item ) {
        if ( $item['data']->id == $item_special )
            $hasmaster = true; // Special product is in cart
        elseif ( $item['data']->id == $freebie_item_id ) 
            $freebiekey = $item_key; // Freebie product is in cart
    }
    // AS freebie product is in cart, it's removed too
    if( !$hasmaster && !empty($freebiekey) )
        WC()->cart->remove_cart_item($freebiekey);
}

the code goes in function.php file of your active child theme (or theme) or also in any plugin file.

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

This code is tested and works.

此代码经过测试并可以使用。