【问题标题】:Woocommerce: How to allow only one product type in a cart?Woocommerce:如何在购物车中只允许一种产品类型?
【发布时间】:2019-08-07 18:57:52
【问题描述】:

我有两种产品类型,一种是简单产品$cart_item['default-engraving'],另一种是信用产品$cart_item['default-engraving'] && $cart_item['iconic-engraving']。我正在尝试找到一个解决方案,如果我将简单的产品添加到购物车,则不应添加信用产品。或者,如果我将信用产品添加到购物车,则不应添加简单产品。但如果我愿意,我可以根据需要添加相同的类型,例如简单的产品类型。

【问题讨论】:

  • 我改进了格式和语法。

标签: php wordpress woocommerce product cart


【解决方案1】:

更新:无法在添加到购物车事件时检测自定义购物车商品数据。

检查购物车项目将允许您防止购物车项目同时具有 $cart_item['default-engraving']$cart_item['iconic-engraving']

add_action( 'woocommerce_check_cart_items', 'check_cart_items_custom_data' );
function check_cart_items_custom_data() {
    // Initializing: set the current product type in an array
    $types = [];

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $item ){
        if( isset( $item['default-engraving'] ) )
            $types[] = 'default';

        if( isset( $item['iconic-engraving'] ) )
            $types[] = 'iconic';
    }

    $types = array_unique( $types );

    // Check the number of product types allowing only one
    if( count( $types ) > 1 ){

        // Displaying a custom notice and avoid checkout
        wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
    }
}

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


原始答案(它不适用于您的情况,因为在添加到购物车事件中无法检测到)

以下是针对产品类型以仅允许购物车中的一种的方式:

add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_type_allowed', 10, 3 );
function only_one_product_type_allowed( $passed, $product_id, $quantity ) {

    // Initializing: set the current product type in an array
    $types = [ wc_get_product( $product_id )->get_type() ];

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $item ){
        // Set each product type in the array
        $types[] = wc_get_product( $item['product_id'] )->get_type();
    }

    $types = array_unique( $types );

    // Check the number of product types allowing only one
    if( count( $types ) > 1 ){

        // Displaying a custom notice
        wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
        return false; // Avoid add to cart
    }

    return $passed;
}

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

相关:Allow only one product category in cart at once in Woocommerce

【讨论】:

  • 感谢您的回复,但我有两种产品类型,其中一种是标志性的(我添加了一些关于信用的信息)另一种是简单的产品。我需要将它们单独添加到购物车中,但客户想购买多少就多少。每个产品一开始都是简单的产品,但是如果您选择一些标志性选项(信用选项),它会变为标志性并关心一些额外的信息并且它会更改,例如可下载产品(定制产品)。因此,此定制产品不应与普通产品一起放在购物车中,或者反向正常产品不应与标志性产品一起放在购物车中。
  • @ShamsMamadjonov 我已经更新了我的答案......正如所解释的,无法在添加到购物车事件中检测到自定义购物车项目数据,因此唯一的方法是防止您的情况下结帐......
【解决方案2】:

你可以试试这个

    add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {

    // Getting the product categories term slugs in an array for the current product
    $term_slugs   = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){

        // Check if the product category of the current product don't match with a cart item

        $_product = wc_get_product( $cart_item['product_id']; );
if( $_product->is_type( 'simple' ) ) {
// code to not allow other product type
} else {
// do stuff for everything else
}
    }
}

【讨论】:

  • 你错过了一个结束 },虽然我将代码添加到项目中,但它不允许我将任何产品添加到购物车。
【解决方案3】:

请检查下面的代码。

<?php
function ji_woocommerce_add_to_cart_validation( $valid, $product_id, $quantity ) 
{
        $_product =  wc_get_product( $product_id);      
        foreach (WC()->cart->get_cart() as $item ){
            $checkTypes[] = wc_get_product( $item['product_id'] )->get_type();
        }
        $checkTypes = array_unique( $types );

        if(count($checkTypes) > 0)
        {
            if(!in_array($_product->get_type(),$checkTypes))
            {
            wc_add_notice( __('Here you can add your error message','textdomain'), 'error' );
                 return false;
            }
        }
        return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'ji_woocommerce_add_to_cart_validation', 10, 3 );
?>

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2018-08-14
    • 2022-07-11
    • 2023-04-05
    相关资源
    最近更新 更多