【问题标题】:Add or remove automatically a free product in Woocommerce cart在 Woocommerce 购物车中自动添加或删除免费产品
【发布时间】:2021-05-09 04:59:56
【问题描述】:

我正在尝试创建代码,以便在客户达到购物车中的特定价格点后自动将商品添加到客户的购物车中。如果他们只订购虚拟产品,我试图排除这种情况,因为“免费礼物”仅适用于正在运出的产品。我使用的代码是以正确的美元金额添加免费礼物,但不排除任何虚拟产品。谁能知道我做错了什么?

代码如下:

/**
 * 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
 */

【问题讨论】:

    标签: php wordpress woocommerce product cart


    【解决方案1】:

    更新:以下内容将自动将免费产品添加到购物车:

    • 如果购物车中至少有一件可发货的商品,
    • 如果免费产品尚未放入购物车,
    • 如果购物车小计不低于特定金额。

    或者将从购物车中删除收费产品:

    • 如果购物车小计不低于特定金额,
    • 如果只有虚拟产品。

    代码:

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

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    相关:

    【讨论】:

    • 感谢您的回答。不幸的是,当我添加它时,我在页面上收到以下错误: ParseError throwed syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' Any建议?
    • @BrettBrooks 已更新,经过测试并且可以正常工作。抱歉耽搁了。如果这个答案回答了你的问题,你可以请accept回答,谢谢。
    • 效果很好!太感谢了! :D
    • 在 ajax 请求上,line_totalline_tax 包含最后一个请求的值,而不是当前请求的值,但是 quantity 是正确的。我用$cart_subtotal += $cart_item['quantity'] * wc_get_price_including_tax( $cart_item['data'] ); 替换了这条线,现在这就像魅力一样。
    猜你喜欢
    • 2017-01-07
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2015-09-12
    • 1970-01-01
    • 2014-02-06
    相关资源
    最近更新 更多