Adding css class to featured products in woocommerce - php

I would like to loop throught all products in shop/category page and add special css class to product if it is featured. I have tried to do this with add_filter hook but it seems I am to dumb to get it working.

You were right about using a filter. I don't know which filter you were trying but you would edit the post_class and add a class if the post type is a product and has the specific category you want.
add_filter( 'post_class', 'namespace_featured_posts' );
function namespace_featured_posts( $classes ) {
if( 'product' == get_post_type() && has_term( 'featured', 'product_cat') ) {
$classes[] = 'featured-post';
return $classes;
}
}

Related

Display WooCommerce Product Dimensions via a Shortcode

I am creating a custom layout (using Elementor) for a product - what I need is to have the dimensions of a product to display in a custom tab I have created.
Is there a shortcode for product dimensions? (some of the products are variable too if that makes a difference)
I have managed to get some code to display the long description in a shortcode (see below) however I'm not advanced enough to know what to do to get it to display product dimensions (length, width, height - ideally each one on a separate line)
Here is the long description code i found that works...
function custom_product_description($atts){
global $product;
try {
if( is_a($product, 'WC_Product') ) {
return wc_format_content( $product->get_description("shortcode") );
}
return "Product description shortcode run outside of product context";
} catch (Exception $e) {
return "Product description shortcode encountered an exception";
}
}
add_shortcode( 'custom_product_description', 'custom_product_description' );
Does anyone can give me some help to display product dimensions via a shortcode?
You can use the following simple code snippet, to display product dimensions via a shortcode:
add_shortcode( 'product_dimensions', 'display_product_dimensions' );
function display_product_dimensions( $atts ){
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => '',
), $atts, 'product_dimensions' ) );
if( $id == '' ) {
global $product;
if( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( get_the_id() );
}
}
return method_exists( $product, 'get_dimensions' ) ? wc_format_dimensions($product->get_dimensions(false)) : '';
}
Code goes in functions.php file of the active child theme (or active theme). it should works.
USAGE:
On WooCommerce product single page: [product_dimensions]
Anywhere with a defined Id (for example product Id 35): [product_dimensions id="35"]

How to remove classes added to Woocommerce li.product?

Woocommerce adds the assigned product categories and attributes as custom classes to li.product
<li class="post-120 product type-product status-publish has-post-thumbnail
category1 category2 category3 category4 pa_one pa_two...">
We have assigned quite a lot of categories to each product and it slows down the site. Is there a way to remove those additional classes?
In the woocommerce plugin '3.6.2' the file 'wc-template-functions.php' it says:
// Note, to change classes you will need to use the newer woocommerce_post_class filter.
So to remove 'first' & 'last' classes I used:
add_filter( 'woocommerce_post_class', 'remove_post_class', 21, 3 ); //woocommerce use priority 20, so if you want to do something after they finish be more lazy
function remove_post_class( $classes ) {
if ( 'product' == get_post_type() ) {
$classes = array_diff( $classes, array( 'last','first' ) );
}
return $classes;
}
For product categories I used:
add_filter( 'product_cat_class', 'remove_category_class', 21, 3 ); //woocommerce use priority 20, so if you want to do something after they finish be more lazy
function remove_category_class( $classes ) {
if ( 'product' == get_post_type() ) {
$classes = array_diff( $classes, array( 'last','first' ) );
}
return $classes;
}
If you know php, You can edit the woocoommerce php template file to remove those additional classes.
If you opened the file, there is a php variable to be added additional classes and it is a array.
I would suggest applying a filter to the post_class function instead of editing the template as woocommerce updates these regularly and can be a pain to keep on top of them. To do this you could check to see if you in the product loop or if a product exists to also remove from the product page like this :
add_filter( 'post_class', function($classes){
$product = wc_get_product();
return ($product) ? array('product' 'or-any-class-you-want') : $classes; } , 9999 );

WooCommerce: Display product variations inside of loop

