【问题标题】:Add or remove a free product based on WooCommerce cart total and month range根据 WooCommerce 购物车总数和月份范围添加或删除免费产品
【发布时间】:2021-06-12 00:36:11
【问题描述】:

我正在处理一个项目,该项目需要在特定促销月份将免费商品添加到购物车。因此,我需要将产品价值设为 0.00 并在特定促销月份将其自动添加到购物车。到目前为止,当添加到购物车的总数达到一定数量时,我已经自动添加了产品

/*
 * Automatically adding the product to the cart when cart total amount reach to $500.
 */

function aapc_add_product_to_cart() {
    global $woocommerce;
    
    $cart_total = 500;  

    if ( $woocommerce->cart->total >= $cart_total ) {
        if ( ! is_admin() ) {
            $free_product_id = 12989;  // Product Id of the free product which will get added to cart
            $found      = false;

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

add_action( 'template_redirect', 'aapc_add_product_to_cart' );
...

【问题讨论】:

  • 谢谢@LoicTheAztec,你救了我的命,效果很好,当客户将可变产品添加到购物车时,我还需要添加一个促销弹出窗口,这可能吗?什么是sn-p?非常感谢您的热心帮助...
  • @LoicTheAztec 感谢您让我知道,我已经接受了答案,也赞成它。谢谢

标签: php wordpress woocommerce cart hook-woocommerce


【解决方案1】:

改用以下方法(处理购物车页面中的动态变化还有一个月范围)。

注意:阈值的总金额只能是购物车项目的总和。

代码:

function is_free_product_allowed_for_month() {
    // Define allowed months in the array (values from 1 to 12)
    $allowed_months   = array('3', '7', '8');

    return in_array( date('n'), $allowed_months );
}


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

    // Check if we are in an allowed month
    if ( ! is_free_product_allowed_for_month() )
        return;

    $free_product_id  = 339; // ID of the free product
    $threshold_amount = 200; // The threshold amount for cart subtotal
    $cart_items_total = 0; // Initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
        // Check if the free product is in cart
        if ( in_array( $free_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
            $cart_item['data']->set_price(0); // Set price to Zero
            $free_item_key = $cart_item_key;

        }
        // Get cart subtotal incl. tax from items (with discounts if any)
        $cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
    }

    // If Cart total is up to the defined amount and if the free products is not in cart, we add it.
    if ( $cart_items_total >= $threshold_amount && ! isset($free_item_key) ) {
        $cart->add_to_cart( $free_product_id );
    }
    // If cart total is below the defined amount and free product is in cart, we remove it.
    elseif ( $cart_items_total < $threshold_amount && isset($free_item_key) ) {
        $cart->remove_cart_item( $free_item_key );
    }
}


// For minicart displayed free product price
add_filter( 'woocommerce_cart_item_price', 'free_product_cart_item_price', 10, 3 );
function free_product_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
    // Check if we are in an allowed month
    if ( ! is_free_product_allowed_for_month() )
        return $price_html;

    $free_product_id  = 339; // ID of the free product

    if ( in_array( $free_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
        return wc_price( 0 );
    }
    return $price_html;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

相关:Add or remove specific cart Item based on WooCommerce cart total

【讨论】:

    猜你喜欢
    • 2021-05-09
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多