Check if current Download is in a specific category - php

So I'm working with the sinlge-download.php page and I'm trying to check if the specific product is in a specific category.
Here is what I tried but I only get the ELSE result even if the download is a book.
if( in_category( 'Books' ) ) {
echo 'This product is a book';
} else {
echo 'This product is not a book';
}

According to EDD docs, the category is: download_category Easy Digital Download Docs
For this... use function has_term since in_category refers to WordPress post type posts and not for custom post types like the downloads.
if( has_term( 'Books', 'download_category' ) ) {
echo 'This product is a book';
} else {
echo 'This product is not a book';
}

You can use this
if( has_term( $term = '', $taxonomy = '', $post = null ) ) {
// do something
}
// $term = Category OR Taxonomy name $taxonomy = Taxonomy Name. OR
// "category" if its default WP category $post = Post ID to check. Leave
// empty to pull this from global query
https://developer.wordpress.org/reference/functions/has_term/

Related

Display custom taxonomy term images for WooCommerce product attributes

In WooCommerce I am using Category and Taxonomy Image plugin that allow me to add the images to product attribute terms.
Now I am trying to display for a specific product attribute, the related term images for each product on shop page.
The author of Category and Taxonomy Image plugin metion to use the following code to display a term image:
if (function_exists('get_wp_term_image'))
{
$meta_image = get_wp_term_image($term_id);
//It will give category/term image url
}
echo $meta_image; // category/term image url
I am using the code below to display "color" product attribute term names on the shop page:
add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
global $product;
$spec_val = $product->get_attribute('spec');
if(!empty($spec_val)) {
echo'<span class="view_attr"> SPECIFICATION: ' . $spec_val . '</span>';
}
}
How To display the term images?
Maybe this is the solution:
add_action('woocommerce_after_shop_loop_item','woo_new_product_tab_content');
function woo_new_product_tab_content() {
global $product;
$ingredients = $product->get_attributes( 'color' );
foreach( $ingredients as $attr_name => $attr ){
foreach( $attr->get_terms() as $term ){
if ( wc_attribute_label( $attr_name ) == "Color" ) {
echo $term->name ;
$meta_image = get_wp_term_image($term->term_id);
echo '<img src="'.$meta_image.'"/>';
}
else echo '';
}
}
}
Product attributes is something very specific and more complex in WooCommerce than other taxonomies. Each product attribute is a taxonomy, has its own terms and can be used for variations on variable products...
The plugins Taxonomy Images and Category and Taxonomy Image allow to have images on all WooCommerce custom taxonomies terms as Product tag and Product attributes (product category has already this feature by default).
Here we use Category and Taxonomy Image and its dedicated function get_wp_term_image().
In the code below you can enable multiple product attributes defined in an array. if the option "Enable Archives?" is enable for the product attribute, you can optionally use the term links.
add_action('woocommerce_after_shop_loop_item','woo_new_product_tab_content');
function woo_new_product_tab_content() {
global $product;
// Define your product attribute labels in the array (label names)
$defined_pa_labels = array( 'Color' );
// Loop through WC_Product_Attribute Objects
foreach( $product->get_attributes() as $taxonomy => $product_attribute ) {
$taxonomy_name = $product_attribute->get_name(); // Slug
$taxonomy_label = wc_attribute_label( $taxonomy_name ); // Name (label name)
if( in_array( $taxonomy_label, $defined_pa_labels ) ) {
// Loop through product attribute WP_Term Objects
foreach( $product_attribute->get_terms() as $term ) {
$term_name = $term->name; // Term name
$term_slug = $term->slug; // Term slug
$term_id = $term->term_id; // Term ID
// Get product attribute term image
if( $image_url = get_wp_term_image( $term_id ) ) {
// Get product attribute term link (optional)
// if the product attribute is enabled on archives)
$term_url = get_term_link( $term, $taxonomy );
// Output
echo '<span style="text-align:center"><img src="'.esc_url( $image_url).'"/>'.$term->name.'</span>';
}
}
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Display only a single linked product category text on any page in Woocommerce

I need to show different product categories on several regular pages but can't find a suitable shortcode for that.
Which shortcode do I have to use to show only single product category (without showing any products from that category) on any page?
Any help is appreciated.
I think that what you are asking is to get the link from a product category to display the linked product category name. For that you can build a custom shortcode:
add_shortcode('linked_pcat', 'display_a_linked_product_category');
function display_a_linked_product_category( $atts ) {
$atts = shortcode_atts( array(
'term' => '',
), $atts, 'linked_pcat' );
$taxomomy = 'product_cat';
$value = $atts['term'];
if( is_numeric( $value ) || (string) (int) $value == $value ) {
$field = 'term_id';
$value = (int) $atts['term'];
} else {
$field = 'slug';
$value = sanitize_title( $atts['term'] );
}
if( term_exists( $value, $taxomomy ) ) {
$term = get_term_by( $field, $value, $taxomomy );
$term_link = get_term_link( $term, $taxomomy );
return '' . $term->name . '';
}
return false;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
USAGE:
Note: For the product category term can use a term name, a term slug or a term ID…
1) In the Wordpress Text Editor (with a term name for example):
[linked_pcat term='Clothing']
2) Inside php code (with a term slug for example):
echo do_shortcode( "[linked_pcat term='t-shirts']" );
3) In a php file between html code (with a term Id for example):
<?php echo do_shortcode( "[linked_pcat term='t-shirts']" ); ?>

Woocommerce | Display text on a single brand page

I want to display text on a single brand page on Woocommerce.
I wrote this snippet on my functions.php but it doesn't seem to work:
function woo_brand_page_check() {
$brands = wp_get_post_terms( $post->ID, ‘product_brand’, array("fields" => "all") );
if (is_tax( 'product_brand' )) {
foreach( $brands as $brand ) {
if($brand == 'BRAND_NAME') {
echo 'Custom text ';
}
}
} else {
echo '';
}
}
add_action('woocommerce_before_shop_loop', 'woo_brand_page_check');
If you have any suggestions on how to implement this, please drop me a line!
Thanks in advance!
If your Brands are product attributes, the taxonomy should be automatically prefixed with 'pa_', so you should use pa_product_brand.
Double check if product_brand has a dash in place of the underscore (as usually happens), so it would become pa_product-brand
Also, you may parr the term in is_tax() instead of looping all terms, something like:
function woo_brand_page_check() {
if ( is_tax( 'pa_product_brand', 'BRAND_NAME' ) ) {
echo 'Custom text ';
}
}
add_action('woocommerce_before_shop_loop', 'woo_brand_page_check');
See: https://codex.wordpress.org/Function_Reference/is_tax
Cheers ;)

