Skip to content

Commit

Permalink
Check if this is WooCommerce product before processing (#2680)
Browse files Browse the repository at this point in the history
* Check if this is WooCommerce product before processing

* Changes the action name to add_meta_boxes_product

---------

Co-authored-by: Kader Ibrahim S <[email protected]>
  • Loading branch information
budzanowski and ibndawood authored Jan 3, 2024
1 parent 18d93b5 commit 570cd2e
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions facebook-commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -1614,38 +1614,40 @@ 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 );
} else {
$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 <a href="https://facebook.com/' . $fb_product_item_id .
Expand Down

0 comments on commit 570cd2e

Please sign in to comment.