Automattic\WooCommerce\Internal\PushNotifications\Notifications

StockNotification::to_payloadpublicWC 10.9.0

Returns the WPCOM-ready payload for this notification.

Returns null if the product no longer exists.

Method of the class: StockNotification{}

No Hooks.

Returns

Array|null.

Usage

$StockNotification = new StockNotification();
$StockNotification->to_payload(): ?array;

Changelog

Since 10.9.0 Introduced.

StockNotification::to_payload() code WC 10.9.1

public function to_payload(): ?array {
	$product = WC()->call_function( 'wc_get_product', $this->get_resource_id() );

	if ( ! $product || ! $product instanceof WC_Product ) {
		return null;
	}

	$product_name = wp_strip_all_tags( $product->get_name() );
	$site_title   = wp_strip_all_tags( get_bloginfo( 'name' ) );

	// For variations, `meta.product_id` is the parent product ID so the mobile app
	// can always navigate to the product details screen. `resource_id` keeps the
	// actual entity ID (variation or simple product) for identification and dedup.
	$is_variation = $product->is_type( 'variation' );
	$product_id   = $is_variation ? $product->get_parent_id() : $product->get_id();

	return array(
		'type'        => $this->get_type(),
		'timestamp'   => gmdate( 'c' ),
		'resource_id' => $this->get_resource_id(),
		'title'       => $this->build_title( $product_name ),
		'message'     => $this->build_message( $product_name, $site_title, $product ),
		'meta'        => array(
			'product_id' => $product_id,
			'event_type' => $this->event_type,
		),
	);
}