Reputation: 2703
I have edited some templates for my WooCommerce shop. I have removed the default price because I use the plugin WooCommerce Extra Product Options for enabling the price and control some specific options.
Now, I want to add downloadable products to my shop. The plugin doesn't support downloads because there is not enough API for that, the author says.
So, I want the price back in my template but only for products where 'download' is turned on. How can I check if a product is downloadable via themplates? Otherwise, all the downloadable products are placed int he category 'Downloads', so a check if the product is in that category is also good for me.
Can you help me?
Upvotes: 6
Views: 7604
Reputation: 6297
You can use is_downloadable() function of WP. WC_Product::is_downloadable() – Checks if a product is downloadable or not.
$bool = WC_Product::is_downloadable();
It is located in
File name: woocommerce/includes/abstracts/abstract-wc-product.php
public function is_downloadable() {
return $this->downloadable == 'yes' ? true : false;
}
Just call this on product-page template.
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $woocommerce, $product;
if ($product->is_downloadable('yes')) {
// Your Logic.
}else{
// Your Logic.
}
?>
For more details See the Link. and another link
Upvotes: 6
Reputation: 2158
You can check it through is_downloadable function. Add it anywhere in your product-page template.
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $woocommerce, $product;
if ($product->is_downloadable('yes')) {
// Your Logic.
}else{
// Your Logic.
}
?>
Upvotes: 4