在Woocommerce中自动从购物车中添加或删除免费赠品产品 [英] Auto add or remove a freebie product from cart in Woocommerce

查看:222
本文介绍了在Woocommerce中自动从购物车中添加或删除免费赠品产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经营一个WooCommerce商店,在您向购物篮中添加明显的产品之后,我们想为每个客户提供免费赠品(电子书),该赠品将显示在购物篮中.

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.

示例:
您将"product1"添加到购物篮,该购物篮现在将显示2个产品. "product1"和"freebie". 当您从购物篮中取出产品时,免费赠品将再次被删除.

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.

我现在获得了以下代码:

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.

我做错了什么?

任何帮助将不胜感激.

推荐答案

更新2 -2018年10月-改进和增强的代码(完全重新访问)

Update 2 - October 2018 - Improved and enhanced code (Completely revisited)

以下代码将在第一次添加到购物车时添加一次免费赠品.如果所有其他购物车商品都被删除,则免费赠品也将被删除:

The following code will add a freebie product on first add to cart just once. If all other cart items are removed, the freebie item will be removed too:

add_action( 'woocommerce_before_calculate_totals', 'add_remove_freebie', 50, 1 );
function add_remove_freebie( $cart ) {

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $freebie_id = 70; // <== HERE set the freebie product ID
    $has_others = false;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Added Woocommerce compatibility version
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();

        if( $product_id == $freebie_id ) {
            // Freebie is in cart
            $freebie_key = $cart_item_key;
        } else {
            // Other items are in cart
            $has_others = true;
        }
    }
    // If freebie product is alone in cart we remove it
    if( ! $has_others && isset( $freebie_key ) ){
        $cart->remove_cart_item( $freebie_key );
    } elseif ( $has_others && ! isset( $freebie_key ) ) {
        $cart->add_to_cart($freebie_id);
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中..经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce中自动从购物车中添加或删除免费赠品产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