Home › Forums › WoodMart support forum › New Label automation
New Label automation
- This topic has 11 replies, 5 voices, and was last updated 4 years ago by Elise Noromit.
-
AuthorPosts
-
June 25, 2020 at 6:06 pm #206494
prokreegerParticipantHello,
I want to automate adding of new label to the product. The label must be visible for 30 days from date the product was created. I have found this code, but I don’t know where it must be added to make it visible on products, categories and widgets like bestsellers and so on.
add_action( 'woocommerce_before_single_product_summary', 'new_badge_shop_page', 10 ); function new_badge_shop_page() { global $product; $newness_days = 30; $created = strtotime( $product->get_date_created() ); if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) { echo '<span class="new product-label">' . esc_html__( 'New!', 'woocommerce' ) . '</span>'; } }
June 26, 2020 at 6:22 am #206562
Elise NoromitMemberHello,
You can add this code to the functions.php of the child theme. We do not guarantee a smooth and correct performance.
Best Regards
June 26, 2020 at 10:51 am #206646
prokreegerParticipantHello,
I added code to function.php and the label appeared on the product page, but not in the place where it should be. It inserted under your code, and when I create a sale, my new label is placed on top of the sale label instead of moving down.
How can I add my code to the right place (see attached picture).
Attachments:
You must be logged in to view attached files.June 26, 2020 at 11:33 am #206666
Eric WatsonParticipantWe added your code to our function of labels, try deleting your code and adding ours instead.
function woodmart_product_label() { global $product; $output = array(); $product_attributes = woodmart_get_product_attributes_label(); $percentage_label = woodmart_get_opt( 'percentage_label' ); if ( $product->is_on_sale() ) { $percentage = ''; if ( $product->get_type() == 'variable' && $percentage_label ) { $available_variations = $product->get_variation_prices(); $max_percentage = 0; foreach( $available_variations['regular_price'] as $key => $regular_price ) { $sale_price = $available_variations['sale_price'][$key]; if ( $sale_price < $regular_price ) { $percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 ); if ( $percentage > $max_percentage ) { $max_percentage = $percentage; } } } $percentage = $max_percentage; } elseif ( ( $product->get_type() == 'simple' || $product->get_type() == 'external' ) && $percentage_label ) { $percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 ); } if ( $percentage ) { $output[] = '<span class="onsale product-label">-' . $percentage . '%' . '</span>'; }else{ $output[] = '<span class="onsale product-label">' . esc_html__( 'Sale', 'woodmart' ) . '</span>'; } } if( !$product->is_in_stock() ){ $output[] = '<span class="out-of-stock product-label">' . esc_html__( 'Sold out', 'woodmart' ) . '</span>'; } if ( $product->is_featured() && woodmart_get_opt( 'hot_label' ) ) { $output[] = '<span class="featured product-label">' . esc_html__( 'Hot', 'woodmart' ) . '</span>'; } $newness_days = 30; $created = strtotime( $product->get_date_created() ); if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) { $output[] = '<span class="new product-label">' . esc_html__( 'New!', 'woocommerce' ) . '</span>'; } if ( $product_attributes ) { foreach ( $product_attributes as $attribute ) { $output[] = $attribute; } } if ( $output ) { echo '<div class="product-labels labels-' . woodmart_get_opt( 'label_shape' ) . '">' . implode( '', $output ) . '</div>'; } }
June 26, 2020 at 11:57 am #206679
prokreegerParticipantThanks a lot))) I am very appreciated for your work. Labels work great in all places.
P.S.: It would be fine if you added this functional in your theme.
June 26, 2020 at 12:00 pm #206681
Eric WatsonParticipantYou are welcome!
Yes the feature is not bad, we are already discussing the possibility of introducing it in the future, thank you.
Best Regards
August 7, 2020 at 7:35 am #217038
JoieParticipantHello, I hope the theme can implement this with the option to allow us to define the newness.
Thank you for your code !
August 7, 2020 at 9:59 am #217066
Elise NoromitMemberHello,
We shall consider adding this feature but do not promise to implement that in the nearest features.
Best Regards
November 15, 2020 at 9:07 am #241667
iamdeveloperParticipantThumb up! Thanks for the request from prokreeger and the code from eric. Please consider add this as a feature of the theme.
I’m not using child theme but with code snippets. The code shows error
The snippet has been deactivated due to an error on line 2:
Cannot redeclare function woodmart_product_label.Can you help?
November 16, 2020 at 8:16 am #241847
Elise NoromitMemberHello,
This feature has been added to the recent version of the theme: https://prnt.sc/vjvsls
Please make sure you run 5.3.3 version. If you do not, please make the full backup of your site and update it.
Best Regards
November 16, 2020 at 8:36 am #241867
iamdeveloperParticipantHi Elise,
Thanks. I aware the product extra opions now in product page too.
I’m looking for the label automation automate adding of new label to the product. The label must be visible for 30 days from date the product was created.
November 16, 2020 at 1:31 pm #242012
Elise NoromitMemberHello,
For now, there is no option to add the label automatically, however, we shall consider adding the feature in the future. We do not promise to implement it in the nearest updates.
Best Regards
-
AuthorPosts
The topic ‘New Label automation’ is closed to new replies.
- You must be logged in to create new topics. Login / Register