【问题标题】:Auto add or remove a freebie product from cart in Woocommerce在 Woocommerce 中自动从购物车中添加或删除免费赠品产品
【发布时间】:2017-01-07 03:31:34
【问题描述】:

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

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

我现在得到了这个代码:

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);
    }
}

但它似乎还没有完全起作用。

我做错了什么?

任何帮助将不胜感激。

【问题讨论】:

  • 谢谢,我会尽快处理的。

标签: php wordpress woocommerce cart product


【解决方案1】:

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

以下代码将在第一次添加到购物车时添加免费赠品产品一次。如果所有其他购物车物品都被移除,那么免费赠品也将被移除:

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 文件中。经过测试并且可以工作。

【讨论】:

  • 非常感谢 Loic,这非常完美! - 但是有一个小问题。也许我没有像预期的那样解释自己,但我希望将免费赠品与我所有的产品一起添加到购物篮中。因此,即使它是产品 1、6、27、1042 或 621,我也希望添加免费赠品 :-) 但如果它只是一个特定的产品,那么这段代码就完美了!我只有一个类别,所以也许我可以用 category_speciel 更改 item_speciel?
  • 好伙伴,我会等待更新,如果它按预期工作,请点赞。
  • 非常感谢,我无法形容我有多高兴,你让它工作了 :-) 该功能现在已经在我的网站上运行,并且运行良好!接受和赞成现在是你的 :-) 我希望许多其他帮助可以将其用于类似目的。
猜你喜欢
  • 2021-05-09
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
相关资源
最近更新 更多