NewOrderNotificationTrigger.php
3 months ago
NewReviewNotificationTrigger.php
3 months ago
StockNotificationRecoveryHandler.php
4 weeks ago
StockNotificationTrigger.php
4 weeks ago
StockNotificationRecoveryHandler.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications\Triggers; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\PushNotifications\Notifications\StockNotification; |
| 8 | use Automattic\WooCommerce\Internal\PushNotifications\Services\NotificationProcessor; |
| 9 | use WC_Product; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Clears the per-event sent-meta on a product when its stock recovers |
| 15 | * above the threshold that would have re-triggered the notification. |
| 16 | * |
| 17 | * The dedup meta written by {@see NotificationProcessor} is namespaced |
| 18 | * per event subtype (e.g. `_wc_push_notification_sent_low_stock`). Without |
| 19 | * recovery, a product only ever emits one push per subtype for its entire |
| 20 | * lifetime — every subsequent low → restocked → low cycle is silently |
| 21 | * suppressed. This handler clears each meta the moment stock crosses back |
| 22 | * above the relevant threshold, so the next downward crossing emits a |
| 23 | * fresh push. |
| 24 | * |
| 25 | * @since 10.9.0 |
| 26 | */ |
| 27 | class StockNotificationRecoveryHandler { |
| 28 | /** |
| 29 | * Registers the recovery hook. |
| 30 | * |
| 31 | * Hooks both `woocommerce_product_set_stock` and |
| 32 | * `woocommerce_variation_set_stock` because variations dispatch a |
| 33 | * separate action (see `wc-stock-functions.php`). The trigger side |
| 34 | * fires for variations too, so without the variation hook a variable |
| 35 | * product's sent-meta would never clear. |
| 36 | * |
| 37 | * @return void |
| 38 | * |
| 39 | * @since 10.9.0 |
| 40 | */ |
| 41 | public function register(): void { |
| 42 | add_action( 'woocommerce_product_set_stock', array( $this, 'on_stock_change' ) ); |
| 43 | add_action( 'woocommerce_variation_set_stock', array( $this, 'on_stock_change' ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Evaluates each event subtype's recovery threshold and clears |
| 48 | * the corresponding sent-meta when the new stock level has recovered. |
| 49 | * |
| 50 | * @param WC_Product $product The product whose stock changed. |
| 51 | * @return void |
| 52 | * |
| 53 | * @since 10.9.0 |
| 54 | */ |
| 55 | public function on_stock_change( WC_Product $product ): void { |
| 56 | $product_id = $product->get_id(); |
| 57 | |
| 58 | if ( $product_id <= 0 ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $stock_quantity = $product->get_stock_quantity(); |
| 63 | |
| 64 | if ( null === $stock_quantity ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $stock = (int) $stock_quantity; |
| 69 | |
| 70 | if ( $stock > (int) wc_get_low_stock_amount( $product ) ) { |
| 71 | $this->clear_meta( $product_id, StockNotification::EVENT_LOW_STOCK ); |
| 72 | } |
| 73 | |
| 74 | if ( $stock > (int) get_option( 'woocommerce_notify_no_stock_amount', 0 ) ) { |
| 75 | $this->clear_meta( $product_id, StockNotification::EVENT_OUT_OF_STOCK ); |
| 76 | } |
| 77 | |
| 78 | if ( $stock >= 0 ) { |
| 79 | $this->clear_meta( $product_id, StockNotification::EVENT_ON_BACKORDER ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Clears the namespaced sent-meta for a given product and event subtype. |
| 85 | * |
| 86 | * @param int $product_id The product ID. |
| 87 | * @param string $event_type One of the StockNotification::EVENT_* constants. |
| 88 | * @return void |
| 89 | */ |
| 90 | private function clear_meta( int $product_id, string $event_type ): void { |
| 91 | ( new StockNotification( $product_id, $event_type ) )->delete_meta( NotificationProcessor::SENT_META_KEY ); |
| 92 | } |
| 93 | } |
| 94 |