Channel_Sync::post_updated( int $post_id, WP_Post $post )

Summary

Listen for updates to product posts to trigger an update to the BigCommerce channel listing


Parameters

$post_id

(int) (Required)

$post

(WP_Post) (Required)


Return

(void)


Source

File: src/BigCommerce/Post_Types/Product/Channel_Sync.php

	public function post_updated( $post_id, $post ) {
		if ( Product::NAME !== $post->post_type ) {
			return;
		}

		/**
		 * Filter whether updates to the post should be pushed up to the
		 * BigCommerce channel listing.
		 *
		 * @param bool $update  Whether the update should be pushed
		 * @param int  $post_id The ID of the post being updated
		 */
		if ( ! apply_filters( 'bigcommerce/channel/listing/should_update', true, $post_id ) ) {
			return;
		}

		$product = new Product( $post_id );
		$channel_id = $product->get_channel_id();
		$listing_id = $product->get_listing_id();
		if ( empty( $channel_id ) || empty( $listing_id ) ) {
			return;
		}

		try {
			$response = $this->channels->getChannelListing( $channel_id, $listing_id );
			$listing  = $response->getData();
		} catch ( ApiException $e ) {
			/**
			 * Error triggered when fetching a listing fails
			 *
			 * @param int $channel_id
			 * @param int $listing_id
			 * @param ApiException $e
			 */
			do_action( 'bigcommerce/channel/error/could_not_fetch_listing', $channel_id, $listing_id, $e );

			return;
		}

		$update_request = new UpdateListingRequest( [
			'channel_id'    => $channel_id,
			'listing_id'    => $listing_id,
			'product_id'    => (int) $listing->getProductId(),
			'state'         => $this->get_listing_state( $post_id, $listing ),
			'name'          => $this->get_listing_title( $post_id, $listing ),
			'description'   => $this->get_listing_description( $post_id, $listing ),
			'external_id'   => $listing->getExternalId(),
			'variants'      => $listing->getVariants(),
		] );

		try {
			$this->channels->updateChannelListings( $channel_id, [ $update_request ] );
		} catch ( ApiException $e ) {
			/**
			 * Error triggered when updating a listing fails
			 *
			 * @param int $channel_id
			 * @param int $listing_id
			 * @param ApiException $e
			 */
			do_action( 'bigcommerce/channel/error/could_not_update_listing', $channel_id, $listing_id, $e );

			return;
		}

		try {
			$response = $this->channels->getChannelListing( $channel_id, $listing_id );
			$listing  = $response->getData();
			$product->update_listing_data( $listing );
		} catch ( ApiException $e ) {
			/**
			 * Error triggered when fetching a listing fails
			 *
			 * @param int $channel_id
			 * @param int $listing_id
			 * @param ApiException $e
			 */
			do_action( 'bigcommerce/channel/error/could_not_fetch_listing', $channel_id, $listing_id, $e );

			return;
		}
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.