I've created a bunch of shortcodes that are either used within posts or echo'd into product pages using wordpress hooks.
I'm trying to create a conditional function so it echos a specific shortcode dependent on the product category. It would need to echo into the 'woocommerce_after_single_product_summary'.
This is the draft so far, can someone give some advice on how to complete this please?
add_action( 'woocommerce_after_single_product_summary', 'add_warranty_notice', 15 );
function add_warranty_notice() {
if ( is_product_category( 'xx-slug' ) ) {
echo do_shortcode("[xx-product-notice]");
} elseif ( is_product_category( 'xy-slug' ) ) {
echo do_shortcode("[xy-product-notice]");
}
}
Hope this help you.
add_action( 'woocommerce_after_single_product_summary', 'add_warranty_notice', 15 );
function add_warranty_notice() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$nterms = get_the_terms( $post->ID, 'product_tag' );
foreach ($terms as $term ) {
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}
if ( $product_cat_name == 'parent_one' ) {
echo do_shortcode("[xx-product-notice]");
} elseif ( $product_cat_name == 'parent_two' ) {
echo do_shortcode("[xy-product-notice]");
}else{
}
}
Related
I need to see the categories in the body to call certain CSS. I tried to add a second category term to the allready excisting working code. But it won't work for the newly added category (portfolio_category). What am I doing wrong?
//add taxonomy to body class
add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
// Add taxonomy terms name to body class
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$taxonomy_terms = get_the_terms($post->ID, 'arbocategory', 'portfolio_category' );
if ( $taxonomy_terms ) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'tax_' . $taxonomy_term->slug;
}
}
}
return $classes;
}
https://developer.wordpress.org/reference/functions/get_the_terms/
get_the_terms() accepst two paramters $post, $taxonomy, you can't just dump multiple taconomies to the function and expect it to work. you need to run get_the_terms() twice.
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$terms = array('arbocategory', 'portfolio_category');
foreach ($terms as $t) {
$taxonomy_terms = get_the_terms($post->ID, $t );
if (is_array($taxonomy_terms) && count($taxonomy_terms)>0) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'tax_' . $taxonomy_term->slug;
}
}
}
}
return $classes;
}
I would like to add a custom content before the WooCommerce shop loop if a specific filter is active.
The example url is this: /shop/?filter_brand=nike
So far I have tried with this:
add_action( 'woocommerce_before_shop_loop', 'amc_add_content_before_loop' );
function amc_add_content_before_loop() {
if ( is_product_category('nike') ) {
echo esc_html__("Custom content", "amc");
}
}
But the content is not shown on the page.
EDIT
With Vincenzo's answer now it works.
I am now trying to use variables and add a shortcode. I tried with this:
add_action( 'woocommerce_before_shop_loop', 'add_custom_content_before_shop_loop2' );
function add_custom_content_before_shop_loop2() {
$terms = get_terms( 'pa_brand' );
foreach ( $terms as $term ) {
if ( is_shop() && isset( $_GET['filter_brand'] ) && $_GET['filter_brand'] == $term->name ) {
echo do_shortcode('[block id="brand-'.$term->name.'"]');
}
}
}
So what it supposed to do is, if a filter $term->name is active, a custom shortcode with the $term->name should be echoed.
But it's not working.
Considering the url you posted: /shop/?filter_brand=nike, you can do it like this:
add_action( 'woocommerce_before_shop_loop', 'add_custom_content_before_shop_loop' );
function add_custom_content_before_shop_loop() {
if ( is_shop() && isset( $_GET['filter_brand'] ) && $_GET['filter_brand'] == 'nike' ) {
// add custom content
}
}
The code has been tested and works. Add it to your active theme's functions.php file.
EDIT
To get the pa_brand attribute name you are using $term->name instead of $term->slug.
$term is an instance of the WP_Term class. The structure of the WP_Term object can be found here.
Try with this:
add_action( 'woocommerce_before_shop_loop', 'add_custom_content_before_shop_loop2' );
function add_custom_content_before_shop_loop2() {
$terms = get_terms( 'pa_brand' );
foreach ( $terms as $term ) {
if ( is_shop() && isset( $_GET['filter_brand'] ) && $_GET['filter_brand'] == $term->slug ) {
echo do_shortcode('[block id="brand-'.$term->slug.'"]');
}
}
}
I have been trying to display the product sub-category name above the product title on the Shop or Product Archive page.
I've tried the code below but I am not sure how to get sub-categories instead of the category:
function category_single_product(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
you can use get_term_children() function
$list_sub_cats = get_term_children($single_cat->term_id,'ca');
I hope this will help you, Visit to get more info https://developer.wordpress.org/reference/functions/get_term_children/
You can use woocommerce_single_product_summary and set priority. Try the below code. Code will go in your active theme functions.php file.
function show_sub_category_before_title(){
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) {
$product_cat = array();
foreach ( $terms as $term ) {
if( $term->parent != 0 ) {
$product_cat[] = $term->name;
}
}
$product_cat = join( ", ", $product_cat );
echo $product_cat;
}
}
add_action( 'woocommerce_single_product_summary', 'show_sub_category_before_title', 1, 1 );
Tested and Works
I've created a do_action('after_product_tabs) hook that I hope to programmatically echo an image under specific product categories, but I'm running into a bug. I'm able to echo out a div as a test for the action hook, but my conditional logic is breaking. Any help would be very much appreciated:
Here's the function:
function after_product_tabs() {
do_action('after_product_tabs'); //creates the action hook
};
function display_prewired_image() {
echo '<div class="test" style="clear: both;">'.$home_url.'</div>'; // This prints!
if( has_term( 'strat_pickups', 'product_cat' ) ) {
echo' <p>DEBUG ME</p>'; // This breaks
}
}
add_action('after_product_tabs', 'display_prewired_image');
I've also Tried:
1.) Passing in Global Variable:
function display_prewired_image() {
global $product;
$prod_id = $product->get_id();
if( has_term( 'strat_pickups', 'product_cat', $prod_id ) ) {
echo' <p>DEBUG ME</p>';
}
}
2.) Changing the conditional logic from product categories to is_single(), which works, so I know my problem is within the logic of strat_pickups:
function display_prewired_image() {
global $product;
if ( is_a( $product, 'WC_Product' ) ) {
// Get product ID
$prod_id = $product->get_id();
if( is_single('354') ) {
echo '<h1 style="red!important; clear: both;">DEBUG ME</h1>';
}
} else {
echo 'No product found!';
}
}
add_action( 'after_product_tabs', 'display_prewired_image' );
For more info, I'm customizing the tabs.php template in Woocommerce. Would that not have access to the $product variable?
Try this instead, global $product is missing
function display_prewired_image() {
global $product;
if ( is_a( $product, 'WC_Product' ) ) {
// Get product ID
$prod_id = $product->get_id();
if ( has_term( 'strat_pickups', 'product_cat', $prod_id ) ) {
echo 'DEBUG ME';
} else {
echo 'Category not found!';
}
} else {
echo 'No product found!';
}
}
//add_action( 'after_product_tabs', 'display_prewired_image' );
add_action( 'woocommerce_before_add_to_cart_form', 'display_prewired_image' );
add_action( 'woocommerce_after_single_product', 'display_prewired_image' );
thank you for your help. This was a bang your head against the wall moment. The product category was not strat_pickups, it was strat-pickups.
I am trying to upgrade my SEO on my webshop. Anyone knows php could fix this puzzle. How can i fit this in each other?
How does this: is_product_category( 'shirts' ) fit in this:
function woa_content_before_shop() {
echo "<p>Insert your Content here...</p>";
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');
WordPress conditional tags like is_product_category() can be used to change the displayed content based on the matched condition.
In this case you can use them to change the printed text based on category. You can exploit them in this way:
function woa_content_before_shop()
{
if ( is_product_category( 'shirts' ) )
{
echo "<p>Here are shirts!</p>";
}
else if ( is_product_category( 'games' ) )
{
echo "<p>Here are games!</p>";
}
else
{
...
}
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');
Use this code. Also set your descriptions in Products > Categories.
function woa_content_before_shop() {
if ( is_product_category() ) {
global $wp_query;
$id = $wp_query->get_queried_object_id();
$desc = term_description( $id, 'product_cat' );
if ( !empty( $desc ) ) {
$output = '<p>' . esc_html( $desc ) . '</p>';
echo $output;
}
}
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');