Product_Creator::create( $product_id )

Summary

Handle product creation logic


Parameters

$product_id

(Required)


Source

File: src/BigCommerce/Webhooks/Product_Creator.php

    public function create( $product_id ) {
        $connections = new Connections();
        $channels    = $connections->active();

        if ( empty( $channels ) ) {
            do_action( 'bigcommerce/import/error', __( 'No channels connected. Product import canceled.', 'bigcommerce' ) );

            return;
        }

        try {
            /*
			 * Listings should not be updated when saving a product on import.
			 *
			 * Create our own callback instead of __return_false() so that
			 * we don't inadvertently unhook someone else's filter later
			 */
            $empty = function () {
                return false;
            };
            add_filter( 'bigcommerce/channel/listing/should_update', $empty, 10, 0 );
            add_filter( 'bigcommerce/channel/listing/should_delete', $empty, 10, 0 );

            $product = $this->catalog->getProductById( $product_id, [
                'include' => [ 'variants', 'custom_fields', 'images', 'videos', 'bulk_pricing_rules', 'options', 'modifiers' ],
            ] )->getData();

            foreach ( $channels as $channel ) {
               $this->handle_product_creation( $product, $channel );
            }
        } catch ( ApiException $e ) {
            do_action( 'bigcommerce/import/error', $e->getMessage(), [
                'response' => $e->getResponseBody(),
                'headers'  => $e->getResponseHeaders(),
            ] );
            do_action( 'bigcommerce/log', Error_Log::DEBUG, $e->getTraceAsString(), [] );
        } finally {
            // unhook the filters we added at the start
            remove_filter( 'bigcommerce/channel/listing/should_update', $empty, 10 );
            remove_filter( 'bigcommerce/channel/listing/should_delete', $empty, 10 );
        }
    }


User Contributed Notes

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