Get tax rate from item

This commit is contained in:
dinamiko 2022-03-16 16:29:22 +01:00
parent 9dc5aa43e6
commit 28edfcbebc
3 changed files with 33 additions and 7 deletions

View file

@ -14,7 +14,6 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
*/
class Item {
const PHYSICAL_GOODS = 'PHYSICAL_GOODS';
const DIGITAL_GOODS = 'DIGITAL_GOODS';
@ -67,6 +66,13 @@ class Item {
*/
private $category;
/**
* The tax rate.
*
* @var float|int
*/
protected $tax_rate;
/**
* Item constructor.
*
@ -85,7 +91,8 @@ class Item {
string $description = '',
Money $tax = null,
string $sku = '',
string $category = 'PHYSICAL_GOODS'
string $category = 'PHYSICAL_GOODS',
float $tax_rate = 0
) {
$this->name = $name;
@ -94,8 +101,9 @@ class Item {
$this->description = $description;
$this->tax = $tax;
$this->sku = $sku;
$this->category = ( self::DIGITAL_GOODS === $category ) ?
self::DIGITAL_GOODS : self::PHYSICAL_GOODS;
$this->category = ( self::DIGITAL_GOODS === $category ) ? self::DIGITAL_GOODS : self::PHYSICAL_GOODS;
$this->category = $category;
$this->tax_rate = $tax_rate;
}
/**
@ -161,6 +169,16 @@ class Item {
return $this->category;
}
/**
* Returns the tax rate.
*
* @return float
*/
public function tax_rate():float
{
return round((float) $this->tax_rate, 2);
}
/**
* Returns the object as array.
*
@ -180,6 +198,10 @@ class Item {
$item['tax'] = $this->tax()->to_array();
}
if ($this->tax_rate()) {
$item['tax_rate'] = (string) $this->tax_rate();
}
return $item;
}
}

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WC_Tax;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
@ -57,6 +58,7 @@ class ItemFactory {
$price_without_tax_rounded = round( $price_without_tax, 2 );
$tax = round( $price - $price_without_tax_rounded, 2 );
$tax = new Money( $tax, $this->currency );
$tax_rates = WC_Tax::get_rates($product->get_tax_class());
return new Item(
mb_substr( $product->get_name(), 0, 127 ),
new Money( $price_without_tax_rounded, $this->currency ),
@ -64,7 +66,8 @@ class ItemFactory {
mb_substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ),
$tax,
$product->get_sku(),
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
reset($tax_rates)['rate'] ?? 0
);
},
$cart->get_cart_contents()
@ -138,6 +141,7 @@ class ItemFactory {
$price_without_tax_rounded = round( $price_without_tax, 2 );
$tax = round( $price - $price_without_tax_rounded, 2 );
$tax = new Money( $tax, $currency );
$tax_rates = WC_Tax::get_rates($product->get_tax_class());
return new Item(
mb_substr( $product->get_name(), 0, 127 ),
new Money( $price_without_tax_rounded, $currency ),
@ -145,7 +149,8 @@ class ItemFactory {
mb_substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ),
$tax,
$product->get_sku(),
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
reset($tax_rates)['rate'] ?? 0
);
}

View file

@ -77,7 +77,6 @@ class OrderEndpoint {
),
);
$data['purchase_units'][0]['items'][0]['tax_rate'] = '19.00';
$data['purchase_units'][0]['shipping']['name']['full_name'] = 'John Doe';
$data['purchase_units'][0]['shipping']['address'] = array(
'address_line_1' => 'Taunusanlage 12',