Add cart cleanup functionality to single page Bookable products

This commit is contained in:
Pedro Silva 2023-07-05 17:10:26 +01:00
parent c00b5906f7
commit 3b5a4b7f23
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
5 changed files with 156 additions and 24 deletions

View file

@ -73,6 +73,13 @@ class Item {
*/
protected $tax_rate;
/**
* The tax rate.
*
* @var string|null
*/
protected $cart_item_key;
/**
* Item constructor.
*
@ -84,6 +91,7 @@ class Item {
* @param string $sku The SKU.
* @param string $category The category.
* @param float $tax_rate The tax rate.
* @param ?string $cart_item_key The cart key for this item.
*/
public function __construct(
string $name,
@ -93,18 +101,20 @@ class Item {
Money $tax = null,
string $sku = '',
string $category = 'PHYSICAL_GOODS',
float $tax_rate = 0
float $tax_rate = 0,
string $cart_item_key = null
) {
$this->name = $name;
$this->unit_amount = $unit_amount;
$this->quantity = $quantity;
$this->description = $description;
$this->tax = $tax;
$this->sku = $sku;
$this->category = ( self::DIGITAL_GOODS === $category ) ? self::DIGITAL_GOODS : self::PHYSICAL_GOODS;
$this->category = $category;
$this->tax_rate = $tax_rate;
$this->name = $name;
$this->unit_amount = $unit_amount;
$this->quantity = $quantity;
$this->description = $description;
$this->tax = $tax;
$this->sku = $sku;
$this->category = ( self::DIGITAL_GOODS === $category ) ? self::DIGITAL_GOODS : self::PHYSICAL_GOODS;
$this->category = $category;
$this->tax_rate = $tax_rate;
$this->cart_item_key = $cart_item_key;
}
/**
@ -179,6 +189,15 @@ class Item {
return round( (float) $this->tax_rate, 2 );
}
/**
* Returns the cart key for this item.
*
* @return string
*/
public function cart_item_key():?string {
return $this->cart_item_key;
}
/**
* Returns the object as array.
*
@ -202,6 +221,10 @@ class Item {
$item['tax_rate'] = (string) $this->tax_rate();
}
if ( $this->cart_item_key() ) {
$item['cart_item_key'] = (string) $this->cart_item_key();
}
return $item;
}
}

View file

@ -44,7 +44,8 @@ class ItemFactory {
public function from_wc_cart( \WC_Cart $cart ): array {
$items = array_map(
function ( array $item ): Item {
$product = $item['data'];
$product = $item['data'];
$cart_item_key = $item['key'] ?? null;
/**
* The WooCommerce product.
@ -61,7 +62,9 @@ class ItemFactory {
$this->prepare_description( $product->get_description() ),
null,
$product->get_sku(),
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
0,
$cart_item_key
);
},
$cart->get_cart_contents()