1

I've got a Wordpress/Woocommerce installation.

On the category page, products either have an 'Add to Cart' button or 'Read more' button depending on whether each is purchasable.

When I look at the is_purchasable() function in Woocommerce, this is the function call that makes the determination:

plugins/woocommerce/includes/class-wc-product-simple.php

public function add_to_cart_text() {
   $text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add to cart', 'woocommerce' ) : __( 'Read more', 'woocommerce' );

   return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
}

This is working as the products all have the correct buttons. Now, I'm trying to figure out the inner workings of is_purchasable. I've scoured through the code in the plugins and themes directories to find instances of is_purchasable being declared somewhere and all I've found are these declarations:

woocommerce/includes/class-wc-product-external.php:

public function is_purchasable() {
    return apply_filters( 'woocommerce_is_purchasable', false, $this );
}

woocommerce/includes/class-wc-product-grouped.php

public function is_purchasable() {
   return apply_filters( 'woocommerce_is_purchasable', false, $this );
}

woocommerce/includes/class-wc-product-variation.php

public function is_purchasable() {
    return apply_filters( 'woocommerce_variation_is_purchasable', $this->variation_is_visible() && parent::is_purchasable() && ( 'publish' === $this->parent_data['status'] || current_user_can( 'edit_post', $this->get_parent_id() ) ), $this );
}

woocommerce/includes/abstracts/abstract-wc-product.php

public function is_purchasable() {
   return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );
}

There is obviously logic somewhere that is determining a product is not purchasable if it's price is not defined or is 0.

Can anyone offer some ideas as to where this logic is hidden?

1
  • 1
    While questions about WooCommerce and other 3rd party plugins/themes/products are offtopic here and not in this stacks scope, the answer to your question is literally inside your question in the code blocks you've shared, it's the second parameter being passed to apply_filters. The reason you're seeing multiple is_purchasable is because there are multiple types of product, and WC uses class inheritance ( a PHP and general programming feature )
    – Tom J Nowell
    Commented Mar 11 at 21:29

0

Browse other questions tagged or ask your own question.