Do you want to display a ‘New Arrival’ badge on the new products of your WooCommerce store and provide a BOGO offer to those products only? This code ensures that the “New Arrival” label is displayed only for products published within the last 5 days, and when these products are added to the cart, automatically the free product also gets added.
// Display the "New Arrival" tag on the shop page for products published in the last 5 days add_action('woocommerce_before_shop_loop_item_title', 'ts_shop_page_new_tag', 10); function ts_shop_page_new_tag() { global $product; // Get the current date $current_date = current_time('mysql'); // Calculate the date from 5 days ago $five_days_ago = date('Y-m-d H:i:s', strtotime('-5 days', strtotime($current_date))); // Check if the product was published in the last 5 days if (strtotime(get_the_date('Y-m-d', $product->get_id())) >= strtotime($five_days_ago)) { echo '<span class="limited"><strong>' . __( 'New Arrival', 'woocommerce' ) . '</strong></span>'; // Add the product with ID 470 as FREE to the cart $free_product_id = 470; // Check if the free product is not already in the cart if (!WC()->cart->find_product_in_cart($free_product_id)) { // Add the free product to the cart with a quantity of 1 WC()->cart->add_to_cart($free_product_id, 1); } } }
Output
With the published date you can see the new arrivals of the products in the backend of your WooCommerce Store.
The below image shows the products on the shop page that have the ‘New Arrival’ badge since these products are newly arrived products in the shop.
So, when these specific ‘New Arrival’ badge products are added to the cart, the corresponding free product specified in the code gets automatically added to the cart.
Alternatively, you can also apply buy one get one offer based on product tags in WooCommerce cart.