Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Sync stock quantity when available #2811

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}
}
Loading