PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Internal / PushNotifications / Triggers / StockNotificationTrigger.php
StockNotificationTrigger.php
103 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\PendingNotificationStore;
9 use WC_Product;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Listens for WooCommerce stock events and feeds stock notifications into
15 * the PendingNotificationStore.
16 *
17 * @since 10.9.0
18 */
19 class StockNotificationTrigger {
20 /**
21 * Registers WordPress hooks for stock events.
22 *
23 * @return void
24 *
25 * @since 10.9.0
26 */
27 public function register(): void {
28 add_action( 'woocommerce_low_stock', array( $this, 'on_low_stock' ) );
29 add_action( 'woocommerce_no_stock', array( $this, 'on_no_stock' ) );
30 add_action( 'woocommerce_product_on_backorder', array( $this, 'on_backorder' ) );
31 }
32
33 /**
34 * Handles the woocommerce_low_stock hook.
35 *
36 * Captures the product's stock quantity at this moment so the dispatcher,
37 * which runs in a separate process and re-fetches the product, doesn't
38 * read a stale value if cache invalidation hasn't fully propagated.
39 *
40 * @param WC_Product $product The product whose stock is low.
41 * @return void
42 *
43 * @since 10.9.0
44 */
45 public function on_low_stock( WC_Product $product ): void {
46 $stock = $product->get_stock_quantity();
47 $this->add_notification(
48 $product->get_id(),
49 StockNotification::EVENT_LOW_STOCK,
50 null !== $stock ? (int) $stock : null
51 );
52 }
53
54 /**
55 * Handles the woocommerce_no_stock hook.
56 *
57 * @param WC_Product $product The product that is out of stock.
58 * @return void
59 *
60 * @since 10.9.0
61 */
62 public function on_no_stock( WC_Product $product ): void {
63 $this->add_notification( $product->get_id(), StockNotification::EVENT_OUT_OF_STOCK );
64 }
65
66 /**
67 * Handles the woocommerce_product_on_backorder hook.
68 *
69 * @param array $args Backorder event data.
70 * @phpstan-param array{product: WC_Product, order_id: int, quantity: int|float} $args
71 * @return void
72 *
73 * @since 10.9.0
74 */
75 public function on_backorder( array $args ): void {
76 $product = $args['product'] ?? null;
77
78 if ( ! $product instanceof WC_Product ) {
79 return;
80 }
81
82 $this->add_notification( $product->get_id(), StockNotification::EVENT_ON_BACKORDER );
83 }
84
85 /**
86 * Creates a stock notification and adds it to the pending store.
87 *
88 * @param int $product_id The product ID.
89 * @param string $event_type The stock event type.
90 * @param int|null $stock_quantity_at_trigger Stock quantity at the moment WC fired the event, or null when not applicable.
91 * @return void
92 */
93 private function add_notification( int $product_id, string $event_type, ?int $stock_quantity_at_trigger = null ): void {
94 if ( $product_id <= 0 ) {
95 return;
96 }
97
98 wc_get_container()->get( PendingNotificationStore::class )->add(
99 new StockNotification( $product_id, $event_type, $stock_quantity_at_trigger )
100 );
101 }
102 }
103