WooCommerce Custom Single Product Template

Good morning, I'm trying to change the header depending on the category of the single product. i am using Wordpress & WooCommerce
My product categories look like this
- the-lawn-store
- - turf
- - grass-seed
- - wildflower-turf
- the-oak-store
- - railway-sleepers
- - pergolas
Basically when viewing an item which falls under the parent category of the-lawn-store I need the header to be <?php get_header('lawn'); ?> and when the parent category is the-oak-store I need the header to be <?php get_header('oak'); ?>, the difference between headers is the styling of the over all page! what is the best way to go about this?
Well, what you need is the category parent. In order to do that, first of all you can get the parent ID with this:
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
if($cat_obj) {
//print_r($cat_obj);
$category_ID = $cat_obj->term_id;
$category_parent = $cat_obj->parent;
$category_taxonomy = $cat_obj->taxonomy;
$category_parent_term = get_term_by( 'id', absint( $category_ID ), $category_taxonomy );
$category_parent_slug = $category_parent_term->slug;
get_header( $category_parent_slug );
}else{
get_header();
}
Uncomment the print_r to see the rest of available vars. Tested on my local woo and works.
You can't filter the get_header() function so you will have to override WooCommerce's single-product.php template. From there you can modify the beginning of the file:
get_header( 'shop' ); ?>
I created the following function to get the top-level product category for any product:
function kia_get_the_top_level_product_category( $post_id = null ){
$product_cat_parent = null;
if( ! $post_id ){
global $post;
$post_id = $post->ID;
}
// get the product's categories
$product_categories = get_the_terms( $product_id, 'product_cat' );
if( is_array( $product_categories ) ) {
// gets complicated if multiple categories, so limit to one
// on the backend you can restrict to a single category with my Radio Buttons for Taxonomies plugin
$product_cat = array_shift( $product_categories);
$product_cat_id = $product_cat->term_id;
while ($product_cat_id) {
$cat = get_term($product_cat_id, 'product_cat'); // get the object for the product_cat_id
$product_cat_id = $cat->parent; // assign parent ID (if exists) to $product_cat_id
// the while loop will continue whilst there is a $product_cat_id
// when there is no longer a parent $product_cat_id will be NULL so we can assign our $product_cat_parent
$product_cat_parent = $cat->slug;
}
}
return $product_cat_parent;
}
Then in your theme's single-product.php you could do:
$parent = kia_get_the_top_level_product_category();
if( $parent == 'oak' ){
get_header('oak');
} elseif( $parent == 'lawn' ){
get_header('lawn');
} else {
get_header('shop');
}
If you don't have a specific header-shop.php then you could technically also do:
$parent = kia_get_the_top_level_product_category();
get_header( $parent );
Overriding this template might put you at risk when WooCommerce upgrades. As an alternative I would suggest filtering the body class.
function wpa_22066003_body_class($c){
if( function_exists('is_product') && is_product() && $parent = kia_get_the_top_level_product_category() ){
$c[] = $parent . '-product-category';
}
return $c;
}
add_filter( 'body_class', 'wpa_22066003_body_class' );

Woocommerce Conditional Product Tabs

Okay, I'm trying to show certain tab content depending on what category a product is listed in.
I have created a custom "Size Chart" tab and want to show for example:
Footwear size chart when product is in "Footwear" category
Clothing size chart when product is in "Clothing" category
No size chart when product is in "Accessories" category.
I've created a custom tab like this:
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['new_tab'] = array(
'title' => __( 'Size Chart', 'woocommerce' ),
'priority' => 55,
'callback' => 'woo_tab_content'
);
return $tabs;
}
Then added the custom tab content like this:
function woo_tab_content() {
echo 'This is the size chart';
}
Does anyone know how to add a conditional statement to this code to allow me to specify what content is displayed depending on what category a product is placed in i.e.
is_product_category( 'footwear' )
Thanks!
in you woo_tab_content function simple add what you have done above so it would like this this:
function woo_tab_content() {
if (is_product_category( 'footwear' )){
echo 'This is the size chart';
} else {
//do something else
}
}
Okay, I managed to fix it using the following code:
function woo_tab_content() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'clothing', $categories ) ) {
echo 'this is the clothing tab content!';
} elseif ( in_array( 'shoes', $categories ) ) {
echo 'this is the shoes tab content!';
} else {
echo 'all other tabs';
}
}
It turns out that is_product_category does not work for the single product pages. Only for the category page itself.

Categories