Skip to content

Commit

Permalink
Merge pull request #2811 from mshymon/update_to_quantity_to_sell_on_f…
Browse files Browse the repository at this point in the history
…acebook_2

Fix - Sync stock quantity when available
  • Loading branch information
vahidkay-meta authored Dec 5, 2024
2 parents feeba3a + 2f89a66 commit c263bd6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
10 changes: 8 additions & 2 deletions includes/fbproduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
91 changes: 91 additions & 0 deletions tests/Unit/fbproductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

0 comments on commit c263bd6

Please sign in to comment.