简体   繁体   English

如果产品已经在购物车中,则禁用Woocommerce添加到购物车按钮

[英]Disable Woocommerce add to cart button if the product is already in cart

I am trying to set the functionality in Woocommerce on the Add to Cart button to only allow a particular product to be added once to the Cart. 我试图在“添加到购物车”按钮上的Woocommerce中设置功能,以仅允许将特定产品添加到购物车一次。

Once a particular product has been added to cart the first time, the Add to Cart needs to be hidden. 首次将特定产品添加到购物车后,需要隐藏“添加到购物车”。

In the cart I can have any number of products - just a max of quantity 1 for each product. 在购物车中,我可以有任意数量的产品-每个产品最多只能有数量1。

I was doing research and saw that I can use woocommerce_add_to_cart_validation hook. 我正在做研究,发现可以使用woocommerce_add_to_cart_validation挂钩。 But have no idea how to start off. 但是不知道如何开始。

How can I allow a particular product to be added once to the Cart? 如何允许将特定产品添加到购物车一次?

Disable add to cart button if product is in cart using woocommerce_is_purchasable hook: 如果产品使用woocommerce_is_purchasable挂钩在购物车中,则禁用添加到购物车按钮:

add_filter( 'woocommerce_is_purchasable', 'disable_add_to_cart_if_product_is_in_cart', 10, 2 );
function disable_add_to_cart_if_product_is_in_cart ( $is_purchasable, $product ){
    // Loop through cart items checking if the product is already in cart
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_id() == $product->get_id() ) {
            return false;
        }
    }
    return $is_purchasable;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works (even for product variations in variable products) . 经过测试和工作(即使是可变产品中的产品变化)


Original answer: Here is an example using woocommerce_add_to_cart_validation hook and that will do the trick (preventing add to cart action and displaying a custom notice when needed), and using a custom utility function that will remove quantity field for your specific defined product ID: 原始答案:这是一个使用woocommerce_add_to_cart_validation钩子的示例,它将完成此工作(防止添加到购物车操作并在需要时显示自定义通知),并使用一个自定义实用程序功能,该功能将删除您定义的特定产品ID的数量字段:

add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 3 );
function limit_cart_items_from_category ( $passed, $product_id, $quantity ){
    // HERE define your product ID
    $targeted_product_id = 37;

    // Check quantity and display notice
    if( $quantity > 1 && $targeted_product_id == $product_id ){
        wc_add_notice( __('Only one item quantity allowed for this product', 'woocommerce' ), 'error' );
        return false;
    }

    // Loop through cart items checking if the product is already in cart
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( $targeted_product_id == $product_id && $cart_item['data']->get_id() == $targeted_product_id ) {
            wc_add_notice( __('This product is already in cart (only one item is allowed).', 'woocommerce' ), 'error' );
            return false;
        }
    }
    return $passed;
}

// Checking and removing quantity field for a specific product 
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
    // HERE define your product ID
    $targeted_product_id = 37;

    if( $targeted_product_id == $product->get_id() )
        $args['min_value'] = $args['max_value'] = $args['input_value'] = 1;

    return $args;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果产品已经在 WooCommerce 购物车中,则禁用添加到购物车按钮 - Disable add to cart button if product is already in WooCommerce cart 根据 WooCommerce 产品自定义库存状态禁用添加到购物车按钮 - Disable add to cart button based on WooCommerce product custom stock status 在单个产品页面/ Wordpress / Woocommerce中禁用Ajax添加到购物车按钮 - Disable ajax for add to cart button in single product page / Wordpress / Woocommerce Woocommerce - 当产品处于缺货状态时禁用 add_to_cart 按钮 - Woocommerce - disable add_to_cart button when product is on backorder 通过 WooCommerce 产品设置中的自定义复选框禁用添加到购物车按钮 - Disable add to cart button via custom checkbox in WooCommerce product settings 在Woocommerce产品类别归档页面上禁用“添加到购物车”按钮 - Disable add to cart button on Woocommerce product category archive pages 如果它已经在 WooCommerce 购物车中,请添加免费产品 - Add a free product if it is already in WooCommerce cart WooCommerce:当产品已在购物车中时更改添加到购物车的文本 - WooCommerce: change the add to cart text when the product is already in cart WooCommerce-产品已经在购物车中,更改“添加到购物车”文本为ajax - WooCommerce - Product already in cart, change “add to cart” text ajax WooCommerce add_to_cart简码-产品已在购物车中 - WooCommerce add_to_cart Shortcode - Product already in Cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM