久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如果產品已在購物車中,請禁用 Woocommerce 添加到

Disable Woocommerce add to cart button if the product is already in cart(如果產品已在購物車中,請禁用 Woocommerce 添加到購物車按鈕)
本文介紹了如果產品已在購物車中,請禁用 Woocommerce 添加到購物車按鈕的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試將 Woocommerce 中的添加到購物車"按鈕上的功能設置為只允許將特定產品添加到購物車一次.

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.

第一次將特定產品添加到購物車后,需要隱藏添加到購物車.

Once a particular product has been added to cart the first time, the Add to Cart needs to be hidden.

在購物車中,我可以有任意數量的產品 - 每個產品最多只能有 1 個.

In the cart I can have any number of products - just a max of quantity 1 for each product.

我在做研究,發現我可以使用 woocommerce_add_to_cart_validation 鉤子.但不知道如何開始.

I was doing research and saw that I can use woocommerce_add_to_cart_validation hook. But have no idea how to start off.

如何允許將特定產品添加到購物車一次?

How can I allow a particular product to be added once to the Cart?

推薦答案

如果產品在購物車中,則使用 woocommerce_is_purchasable 鉤子禁用添加到購物車按鈕:

Disable add to cart button if product is in cart using woocommerce_is_purchasable hook:

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;
}

代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試且有效(即使對于可變產品中的產品變體).

Code goes in function.php file of your active child theme (or active theme). Tested and works (even for product variations in variable products).

原答案:這是一個使用 woocommerce_add_to_cart_validation 鉤子的示例,它可以解決問題(防止添加到購物車操作并在需要時顯示自定義通知),并使用自定義實用程序功能刪除您特定定義的數量字段產品編號:

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:

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;
}

代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

這篇關于如果產品已在購物車中,請禁用 Woocommerce 添加到購物車按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 电影91久久久 | 日本在线精品视频 | 亚洲国产一区在线 | 精品av天堂毛片久久久借种 | 成人午夜黄色 | 久久精品久久久久久 | 一区二区视频在线观看 | 成人在线免费看 | 国产精品久久国产精品 | av网站在线播放 | 久久精品一区二区 | 岛国av在线免费观看 | 国产在线播放一区二区三区 | 日本高清aⅴ毛片免费 | 日韩一区二区三区在线视频 | 欧美日韩免费 | 国产精品久久影院 | 亚洲成av | 国产一区二区三区视频 | 蜜桃精品视频在线 | 亚洲成人网在线播放 | 99re在线视频| 成年精品 | 91不卡| 国产精品一区二区欧美 | 黄色一级大片在线免费看产 | 亚洲情侣视频 | 日本污视频| 国产成人精品久久二区二区91 | 狠狠av| 亚洲高清在线 | 国产乱肥老妇国产一区二 | 日韩成人免费视频 | 国产超碰人人爽人人做人人爱 | 国产亚洲精品美女久久久久久久久久 | 久久久久国产精品午夜一区 | 在线欧美视频 | 久久精品小视频 | 亚洲成人精品在线观看 | 国产精品www | 国产精品污www一区二区三区 |