Skip to content

Commit

Permalink
Merge pull request #2809 from vinkmeta/sale_price
Browse files Browse the repository at this point in the history
Fix - Sale price effective date
  • Loading branch information
vahidkay-meta authored Dec 4, 2024
2 parents 6707652 + 38b292c commit f38a0b7
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 12 deletions.
33 changes: 21 additions & 12 deletions includes/fbproduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,22 +457,31 @@ public function add_sale_price( $product_data, $for_items_batch = false ) {

$sale_price = $this->woo_product->get_sale_price();
$sale_price_effective_date = '';

$sale_start =
( $date = $this->woo_product->get_date_on_sale_from() )
? date_i18n( WC_DateTime::ATOM, $date->getOffsetTimestamp() )
: self::MIN_DATE_1 . self::MIN_TIME;

$sale_end =
( $date = $this->woo_product->get_date_on_sale_to() )
? date_i18n( WC_DateTime::ATOM, $date->getOffsetTimestamp() )
: self::MAX_DATE . self::MAX_TIME;
$sale_start = '';
$sale_end = '';

// check if sale exist
if ( is_numeric( $sale_price ) && $sale_price > 0 ) {
$sale_price_effective_date = $sale_start . '/' . $sale_end;
$sale_price =
$sale_start =
( $date = $this->woo_product->get_date_on_sale_from() )
? date_i18n( WC_DateTime::ATOM, $date->getOffsetTimestamp() )
: self::MIN_DATE_1 . self::MIN_TIME;
$sale_end =
( $date = $this->woo_product->get_date_on_sale_to() )
? date_i18n( WC_DateTime::ATOM, $date->getOffsetTimestamp() )
: self::MAX_DATE . self::MAX_TIME;
$sale_price_effective_date =
( $sale_start == self::MIN_DATE_1 . self::MIN_TIME && $sale_end == self::MAX_DATE . self::MAX_TIME )
? ''
: $sale_start . '/' . $sale_end;
$sale_price =
intval( round( $this->get_price_plus_tax( $sale_price ) * 100 ) );

// Set Sale start and end as empty if set to default values
if ( $sale_start == self::MIN_DATE_1 . self::MIN_TIME && $sale_end == self::MAX_DATE . self::MAX_TIME ) {
$sale_start = '';
$sale_end = '';
}
}

// check if sale is expired and sale time range is valid
Expand Down
110 changes: 110 additions & 0 deletions tests/Unit/fbproductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,114 @@ public function test_filter_fb_description() {
$this->assertEquals( $description, 'fb description' );

}

/**
* Test Data Provider for sale_price related fields
*/
public function provide_sale_price_data() {
return [
[
11.5,
null,
null,
1150,
'11.5 USD',
'',
'',
'',
],
[
0,
null,
null,
0,
'0 USD',
'',
'',
'',
],
[
null,
null,
null,
0,
'',
'',
'',
'',
],
[
null,
'2024-08-08',
'2024-08-18',
0,
'',
'',
'',
'',
],
[
11,
'2024-08-08',
null,
1100,
'11 USD',
'2024-08-08T00:00:00+00:00/2038-01-17T23:59+00:00',
'2024-08-08T00:00:00+00:00',
'2038-01-17T23:59+00:00',
],
[
11,
null,
'2024-08-08',
1100,
'11 USD',
'1970-01-29T00:00+00:00/2024-08-08T00:00:00+00:00',
'1970-01-29T00:00+00:00',
'2024-08-08T00:00:00+00:00',
],
[
11,
'2024-08-08',
'2024-08-09',
1100,
'11 USD',
'2024-08-08T00:00:00+00:00/2024-08-09T00:00:00+00:00',
'2024-08-08T00:00:00+00:00',
'2024-08-09T00:00:00+00:00',
],
];
}

/**
* Test that sale_price related fields are being set correctly while preparing product.
*
* @dataProvider provide_sale_price_data
* @return void
*/
public function test_sale_price_and_effective_date(
$salePrice,
$sale_price_start_date,
$sale_price_end_date,
$expected_sale_price,
$expected_sale_price_for_batch,
$expected_sale_price_effective_date,
$expected_sale_price_start_date,
$expected_sale_price_end_date
) {
$product = WC_Helper_Product::create_simple_product();
$facebook_product = new \WC_Facebook_Product( $product );
$facebook_product->set_sale_price( $salePrice );
$facebook_product->set_date_on_sale_from( $sale_price_start_date );
$facebook_product->set_date_on_sale_to( $sale_price_end_date );

$product_data = $facebook_product->prepare_product( $facebook_product->get_id(), \WC_Facebook_Product::PRODUCT_PREP_TYPE_ITEMS_BATCH );
$this->assertEquals( $product_data['sale_price'], $expected_sale_price_for_batch );
$this->assertEquals( $product_data['sale_price_effective_date'], $expected_sale_price_effective_date );

$product_data = $facebook_product->prepare_product( $facebook_product->get_id(), \WC_Facebook_Product::PRODUCT_PREP_TYPE_FEED );
$this->assertEquals( $product_data['sale_price'], $expected_sale_price );
$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 );
}
}

0 comments on commit f38a0b7

Please sign in to comment.