代码之家  ›  专栏  ›  技术社区  ›  developerme

买一送一,无优惠券代码

  •  1
  • developerme  · 技术社区  · 8 年前

    我想在我的网站上实现功能,当用户购买一个x产品,然后用户将获得一个y产品作为免费。

    在我的网站上我有各种各样的产品。如果顾客购买1公斤一包的X产品,那么顾客想免费得到30毫升的Y产品。

    我在function.php中添加了下面的代码,但它的工作问题是刷新页面时礼品产品数量正在增加。 我的更新代码。

        add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );
    
    function bbloomer_apply_matched_coupons() {
    
        global $woocommerce;
     $cat_in_cart = false;
        foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
    
        // this is your product ID
        $autocoupon = array( 123411 );
    
        if( in_array( $values['variation_id'], $autocoupon ) ) {  
           $cat_in_cart = true;
            break;
    
        }
    
    
        }
     if ( $cat_in_cart ) {   
    $product_id   = 2044;
    $quantity     = 1;
    $variation_id = 2046;
    $variation    = array(
        'pa_size'  => '30ml'
    );
    
    $woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
     }
    
    }
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Outsource WordPress    8 年前

    将此添加到“functions.php”的开头。

    ob_start();
    session_start();
    

    然后添加此代码。

    add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );
    
    function bbloomer_apply_matched_coupons() {
    
        if(!$_SESSION['GiftAdded']) {
            global $woocommerce;
            $cat_in_cart = false;
            $coupon_in_cart = false;
    
            $autocoupon = array( 123411 ); // variation ids of products that offers gifts
            $freecoupon = array( 2046 ); // variation ids of products that are gift coupons
    
            foreach ( $woocommerce->cart->cart_contents as $key => $values ) {      
                if( in_array( $values['variation_id'], $autocoupon ) ) {  
                    $cat_in_cart = true;                
                }
                if( in_array( $values['variation_id'], $freecoupon) ) {  
                    $coupon_in_cart = true;             
                }
            }
    
             if ( $cat_in_cart && !$coupon_in_cart ) {   
                $product_id   = 2044;
                $quantity     = 1;
                $variation_id = 2046;
                $variation    = array( 'pa_size'  => '30ml' );
    
                $woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
                $_SESSION['GiftAdded']=true;
            }
        }
    }
    

    这个 $_SESSION['GiftAdded'] 将阻止礼品产品在手动删除时再次添加。

        2
  •  1
  •   LoicTheAztec    7 年前

    当数量发生变化或产品从购物车中取出并将免费产品的价格设置为零时,根据数量使用它处理的另一个钩子还有另一种选择:

    add_action( 'woocommerce_before_calculate_totals', 'add_free_product_to_cart', 20, 1 );
    function add_free_product_to_cart( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        $products_ids = array( 123411 ); // Products IDs to check
        $free_var_id  = 2046; // free product (variation ID)
        $free_prod_id = 2044; // free product (product ID)
        $free_attr    = array( 'pa_size'  => 'medium' ); // Free product attribute
        $free_price   = 0;
    
        $targeted = $free = array('qty' => 0 ); // Initializing
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( in_array( $cart_item['data']->get_id(), $products_ids ) ) {
                $targeted['qty'] += $cart_item['quantity'];
            } elseif ( $cart_item['data']->get_id() == $free_var_id ) {
                $cart_item['data']->set_price($free_price);
                $free['qty'] += $cart_item['quantity'];
                $free['key'] = $cart_item_key;
            }
        }
        // We exit (No changes are needed)
        if( $targeted['qty'] == $free['qty'] )
            return;
    
        if( $targeted['qty'] > 0 && $free['qty'] == 0 )
        {
            // Add the free product
            $cart->add_to_cart( $free_prod_id, $targeted['qty'], $free_var_id, $free_attr );
        }
        elseif(  $targeted['qty'] > 0 && $free['qty'] > 0 && $targeted['qty'] != $free['qty'] )
        {
            // Adjust free product quantity
            $cart->set_quantity( $free['key'], $targeted['qty'] );
        }
        elseif(  $targeted['qty'] == 0 && $free['qty'] > 0)
        {
            // Remove free product
            $cart->remove_cart_item( $free['key'] );
        }
    }
    

    代码放在活动子主题(或主题)的function.php文件或任何插件文件中。

    推荐文章