Disable add to cart button based on WooCommerce product custom stock status

假如想象 提交于 2021-02-16 20:00:11

问题


Currently in woocommerce, add to cart button disabled, if the stock status is out of stock. I add new stock status with the label Discontinued product by using woocommerce_product_stock_status_options, now I am looking for a way to treat this product like it is out of stock.

Since I believe it is better to separate between a product that is not produced anymore and a product that produced and available in another store but it's not in stock.


回答1:


You can use the following to disable add to cart button based on a custom stock status (where you will replace custom_status_slug by your custom status slug):

add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
    if ( $product->get_stock_status() === 'custom_status_slug' ) {
        return false;
    }

    return $purchasable;
}

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



来源:https://stackoverflow.com/questions/65856878/disable-add-to-cart-button-based-on-woocommerce-product-custom-stock-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!