【问题标题】:Force specific WooCommerce product to be sold in separate order强制以单独的顺序销售特定的 WooCommerce 产品
【发布时间】:2021-07-10 18:28:27
【问题描述】:

我正试图强制 WooCommerce 中的特定产品单独销售。 但是,我希望此产品无限量销售。

基于Force sold individually product to be bought alone in WooCommerce 运行良好的答案代码,我目前正在使用:

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
    // Product id to bought alone 
    $product_id_alone = 666;
 
    // Set variable
    $alone = true;
 
    // If passed
    if ( $passed ) {
        // If cart is NOT empty when a product is added
        if ( !WC()->cart->is_empty() ) {
 
            // If product id added = product id alone
            if ( $product_id_alone == $product_id ) {
                $alone = false;
            } else {
                // Generate a unique ID for the cart item
                $product_cart_id = WC()->cart->generate_cart_id( $product_id_alone );
 
                // Check if product is in the cart
                $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
 
                // If product is already in cart
                if ( $in_cart ) {
                    $alone = false;
                }
            }
        } else {
 
            if ( $product_id_alone == $product_id) {
                $alone = true;         
            }
        }
    }
 
    if ( $alone == false ) {
        // Set error message
        $message = 'Product 666 must be bought separately.';
        wc_add_notice( __( $message, 'woocommerce' ), 'error' );
        $passed = false;
    }
 
    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );

如果购物车是空的,我可以添加带有自定义数量的 ID 666 的产品。

将 ID 666 的产品添加到购物车后,我无法将其他产品添加到购物车。

如果我先将另一个产品添加到空购物车,我无法将产品 ID 666 添加到购物车。

问题在于,如果我将产品 ID 666 添加到空购物车中,我无法通过将更多产品添加到购物车中来增加产品 666 的数量。

【问题讨论】:

    标签: php wordpress woocommerce product cart


    【解决方案1】:

    要强制以单独的顺序单独销售特定产品,请使用:

    function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
        // Product id to bought alone 
        $product_id_alone = 666;
    
        // Set variable
        $flag = false;
    
        // If cart is NOT empty when a product is added
        if ( ! WC()->cart->is_empty() ) {
            // Generate a unique ID for the cart item
            $product_cart_id = WC()->cart->generate_cart_id( $product_id_alone );
    
            // Check if product is in the cart
            $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
    
            // If product is already in cart & product ID added is not equal to product ID alone
            if ( $in_cart && ( $product_id != $product_id_alone ) ) {
                $flag = true;
            // Product ID alone is NOT in cart & product ID added is equal to product ID alone
            } elseif( ! $in_cart && ( $product_id == $product_id_alone ) ) {
                $flag = true;
            }
        }
    
        // True
        if ( $flag ) {
            // Set error message
            wc_add_notice( sprintf(
                __( 'Product %s must be bought separately', 'woocommerce' ),
                $product_id_alone,
            ), 'error' );
    
            // Passed = false
            $passed = false;
        }
    
        return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
    

    添加 ID 为 666 的产品时:

    • 如果购物车是空的,可以添加任意数量的产品。
    • 如果购物车不为空且 ID 为 666 的产品不在购物车中,请停止。
    • 如果购物车不为空且 ID 为 666 的产品在购物车中,请继续。

    当添加具有不同 ID 的产品时:

    • 如果购物车是空的,可以添加任意数量的产品。
    • 如果购物车不为空且 ID 为 666 的产品不在购物车中,请继续。
    • 如果购物车不为空且 ID 为 666 的产品在购物车中,请停止。

    【讨论】:

    • 非常感谢!完全按照我的意愿去做。
    【解决方案2】:

    这是我的脚本,它根据某个标签的值防止某种类型的产品与另一个产品的顺序相同

    function wc_giftcard_with_own_order( $passed_validation, $product_id ) {
        $cart = WC()->cart->get_cart();
        
        if ( empty($cart) ) {
            // carrello vuoto, per cui passa
            return $passed_validation;
        }
        
        // carrello non vuoto
        // conta quanti prodotti sono di tipo gift card e quanti no
        // tag "gift card" = ID tag 340
        $coupon_si = 0;
        $coupon_no = 0;
        
        foreach ( $cart as $cart_item_key => $cart_item )
            if ( has_term( 340, 'product_tag', $cart_item['product_id'] ) ) {
                $coupon_si += 1;
            }
            else {
                $coupon_no += 1;
            }
            
        // se il prodotto corrente è una gift card, allora può essere aggiunto ad un carrello con soli articole gift card
        // se il prodotto corrente non è una gift card, allora può essere aggiunto ad un carrello con soli articoli non gift card
        if ( has_term( 340, 'product_tag', $product_id ) ) {
            if ( $coupon_no == 0 ) {
                return $passed_validation;
            }
            else {
                wc_add_notice( __( 'La gift card NON può essere acquistata con altri prodotti', 'woocommerce' ), 'error' );
                return false;
            }
        }
        else {
            if ( $coupon_si == 0 ) {
                return $passed_validation;
            }
            else {
                wc_add_notice( __( 'In ordine esiste già almeno una gift card, che NON può essere acquistata con altri prodotti', 'woocommerce' ), 'error' );
                return false;
            }
        }
        
        return $passed_validation;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'wc_giftcard_with_own_order', 10, 2 );
    

    【讨论】:

    • 这个答案似乎不适用于这个问题,你为什么在这里发布这个?问题中根本没有提到产品标签
    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多