How can I display the product variations for each product within a loop such as the one on the Shop page? Is there any function that I can add to the content-product template that retrieves the list of variations and displays them?
You can use this code to add product variation to the shop/product category loops
// Load our function when hook is set
add_action( 'pre_get_posts', 'custom_modify_query_get_posts_by_date' );
// Modify the current query
function custom_modify_query_get_posts_by_date( $query ) {
// Check if on frontend and main query is modified and if its shop or product category loop
if( (! is_admin() && $query->is_main_query()) && ( is_shop() || is_product_category() ) ) {
$query->set( 'order', 'ASC' );
add_filter( 'posts_where', 'rc_filter_where' );
}
return $query;
}
// Add products variation post type to the loop
function rc_filter_where( $where = '' ) {
$type = 'product_variation';
$where .= " OR post_type = '$type'";
return $where;
}
Best way to do this is alter the loop. Something like that will help:
function filter_wc_query($query_args){
if(is_archive()){ //here you can use any conditional function to show variations on
$query_args[] = ['post_parent'=> '*'];
}
return $query_args;
}
add_filter('woocommerce_get_query_vars','filter_wc_query');
Or if you want to this on view level:
https://gist.github.com/lukecav/2470d9fe6337e13da4faae2f6d5fe15f
In this solution image you can grab by standard WP function:
get_post_thumbnail($product->ID,'shop_list');
But remeber that variation link will bring user to parent product and force him to select variation once more.

How to add post tags to WooCommerce products?

I noticed that post tags are different than WooCommerce product tags.
I need to add post tags to WooCommerce products so i can include some WooCommerce products in the post tag archives.
Is this possible?
I've tried these code snippets but it doesn't add them.
add_filter( 'pre_get_posts', 'add_custom_types' );
function add_custom_types( $query ) {
if ( is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array( 'post', 'products', 'product' ) );
return $query;
}
}
add_filter('request', 'post_type_tags_fix');
function post_type_tags_fix($request) {
if ( isset($request['tag']) && !isset($request['post_type']) )
$request['post_type'] = array( 'products', 'product' );
return $request;
}
I needed to do this as well and have come up with what I think is a good way of doing this. A few steps:
Firstly, you need to add the post_tag taxonomy to the woocommerce product post type. You can do this easily with the following filter:
function custom_wc_add_post_tags( $args ){
$args['taxonomies'] = array('post_tag');
return $args;
}
add_filter( 'woocommerce_register_post_type_product', 'custom_wc_add_post_tags' );
This will add a new 'Tags' item to your admin menu, and allow you to tag products with regular post tags rather that the woocommerce-specific 'product_tag' taxonomy. Secondly, you'll likely want to remove the 'product_tag' taxonomy if you don't plan on using it? Because the above will result in two admin menu items called 'tags' which will get confusing. The following will do that for you:
add_filter('woocommerce_taxonomy_objects_product_tag', '__return_empty_array');
add_filter('woocommerce_taxonomy_args_product_tag', '__return_empty_array');
And to remove the column from the 'Products' table on the backend:
function custom_wc_remove_product_tags_column($columns){
unset( $columns['product_tag'] );
return $columns;
}
add_filter( 'manage_edit-product_columns', 'custom_wc_remove_product_tags_column', 15 );
That will actually add the tags to your products. If you then need these to show up in your archive page, you may still need to modify that page's query to look for posts of type 'product' as well as the standard 'post'. It looks like your function above on the 'pre_get_posts' hook will do that part for you. Hope this helps!
Use below code to get archive page in product tag page. now product tag page use theme archive.php file.
Add Below code in theme functions.php
add_filter( 'template_include', 'woocommerce_product_tag_page_template', 99 );
if ( ! function_exists( 'hcode_post_format_parameter' ) ) {
function woocommerce_product_tag_page_template( $template ) {
if ( is_tax( 'product_tag' ) ) {
get_template_part('archive');
}
return $template;
}
}

Adding SKU as body class in Woocmmerce for Wordpress

I want to be able to target certain product-pages with CSS based on the SKU of the product they have on. Therefore I need to add the SKU as a body class for all Woocommerce products. How would I accomplish this?
//Add Woocommerce body classes
add_filter('body_class','sku_woocommerce_body_classes');
function sku_woocommerce_body_classes($classes){
global $post;
$product = get_product( $post->ID );
if ( $product->product_type == 'simple');
$classes[] = $product->get_sku();
return $classes;
}
You weren't clear on what type of pages to target, but the if statement will get you where you need to go in order to target them.

Categories