Display New Badge Woocommerce If In New Category - php

I need to display a 'new' badge to product on the archive page but only for products in the 'New' category
add_action( 'woocommerce_after_shop_loop_item_title', 'lottie_new_badge', 40 );
function lottie_new_badge($badge) {
global $product;
if (has_term ('new')) {
echo '<p>New</p>';
}
return $badge;
}
Got this code but not working, mixed few bits of code together to try get it to work but no luck.

There is missing arguments when using has_term() function with Woocommerce product category. Also as you are using an action hook, $badge is not needed and not has to be returned like in a filter hook. So try the following instead:
add_action( 'woocommerce_after_shop_loop_item_title', 'display_lottie_new_badge', 40 );
function display_lottie_new_badge() {
global $product;
if ( has_term ( array('new'), 'product_cat', $product->get_id() ) ) {
echo '<p class="new">New</p>';
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.

Related

How to target a certain product in woocomerce if it is a Variation

I'm trying to figure out how to target a specific variable product in woocommerce to show this message underneath the "Add to cart" button. The current code is showing for all variable product type.
`
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message' , 10, 0 );
function custom_product_message() {
global $product;
if( ! $product->is_type( 'variable' )) return; // Only for variable products
echo '<div class="woo_variable_custom_message">Hello</div>';
}
`
This is the snippet I am working on.`
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message' , 10, 0 );
function custom_product_message() {
global $product;
if( ! $product->is_type( 'variable' )) return; // Only for variable products
echo '<div class="woo_variable_custom_message">Hello</div>';
}
`
Global and dynamic solution is create a custom field in product and you can custom text field.
You can create custom field by custom code or you can use ACF plugin to create custom field in the product.
Custom code to create custom field:
https://www.cloudways.com/blog/add-custom-product-fields-woocommerce/
ACF plugin:
https://wordpress.org/plugins/advanced-custom-fields/
Then using below action hook you can make the process dynamic from admin itself using one time code.
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message', 20 );
function custom_product_message() {
global $product;
$getcustomText = get_post_meta(post_id, 'meta_key', true);
if ( !empty ($getcustomText)) {
echo '<div class="woo_variable_custom_message">Hello</div>';
}
}
Quick Note:
I know process is little big, but it will never give you trouble some or make again and again coding to add variable product id or other parameter in the theme functions file
Thanks
Found out this can target Variable or simple product type using this way.
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message', 20 );
function custom_product_message() {
if ( is_single( '8941' )) {
echo '<div class="woo_variable_custom_message">Hello</div>';
}
}

Woocommerce - Display the product category under product title in shop/archive page

I want to add the product's category between the title and the price of each product. As long as I edited the archive-product.php file with codes i found here, nothing seemed to work. I created a copy of the file on the active theme too.
we can use 'woocommerce_after_shop_loop_item_title' action for this case
function product_category_in_shop_loop() {
global $product;
$product_id = $product->get_id();
$cat = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'names'));
if( !empty($cat[0]) ){
echo '<p class="catil">'.$cat[0].'</p>';
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'product_category_in_shop_loop', 40 );
code goes to child theme function.php.

Remove review stars if no reviews in WooCommerce shop and archives

I want to not display the stars on products in the Shop page loop. My current code doesn't work...
I'm working on a WooCommerce website with Astra.
function no_stars_if_no_reviews() {
global $product;
if ($average = $product->get_average_rating()) :
if($average > 0) {
remove_filter(get_average_rating);
}
}
I just want to remove the filter when no reviews are there, but keep it otherwise. Once again, this is for the Products page, NOT single.
Simply use the following, to remove rating stars from shop and archive pages when no reviews:
add_action( 'woocommerce_after_shop_loop_item_title', 'conditionally_remove_loop_rating', 4 );
function conditionally_remove_loop_rating(){
global $product;
if( ! ( $product->get_review_count() > 0 ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Get the Page ID when the displayed product as has a different ID in Woocommerce

I am trying to run a function specific for one product that has a different ID than the page where is displayed. To this I run a function from my child theme functions.php.
I have tried page_id, post id, is_page but I do not get it to work.
My page-id is 8405, product id is 837.
My latest code try is:
// Remove price variation on single product page
add_action( 'woocommerce_before_single_product', 'my_remove_variation_price' );
function my_remove_variation_price() {
if ( is_page('8405') ) {
global $product;
if ( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price' );
}}
}
How can I get the Page ID which is different frm the displayed product ID?
The following code will do the job and it will work specifically for your page ID:
// Remove price variation on single product page
add_action( 'woocommerce_single_product_summary', 'remove_variable_product_price', 1 );
function remove_variable_product_price() {
global $wp, $product;
$page = get_page_by_path($wp->request);
if ( $product->is_type( 'variable' ) && $page->ID == 8405 ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

Add custom content Next add-to-cart In button for a specific Product

I am trying to add content next to add to cart button in woocommerce for specific product, I have used the bellow woocommerce hook, But unfortunately it didn't work:
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
function add_content_after_addtocart_button_func() {
$current_product = $product->id;
if($current_product === '13333') {
echo '<div class="second_content">Place your content here!</div>';
}
}
Any one can help me on this?
Thanks
Update: Added compatibility with WC +3
This custom function hooked in woocommerce_single_product_summary action hook, will display a custom content after add-to-cart button for a specific product ID in the single product page:
add_action( 'woocommerce_single_product_summary', 'add_content_after_addtocart_button_func', 35 );
function add_content_after_addtocart_button_func() {
global $product;
// Added compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if($product_id == 13333)
echo '<div class="custom-content">'.__('Place your content here!','woocommerce').'</div>';
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Categories