From 851bf05f15f429b0dbe2ec65fe00829369bfe586 Mon Sep 17 00:00:00 2001 From: Marian Shymon Date: Tue, 1 Oct 2024 17:33:01 +0100 Subject: [PATCH 1/2] Always send stock quantity when available. --- includes/fbproduct.php | 10 +++- tests/Unit/fbproductTest.php | 91 ++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/includes/fbproduct.php b/includes/fbproduct.php index 22bcd392..91081bea 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -724,9 +724,15 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel $product_data = $this->apply_enhanced_catalog_fields_from_attributes( $product_data, $google_product_category ); } - // add the Commerce values (only stock quantity for the moment) - if ( Products::is_product_ready_for_commerce( $this->woo_product ) ) { + // Add stock quantity if the product or variant is stock managed. + // In case if variant is not stock managed but parent is, fallback on parent value + if ( $this->woo_product->managing_stock() ) { $product_data['quantity_to_sell_on_facebook'] = (int) max( 0, $this->woo_product->get_stock_quantity() ); + } else if ($this->woo_product->is_type( 'variation' )) { + $parent_product = wc_get_product( $this->woo_product->get_parent_id()); + if ( $parent_product && $parent_product->managing_stock()) { + $product_data['quantity_to_sell_on_facebook'] = (int) max( 0, $parent_product->get_stock_quantity() ); + } } // Only use checkout URLs if they exist. diff --git a/tests/Unit/fbproductTest.php b/tests/Unit/fbproductTest.php index 36b1b2bf..c8fbbdb0 100644 --- a/tests/Unit/fbproductTest.php +++ b/tests/Unit/fbproductTest.php @@ -213,4 +213,95 @@ public function test_sale_price_and_effective_date( $this->assertEquals( $product_data['sale_price_start_date'], $expected_sale_price_start_date ); $this->assertEquals( $product_data['sale_price_end_date'], $expected_sale_price_end_date ); } + + /** + * Test quantity_to_sell_on_facebook is populated when manage stock is enabled for simple product + * @return void + */ + public function test_quantity_to_sell_on_facebook_when_manage_stock_is_on_for_simple_product() { + $woo_product = WC_Helper_Product::create_simple_product(); + $woo_product->set_manage_stock('yes'); + $woo_product->set_stock_quantity(128); + + $fb_product = new \WC_Facebook_Product( $woo_product ); + $data = $fb_product->prepare_product(); + + $this->assertEquals( $data['quantity_to_sell_on_facebook'], 128 ); + } + + /** + * Test quantity_to_sell_on_facebook is not populated when manage stock is disabled for simple product + * @return void + */ + public function test_quantity_to_sell_on_facebook_when_manage_stock_is_off_for_simple_product() { + $woo_product = WC_Helper_Product::create_simple_product(); + $woo_product->set_manage_stock('no'); + + $fb_product = new \WC_Facebook_Product( $woo_product ); + $data = $fb_product->prepare_product(); + + $this->assertEquals(isset($data['quantity_to_sell_on_facebook']), false); + } + + /** + * Test quantity_to_sell_on_facebook is populated when manage stock is enabled for variable product + * @return void + */ + public function test_quantity_to_sell_on_facebook_when_manage_stock_is_on_for_variable_product() { + $woo_product = WC_Helper_Product::create_variation_product(); + $woo_product->set_manage_stock('yes'); + $woo_product->set_stock_quantity(128); + + $woo_variation = wc_get_product($woo_product->get_children()[0]); + $woo_variation->set_manage_stock('yes'); + $woo_variation->set_stock_quantity(23); + + $fb_parent_product = new \WC_Facebook_Product($woo_product); + $fb_product = new \WC_Facebook_Product( $woo_variation, $fb_parent_product ); + + $data = $fb_product->prepare_product(); + + $this->assertEquals( $data['quantity_to_sell_on_facebook'], 23 ); + } + + /** + * Test quantity_to_sell_on_facebook is not populated when manage stock is disabled for variable product and disabled for its parent + * @return void + */ + public function test_quantity_to_sell_on_facebook_when_manage_stock_is_off_for_variable_product_and_off_for_parent() { + $woo_product = WC_Helper_Product::create_variation_product(); + $woo_product->set_manage_stock('no'); + + $woo_variation = wc_get_product($woo_product->get_children()[0]); + $woo_product->set_manage_stock('no'); + + $fb_parent_product = new \WC_Facebook_Product($woo_product); + $fb_product = new \WC_Facebook_Product( $woo_variation, $fb_parent_product ); + + $data = $fb_product->prepare_product(); + + $this->assertEquals(isset($data['quantity_to_sell_on_facebook']), false); + } + + /** + * Test quantity_to_sell_on_facebook is not populated when manage stock is disabled for variable product and enabled for its parent + * @return void + */ + public function test_quantity_to_sell_on_facebook_when_manage_stock_is_off_for_variable_product_and_on_for_parent() { + $woo_product = WC_Helper_Product::create_variation_product(); + $woo_product->set_manage_stock('yes'); + $woo_product->set_stock_quantity(128); + $woo_product->save(); + + $woo_variation = wc_get_product($woo_product->get_children()[0]); + $woo_variation->set_manage_stock('no'); + $woo_variation->save(); + + $fb_parent_product = new \WC_Facebook_Product($woo_product); + $fb_product = new \WC_Facebook_Product( $woo_variation, $fb_parent_product ); + + $data = $fb_product->prepare_product(); + + $this->assertEquals( $data['quantity_to_sell_on_facebook'], 128 ); + } } From 2f89a668f1c3d1a0899bb47fe1994695db6dc879 Mon Sep 17 00:00:00 2001 From: Marian Shymon Date: Wed, 20 Nov 2024 16:05:20 +0000 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Rodrigue --- includes/fbproduct.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/fbproduct.php b/includes/fbproduct.php index 91081bea..9724541f 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -725,12 +725,12 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel } // Add stock quantity if the product or variant is stock managed. - // In case if variant is not stock managed but parent is, fallback on parent value + // In case if variant is not stock managed but parent is, fallback on parent value. if ( $this->woo_product->managing_stock() ) { $product_data['quantity_to_sell_on_facebook'] = (int) max( 0, $this->woo_product->get_stock_quantity() ); - } else if ($this->woo_product->is_type( 'variation' )) { - $parent_product = wc_get_product( $this->woo_product->get_parent_id()); - if ( $parent_product && $parent_product->managing_stock()) { + } else if ( $this->woo_product->is_type( 'variation' ) ) { + $parent_product = wc_get_product( $this->woo_product->get_parent_id() ); + if ( $parent_product && $parent_product->managing_stock() ) { $product_data['quantity_to_sell_on_facebook'] = (int) max( 0, $parent_product->get_stock_quantity() ); } }