最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Set "woocommerce_is_purchasable" to false for specific "$product->is_stock_status&

programmeradmin1浏览0评论

I created a plugin to add a new stock status to my woocommerce products. I used this as my reference (copy and pasted into my plugins php file): Create WooCommerce custom stock option that is non-purchasable.

Everything works as it should but I also want to disable the add to cart button for products with

is_stock_status( 'callavailability' )

I think i can acheive this with

add_filter( 'woocommerce_is_purchasable', false );

but I can't figure out how to trigger the filter for only those specific products.

Here is the code I have so far:

add_filter( 'woocommerce_is_purchasable', 'command' );

function command( $value )
{
    if ( $product->is_stock_status( 'callavailability' ) )
        $value[] = 'false';

    return $value;
}

When I put this in my child theme's functions.php via ftp, all pages that contain products (single, archive, etc) are blank with only the site header visible and no footer. All non-woocommerce pages work fine and even products displayed via shortcode show up on the non-woocommerce pages.

I have no idea what I'm doing wrong and I have literally tried over 50 examples and guides on google and I always either get blank pages or http 500 errors.

Once I get this figured out, I also need to add a badge (Something like the sale badge) with custom text and styling (colour) I assume its not too difficult since when products are set to Out of stock they have an " out of stock" badge already.

Thanks

I created a plugin to add a new stock status to my woocommerce products. I used this as my reference (copy and pasted into my plugins php file): Create WooCommerce custom stock option that is non-purchasable.

Everything works as it should but I also want to disable the add to cart button for products with

is_stock_status( 'callavailability' )

I think i can acheive this with

add_filter( 'woocommerce_is_purchasable', false );

but I can't figure out how to trigger the filter for only those specific products.

Here is the code I have so far:

add_filter( 'woocommerce_is_purchasable', 'command' );

function command( $value )
{
    if ( $product->is_stock_status( 'callavailability' ) )
        $value[] = 'false';

    return $value;
}

When I put this in my child theme's functions.php via ftp, all pages that contain products (single, archive, etc) are blank with only the site header visible and no footer. All non-woocommerce pages work fine and even products displayed via shortcode show up on the non-woocommerce pages.

I have no idea what I'm doing wrong and I have literally tried over 50 examples and guides on google and I always either get blank pages or http 500 errors.

Once I get this figured out, I also need to add a badge (Something like the sale badge) with custom text and styling (colour) I assume its not too difficult since when products are set to Out of stock they have an " out of stock" badge already.

Thanks

Share Improve this question edited Sep 26, 2019 at 16:40 Tristan Glaw asked Sep 26, 2019 at 16:34 Tristan GlawTristan Glaw 111 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You are using not existing variable $product

try this:

add_filter( 'woocommerce_is_purchasable', 'command', 10, 2 );

function command( $value, $product ) {
    if ( $product->is_stock_status( 'callavailability' ) )
        $value[] = 'false';

    return $value;
}

The first parameter is actually a boolean value.

发布评论

评论列表(0)

  1. 暂无评论