Woocommerce category description using shortcode - php

I want to replace the Woocommerce category description on pages using a shortcode.
With the shortcode I want to dynamically display custom featured image and description for current category.
Here is some code I found from searching here and Google. This code seemed to work but also affected single product pages.
Can someone tell me what is wrong with this please ?
add_filter('woocommerce_short_description', function ($description) {
if (! is_product_category()) { return; }
return do_shortcode('[porto_block id="510079"]');
});

Use the following conditions to make sure it only works on category pages.
add_filter('woocommerce_short_description', function ($description) {
if ( is_product_category() && !is_single() && !is_product()) {
return do_shortcode('[porto_block id="510079"]');
}else{
return $description;
}
});

Related

Parent product category page in Woocommerce

Is it possible in Woocommerce, if the product category page shows its subcategories, to do any actions? I need to add a class to the body tag on such pages. Is it real? Example:
function my_custom_body_class_woo_cat($classes) {
if ( is_product_category() && is_subcategory_show() ) {
$classes[] = 'my-class';
return $classes;
}
}
add_filter('body_class','my_custom_body_class_woo_cat');`
I tried to look for solutions, but I didn't find one
I checked woocommerce conditional tags and is_subcategory_show() is not a valid woocommerce function.
So in order for this to work we need to do our own checks before assigning the extra class or classes to the body.
Here is a code snippet that does what you asked( if i understood your question correctly ):
add_filter('body_class', function ($classList) {
// Not a product category, return the classList intact
if (!is_product_category()) {
return $classList;
}
$desiredDisplayType = 'subcategories';
$term = get_queried_object();
// Not a parent category, return classList intact
if ($term->parent !== 0) {
return $classList;
}
// Get the woocommerce category archive display option (the one you set up in the customizer)
$categoryArchiveDisplayOption = get_option('woocommerce_category_archive_display');
// Get the display type for the current category
$currentCategoryDisplayOption = get_term_meta($term->term_id, 'display_type', true);
// The current category displatype isn't the desired one, return classList intact
if ($currentCategoryDisplayOption !== $desiredDisplayType) {
return $classList;
}
// Neither of the option are the desired one, return classList intact
if (
$currentCategoryDisplayOption !== $desiredDisplayType
&& $categoryArchiveDisplayOption !== $desiredDisplayType
) {
return $classList;
}
// All checks passed, add my class to body
return array_merge($classList, ['my-class']);
});
Woocommerce has a general option in the customizer where you set up if you want category archives to display subcategories.
Then when you edit a specific category you can assign a display type specific to that category, so this should take precedence over the general option.

How to echo a shortcode in a specific product category

I have this php code and it displays a button in the single product page. Everything works fine but the button generated from the shortcode is displayed in EVERY product category.
I want to display the button from shortcode ONLY in a specific product category.
<?php
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote()
{
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
?>
I tried this but the site crashed
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote()
if ( in_category( $cat 'category-slug' ) ) {
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
Let's see if I can help you!
First of all you're missing two curly brackets in your function and that is why your site crashed. You need to wrap your function with the curly brackets to avoid this.
What I think you want to do is to only execute the shortcode if it's a certain product category archive page. You can do this by using if( is_product_category() ) You need to define the category either by slug, id or title.
Try this function and change "example" to the title of your category. Let me know how it works out!
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote() {
if( is_product_category('example') ) { //change "example" to category title, id or slug
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
}

How to replace Wordpress default search with Woocommerce search

I need help replacing the default Wordpress search with Woocommerce search.
Tried this in functions.php
add_filter( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts( $query ) {
if ( is_search() ) {
$query->set('post_type', 'product');
}
return $query;
}
but the output of the search is still just a list of post-like search results. I would like the same output (product archive'ish) as the Woocommerce search outputs.
Is that possible?
Found a plugin. If anyone else wants a solutions without coding: https://wordpress.org/plugins/woocommerce-menu-extension/

WooCommerce how to check if page is_shop() in functions.php?

In WooCommerce, My Category Listing page and product listing page are rendered from archieve-product.php ( By Default) . How to check if page is_shop() in functions.php? As is_shop function does not work in functions.php. I simply want to remove my sidebar from Category listing page not from product listing page.
When placed inside a hook, is_shop will work in functions.php
add_action( 'template_redirect', 'custom_template_redirect' );
function custom_template_redirect() {
if( is_shop() ) :
// code logic here
endif;
}
Here is a list of all WooCommerce conditionals
You can write a condition into "archive-product.php" for category page like,
$cate = get_queried_object();
if(is_product_category() && $cate->parent != 0 ){
// Write code here
//include sidebar here
}
By using this code this will check the page for product_category and also check for a parent.
call it using WordPress Hook pre get posts
add_action('pre_get_posts','nameTheFunction');
function nameTheFunction(){
if(is_shop()){
// your code here
}
}// function end here
Read more about pre get posts Hook
https://developer.wordpress.org/reference/hooks/pre_get_posts/
You can use function_exists
if( function_exists("is_shop") ) {
// call it or do something else
}
else {
// load it from somewhere
}
Official docs: https://secure.php.net/function_exists

How to exclude a post by id from the category.php query in wordpress

I have a template where there is a main latest featured post (tagged as featured), and then 8 more below it.
On the homepage, i was able to query for the latest featured post, and then pass its id to a function in the pre_get_posts wordpress filter. it works great there
function mh_exclude_featured_query( $query ) {
if(is_home()){
if ( $query->is_home() ) {
$feat = get_featured_post();
$query->query_vars['post__not_in'] = array($feat->ID);
}
}
}
add_action( 'pre_get_posts', 'mh_exclude_featured_query' );
but i'm also trying to do the same thing in the category.php, where i would show the latest post tagged as featured from that category. and then the remaining posts below with the featured post excluded.
Unfortnately, when i try the same method as above by using the pre_get_posts filter, i get stuck in an infinite loop and run out of memory.
if($query->is_category() && $query->is_main_query()){
$cur_cat_id = get_cat_id( single_cat_title("",false) );
$feat = get_featured_post($cur_cat_id);
$query->query_vars['post__not_in'] = array($feat->ID);
}
not sure what i'm doing differently that leads to a memory exhaustion. the category.php and index.php are near identical in their structure.
use the pre_get_posts filter:
<?php
function excludePostId($query) {
$postIds = array(
24, 10
);
if (is_category() && is_main_query()) {
set_query_var('post__not_in', $postIds);
}
}
add_action('pre_get_posts', 'excludePostId');
is_category() will accept a category slug or ID. You can limit which categories the post will be excluded from.
Add This Code
<?php if(!is_category('category_id')){
echo 'Your Code Here';
}?>

Categories