List recent back in stock products
-
Is it possible to embed a list of recently back in stock products to the front-end of a website.
I know this plugins aims to be a notification solution, but perhaps you store information on the products like for example the timestamp, when a product comes back in stock, that could be used to generate such a list of recently back in stock products?
-
Hello there,
Good Day!!
We’re here to assist you.
We’re sorry to inform you that your requested feature is unavailable on our end. However, we noted your request and will try to implement this down the road based on its feasibility.
Thanks.
Okay I see thank you
And is there a hook provided by your plugin, which fires an action, when the product changes to back in stock?
Then we should be able to add an action, which saves a timestamp to the product meta, and we would be able to select the products with a meta query on WordPress.
Is there such an action hook?
Hello there,
Good Day!!
We suggest you check for the hook in the file class-core.php where you may choose the hook and use it based on your preference.
Thanks.
Thanks, hooking into
cwginstock_trigger_status
and saving the back in stock timestamp to product meta to be able to select products that are in stock and got in stock in the last X days works fine.For everybody interested, here is a base version for a plugin doing the job:
<?php /** * Plugin Name: WooCommerce back in stock since * Requires PHP: 8.0 * Version: 1.0.0 */ class PhpBotWcBackInStockSince { private $metaKey = '_php_bot_wc_back_in_stock_since'; /** * constructor */ public function __construct() { add_action( hook_name: 'cwginstock_trigger_status', callback: [$this, 'saveSince'], accepted_args: 3, ); add_shortcode( tag: 'php_bot_wc_back_in_stock_since', callback: [$this, 'shortcode'], ); } function saveSince($id, $stockstatus, $obj) { update_post_meta($id, $this->metaKey, current_time('mysql')); } /** * [php_bot_wc_back_in_stock_since days="7"] */ function shortcode(array $atts): string { $atts = shortcode_atts( array( 'days' => 7, ), $atts, 'php_bot_wc_back_in_stock_since' ); $objs = $this->getProductsInStockSince($atts['days']); if (empty($objs)) { return ''; } $ids = array_map( function ($obj) { if ($obj->post_type === 'product_variation') { return $obj->post_parent; } return $obj->ID; }, $objs ); return do_shortcode( sprintf( '[products ids="%s"]', implode(',', $ids) ) ); } private function getProductsInStockSince(int $days): array { if ($days < 1) { return []; } $args = [ 'post_type' => ['product', 'product_variation'], 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => [ [ 'key' => '_stock', 'value' => 0, 'compare' => '>', ], [ 'key' => $this->metaKey, 'value' => date('Y-m-d H:i:s', strtotime('-' . $days . ' days')), 'compare' => '>=', ], ], ]; $query = new WP_Query($args); return $query->posts; } } new PhpBotWcBackInStockSince();
Hi, I have the same question. I would be very interested to have this feature, in order to :
– display recent back in stock products in frontend
– put a label on products showing “just back in stock” based on the timestamp that would be stored in product meta (for example for the products that are back in stock since the last 14 days.
I have the pro version of Back In Stock Notifier for WooCommerce, so it would be perfect if one day you add this new feature.
@phpbot thanks for you base version plugin. If I understand, you store the time stamp in product meta. And then a shortcode allows to displays de list of products ? However this code doesn’t work for me at the moment. Is it working for you ?
Thank you for your answer@corentinco I can confirm the code is working for me.
Note that you have to be running on a minimum PHP Version of 8.0.0 to get things running smoothly (as noted in the plugin header comment).
If you are on PHP 8 and are facing an error message, feel free to post it here, so we can take a look into it.
Hi @phpbot ! Thank you very much for your answer. I tried again and indeed it was a misconfiguration I did that caused the problem.
It is working fine now, thank you.
I am now trying to display the products in order to show first the more recent back in stok products (for the moment is it in alphabetical order). However, I am a begginer and I don’t know how to write it in the code.
I am also trying to add a parameter in the shortcode for the limit of numbers of products displayed. I don’t know how to do it in the shortcode, but I managed to do it directly in the code by changing ‘posts_per_page’ => -1 to 8.
What I am trying to do it to use a shortcode like this : [php_bot_wc_back_in_stock_since days=”14″ limit=”8″ orderby=”datebackstock” order=”DESC”]
In any case, thank you for your help for this plugin
You can add sorting by the custom meta field (orderby), to get latest/oldest products. Here is how to implement in the Query: https://stackoverflow.com/a/50937314/13765033
The class method, which returns the shortcode contents itself uses the WooCommerce built in
[products]
shortcode, which supports a variety of attributes, you can go ahead and extend the method to dynamically set these attributes to the finaldo_shortcode()
call. This way, you will be able to use all WooCommerce built-in attributes on the custom shortcode.Thank you @phpbot I worked on this.
I added in the $args'date_query' => ['before' => '- 7 days',]
in order not to display products that were recently created on the website (because it is not back in stock products, it is recent products).However, I am unsuccessful on orderby meta date… in order to show first the more recent back in stock products. I added
'meta_key' => '_php_bot_wc_back_in_stock_since', 'orderby' => array('meta_type'=> 'DATE'), 'order' => 'DESC',
But it is not working, it is still showing the products by alphabetical order. I tried several combinations of orderby code (DATE, DATETIME) without success, I don’t know why.
Thank you for your precious help so far, I am learning thanks tou you.
Here is a useful answer about sorting based on custom metadata: https://wordpress.stackexchange.com/a/109853/218274
For full details, check out: https://developer.www.ads-software.com/reference/classes/wp_query/#order-orderby-parameters
I guess you are looking for:
'orderby' => 'meta_value'
-
This reply was modified 2 years, 11 months ago by
phpbot.
Thank you, I tried, but I don’t manage to make this orderby work.
Never mind, thank your for having helped me.
Have a nice day -
This reply was modified 2 years, 11 months ago by
- The topic ‘List recent back in stock products’ is closed to new replies.