Display price depend on price location (#25080)

* display price depend on price location

* change arg key

* test for wc_get_price_to_display

* fix spacing & function docblock

* Add changelog file

* Address PHPCS issues

* Rename `display_location` to `display_context`

* Address PHPCS issues

* Update changelog wording

---------

Co-authored-by: Kolya lukin <mykola@imagecms.net>
Co-authored-by: Néstor Soriano <konamiman@konamiman.com>
Co-authored-by: Jorge A. Torres <jorge.torres@automattic.com>
This commit is contained in:
mynewvk 2023-03-14 23:27:39 +02:00 committed by GitHub
parent 3809392f7c
commit f43e36a45b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 170 additions and 9 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: enhancement
Add 'display_context' argument to wc_get_price_to_display().

View File

@ -1101,9 +1101,14 @@ function wc_get_price_excluding_tax( $product, $args = array() ) {
}
/**
* Returns the price including or excluding tax, based on the 'woocommerce_tax_display_shop' setting.
* Returns the price including or excluding tax.
*
* By default it's based on the 'woocommerce_tax_display_shop' setting.
* Set `$arg['display_context']` to 'cart' to base on the 'woocommerce_tax_display_cart' setting instead.
*
* @since 3.0.0
* @since 7.6.0 Added `display_context` argument.
*
* @param WC_Product $product WC_Product object.
* @param array $args Optional arguments to pass product quantity and price.
* @return float
@ -1112,15 +1117,19 @@ function wc_get_price_to_display( $product, $args = array() ) {
$args = wp_parse_args(
$args,
array(
'qty' => 1,
'price' => $product->get_price(),
'qty' => 1,
'price' => $product->get_price(),
'display_context' => 'shop',
)
);
$price = $args['price'];
$qty = $args['qty'];
$price = $args['price'];
$qty = $args['qty'];
$tax_display = get_option(
'cart' === $args['display_context'] ? 'woocommerce_tax_display_cart' : 'woocommerce_tax_display_shop'
);
return 'incl' === get_option( 'woocommerce_tax_display_shop' ) ?
return 'incl' === $tax_display ?
wc_get_price_including_tax(
$product,
array(

View File

@ -1078,4 +1078,136 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
$this->assertEquals( $status_options, wc_get_product_stock_status_options() );
}
/**
* Tests `wc_get_price_to_display()` and its `display_context` argument.
*/
public function test_wc_get_price_to_display() {
// Enable taxes.
update_option( 'woocommerce_calc_taxes', 'yes' );
update_option( 'woocommerce_prices_include_tax', 'no' );
$customer_location = WC_Tax::get_tax_location();
$tax_rate = array(
'tax_rate_country' => $customer_location[0],
'tax_rate_state' => '',
'tax_rate' => '20.0000',
'tax_rate_name' => 'VAT',
'tax_rate_priority' => '1',
'tax_rate_compound' => '0',
'tax_rate_shipping' => '1',
'tax_rate_order' => '1',
'tax_rate_class' => '',
);
WC_Tax::_insert_tax_rate( $tax_rate );
$product = new WC_Product_Simple();
$product->set_regular_price( '100' );
// Display price included taxes at shop and cart.
update_option( 'woocommerce_tax_display_cart', 'incl' );
update_option( 'woocommerce_tax_display_shop', 'incl' );
$price_shop = wc_get_price_to_display(
$product,
array(
'price' => 100,
'qty' => 1,
'display_context' => 'shop',
)
);
$this->assertEquals( 120, $price_shop );
$price_shop = wc_get_price_to_display(
$product,
array(
'price' => 100,
'qty' => 1,
'display_context' => 'cart',
)
);
$this->assertEquals( 120, $price_shop );
// Display price included taxes only at shop.
update_option( 'woocommerce_tax_display_cart', 'excl' );
update_option( 'woocommerce_tax_display_shop', 'incl' );
$price_shop = wc_get_price_to_display(
$product,
array(
'price' => 100,
'qty' => 1,
)
);
$this->assertEquals( 120, $price_shop );
$price_shop = wc_get_price_to_display(
$product,
array(
'price' => 100,
'qty' => 1,
'display_context' => 'cart',
)
);
$this->assertEquals( 100, $price_shop );
// Display price included taxes only at cart.
update_option( 'woocommerce_tax_display_cart', 'incl' );
update_option( 'woocommerce_tax_display_shop', 'excl' );
$price_shop = wc_get_price_to_display(
$product,
array(
'price' => 100,
'qty' => 1,
'display_context' => 'shop',
)
);
$this->assertEquals( 100, $price_shop );
$price_shop = wc_get_price_to_display(
$product,
array(
'price' => 100,
'qty' => 1,
'display_context' => 'cart',
)
);
$this->assertEquals( 120, $price_shop );
// Display price excluded taxes at shop and cart.
update_option( 'woocommerce_tax_display_cart', 'excl' );
update_option( 'woocommerce_tax_display_shop', 'excl' );
$price_shop = wc_get_price_to_display(
$product,
array(
'price' => 100,
'qty' => 1,
)
);
$this->assertEquals( 100, $price_shop );
$price_shop = wc_get_price_to_display(
$product,
array(
'price' => 100,
'qty' => 1,
'display_context' => 'cart',
)
);
$this->assertEquals( 100, $price_shop );
}
}

View File

@ -519,7 +519,12 @@ class WC_Tests_Tax extends WC_Unit_Test_Case {
$this->assertGreaterThan( 0, $tax_rate_id );
$new_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d", $tax_rate_id ) );
$new_row = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d",
$tax_rate_id
)
);
$this->assertEquals( $new_row->tax_rate_country, 'GB' );
$this->assertEquals( $new_row->tax_rate_state, '' );
@ -617,7 +622,12 @@ class WC_Tests_Tax extends WC_Unit_Test_Case {
WC_Tax::_update_tax_rate_postcodes( $tax_rate_id, $to_save );
$results = $wpdb->get_col( $wpdb->prepare( "SELECT location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE tax_rate_id = %d ORDER BY location_code ASC", $tax_rate_id ) );
$results = $wpdb->get_col(
$wpdb->prepare(
"SELECT location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE tax_rate_id = %d ORDER BY location_code ASC",
$tax_rate_id
)
);
$this->assertEquals( array( '12345', '90210...90215' ), $results );
}
@ -646,8 +656,14 @@ class WC_Tests_Tax extends WC_Unit_Test_Case {
WC_Tax::_update_tax_rate_cities( $tax_rate_id, $to_save );
$results = $wpdb->get_col( $wpdb->prepare( "SELECT location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE tax_rate_id = %d ORDER BY location_code ASC", $tax_rate_id ) );
$results = $wpdb->get_col(
$wpdb->prepare(
"SELECT location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE tax_rate_id = %d ORDER BY location_code ASC",
$tax_rate_id
)
);
$this->assertEquals( array( 'SOMEWHERE', 'SOMEWHERE_ELSE' ), $results );
}
}