简体   繁体   中英

Hiding 'Add to Cart' button on WooCommerce variable products

I'm trying to hide the Add to Cart button when a variable product is at a certain stock level (so retailers and wholesalers see out of stock at different stock levels). I have managed to do this for simple products but am really struggling as to how to implement this with variable products.

For the simple product I put

<?php 
if ( $product->get_stock_quantity() <= 2 && current_user_can('customer')) { 
    echo 'out of stock'; 
} else { //'add to cart' button }

I don't know if it's possible to implement something similar for variable products? Any help appreciated.

从woocommerce获取产品ID,然后使用该产品ID实施您的代码。

At first glance, I think your if statement is evaluating incorrectly; you're missing brackets.

Try

<?php if ( ($product->get_stock_quantity() <= 2) && (current_user_can('customer'))) { 
echo 'out of stock'; } else { //'add to cart' button }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM