From 570cd2e56a9d5b654ec9dfb463211474857cacf7 Mon Sep 17 00:00:00 2001 From: Bartosz Budzanowski Date: Wed, 3 Jan 2024 13:27:21 +0100 Subject: [PATCH] Check if this is WooCommerce product before processing (#2680) * Check if this is WooCommerce product before processing * Changes the action name to add_meta_boxes_product --------- Co-authored-by: Kader Ibrahim S --- facebook-commerce.php | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/facebook-commerce.php b/facebook-commerce.php index bde685859..0947848df 100644 --- a/facebook-commerce.php +++ b/facebook-commerce.php @@ -315,7 +315,7 @@ public function __construct( WC_Facebookcommerce $facebook_for_woocommerce ) { add_action( 'add_meta_boxes', 'WooCommerce\Facebook\Admin\Product_Sync_Meta_Box::register', 10, 1 ); - add_action( 'add_meta_boxes', [ $this, 'display_batch_api_completed' ], 10, 2 ); + add_action( 'add_meta_boxes_product', [ $this, 'display_batch_api_completed' ], 10, 2 ); add_action( 'wp_ajax_ajax_fb_toggle_visibility', @@ -1614,31 +1614,32 @@ public function delete_product_set_item( string $fb_product_set_id ) { /** * Displays Batch API completed message on simple_product_publish. - * This is called by the hook `add_meta_boxes` because that is sufficient time + * This is called by the hook `add_meta_boxes_product` because that is sufficient time * to retrieve product_item_id for the product item created via batch API. * * Some sanity checks are added before displaying the message after publish * - product_item_id : if exists, means product was created else not and don't display * - should_sync: Don't display if the product is not supposed to be synced. * - * @param string $post_type Wordpress Post type * @param WP_Post $post Wordpress Post * @return void */ - public function display_batch_api_completed( string $post_type, WP_Post $post ) { - $fb_product = new \WC_Facebook_Product( $post->ID ); - $fb_product_item_id = null; - $should_sync = true; - $no_sync_reason = ''; + public function display_batch_api_completed( $post ) { + $fb_product = new \WC_Facebook_Product( $post->ID ); + $fb_product_item_id = null; + $should_sync = true; - if ( $fb_product->woo_product instanceof \WC_Product ) { - try { - facebook_for_woocommerce()->get_product_sync_validator( $fb_product->woo_product )->validate(); - } catch ( \Exception $e ) { - $should_sync = false; - $no_sync_reason = $e->getMessage(); - } + // Bail if this is not a WooCommerce product. + if ( ! $fb_product->woo_product instanceof \WC_Product ) { + return; } + + try { + facebook_for_woocommerce()->get_product_sync_validator( $fb_product->woo_product )->validate(); + } catch ( \Exception $e ) { + $should_sync = false; + } + if( $should_sync ) { if ( $fb_product->woo_product->is_type( 'variable' ) ) { $fb_product_item_id = $this->get_product_fbid( self::FB_PRODUCT_GROUP_ID, $post->ID, $fb_product->woo_product ); @@ -1646,6 +1647,7 @@ public function display_batch_api_completed( string $post_type, WP_Post $post ) $fb_product_item_id = $this->get_product_fbid( self::FB_PRODUCT_ITEM_ID, $post->ID, $fb_product->woo_product ); } } + if ( $fb_product_item_id ) { $this->display_success_message( 'Created product