简体   繁体   English

在 Woocommerce 购物车中自动添加或删除免费产品

[英]Add or remove automatically a free product in Woocommerce cart

I'm trying to create code that automatically adds an item to the customer's cart once they reach a particular price point in the cart.我正在尝试创建代码,一旦他们达到购物车中的特定价格点,就会自动将商品添加到客户的购物车中。 AND I'm trying to exclude that from happening if they are only ordering virtual products, as the "free gift" is only meant for products that are being shipped out.如果他们只订购虚拟产品,我试图排除这种情况,因为“免费礼物”仅适用于正在运出的产品。 The code I'm using is adding the free gift at the right dollar amount, but it's not excluding any virtual product.我使用的代码是以正确的美元金额添加免费礼物,但不排除任何虚拟产品。 Can anyone id what I'm doing wrong?任何人都可以识别我做错了什么吗?

Here's the code:这是代码:

/**
 * Add another product depending on the cart total
 */

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
  if ( ! is_admin() ) {
        global $woocommerce;
        $product_id = 85942; //replace with your product id
        $found = false;
        $cart_total = 15; //replace with your cart total needed to add above item

        if( $woocommerce->cart->total >= $cart_total ) {
            //check if product already in cart
            if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {

                $isVirtualOnly = false;
                foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
                    $_product = $values[‘data’];
                    if ($_product != null)
                        if ($_product->get_type() != $_virtual)
                                $isVirtualOnly = false;
                }

                if ($isVirtualOnly != true) {
                    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
                        $_product = $values['data'];
                        if ( $_product->get_id() == $product_id )
                            $found = true;
                    }
                    // if product not found, add it
                    if ( ! $found )
                        $woocommerce->cart->add_to_cart( $product_id );
                }
            } else {
                    // if no products in cart, add it
                    $woocommerce->cart->add_to_cart( $product_id );
            }
        }
    }
}

/**
 * END Add another product depending on the cart total
 */

Updated: The following will auto add to cart a free product:更新:以下内容将自动将免费产品添加到购物车:

  • if there is at least a shippable item in cart,如果购物车中至少有一个可发货的物品,
  • if the free product is not in cart yet,如果免费产品尚未在购物车中,
  • and if the cart subtotal is not below a specific amount.如果购物车小计不低于特定金额。

Or will remove the fee product from cart:或者将从购物车中删除收费产品:

  • if the cart subtotal is not below a specific amount,如果购物车小计不低于特定金额,
  • and if there is only virtual products.如果只有虚拟产品。

The code:编码:

add_action( 'woocommerce_before_calculate_totals', 'add_free_product_to_cart' );
function add_free_product_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $free_product_id = 38; // <= Set the free product id to add
    $min_subtotal    = 80; // <= Set the minimum cart subtotal required

    $has_shippable   = $free_key = false; // Initializing
    $cart_subtotal   = 0;

    // Loop through cart items (first loop)
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
        // Check if free product is in cart
        if ( $free_product_id == $cart_item['product_id'] ) {
            $free_key = $cart_item_key;
            $free_qty = $cart_item['quantity'];
            $cart_item['data']->set_price(0); // Optional: Set free product price to zero
        }

        // Check for non virtual products
        if ( $cart_item['data']->is_virtual() !== true ) {
            $has_shippable = true;
        }
        // Calculate items subtotal: Add discounted Line total with taxes
        $cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];
    }

    // Add Free product
    if ( $cart_subtotal >= $min_subtotal && $has_shippable && $free_key === false ) {
        $cart->add_to_cart( $free_product_id, 1 );
    }
    // Remove free product
    elseif ( ( $cart_subtotal < $min_subtotal  ) && $free_key !== false ) {
        $cart->remove_cart_item( $free_key );
    }
    // Adjust free product quantity to 1
    elseif ( $free_key !== false && $free_qty > 1 ) {
        $cart->set_quantity( $free_key, 1 );
    }
}

// Optional: Display free product price to zero on minicart
add_filter( 'woocommerce_cart_item_price', 'change_minicart_free_gifted_item_price', 10, 3 );
function change_minicart_free_gifted_item_price( $price_html, $cart_item, $cart_item_key ) {
    $free_product_id   = 38;

    if( $cart_item['product_id'] == $free_product_id ) {
        return wc_price( 0 );
    }
    return $price_html;
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

Related:有关的:

What do i need to change when i want 2 free products in my cart?当我想要购物车中的 2 件免费产品时,我需要更改什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动添加或删除 Woocommerce 购物车中的免费产品,但购物车上没有订阅产品 - Add or remove automatically a free product in Woocommerce cart but not with subscription product on the cart 将免费的产品添加到购物车 - Add Woocommerce free product to cart 根据 WooCommerce 购物车总数和月份范围添加或删除免费产品 - Add or remove a free product based on WooCommerce cart total and month range 如果它已经在 WooCommerce 购物车中,请添加免费产品 - Add a free product if it is already in WooCommerce cart Woocommerce:如果购物车有3件商品,则添加免费商品 - Woocommerce: add free product if cart has 3 items WooCommerce单品页面自动加入购物车 - Automatically add product to cart when visiting single product page in WooCommerce "在 WooCommerce 中以最少的购物车数量添加免费的赠品产品" - Add free gifted product for a minimal cart amount in WooCommerce 允许删除Woocommerce中从购物车自动添加的产品 - Allow to remove product automatically added from cart in Woocommerce 在Woocommerce中访问时自动添加到购物车最新发布的产品 - Automatically add to cart latest published product on visit in Woocommerce Woocommerce-在页面加载时,将产品添加到购物车并自动进行结帐 - Woocommerce - On page load, add product to cart and proceed to Checkout automatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM