| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trait WcCartTrait. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Framework; |
| 11 |
|
| 12 |
use stdClass; |
| 13 |
use WC_Cart; |
| 14 |
use WC_Customer; |
| 15 |
use WP_Error; |
| 16 |
|
| 17 |
/** |
| 18 |
* Trait WcCartTrait. |
| 19 |
* |
| 20 |
* @package Packetery |
| 21 |
*/ |
| 22 |
trait WcCartTrait { |
| 23 |
/** |
| 24 |
* Gets cart contents. |
| 25 |
* |
| 26 |
* @return array<string|int, mixed> of cart items |
| 27 |
*/ |
| 28 |
public function cartGetCartContents(): array { |
| 29 |
return WC()->cart->get_cart_contents(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Gets cart total. This is the total of items in the cart, but after discounts. Subtotal is before discounts. |
| 34 |
* |
| 35 |
* @return float |
| 36 |
*/ |
| 37 |
public function cartGetCartContentsTotal(): float { |
| 38 |
return (float) WC()->cart->get_cart_contents_total(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Gets cart tax amount. |
| 43 |
* |
| 44 |
* @return float |
| 45 |
*/ |
| 46 |
public function cartGetCartContentsTax(): float { |
| 47 |
return (float) WC()->cart->get_cart_contents_tax(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get weight of items in the cart. |
| 52 |
* |
| 53 |
* @return float |
| 54 |
*/ |
| 55 |
public function cartGetCartContentsWeight(): float { |
| 56 |
return WC()->cart->get_cart_contents_weight(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the contents of the cart in an array. |
| 61 |
* |
| 62 |
* @return array contents of the cart |
| 63 |
*/ |
| 64 |
public function cartGetCartContent(): array { |
| 65 |
return WC()->cart->get_cart(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Gets cart instance. |
| 70 |
* |
| 71 |
* @return WC_Cart|null |
| 72 |
*/ |
| 73 |
public function cart(): ?WC_Cart { |
| 74 |
return WC()->cart; |
| 75 |
} |
| 76 |
|
| 77 |
public function cartCalculateTotals(): void { |
| 78 |
WC()->cart->calculate_totals(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Value is cast to float because PHPDoc is not reliable. |
| 83 |
*/ |
| 84 |
public function cartGetSubtotal(): float { |
| 85 |
return (float) WC()->cart->get_subtotal(); |
| 86 |
} |
| 87 |
|
| 88 |
public function cartGetCustomer(): WC_Customer { |
| 89 |
return WC()->cart->get_customer(); |
| 90 |
} |
| 91 |
|
| 92 |
public function cartCalculateShipping(): array { |
| 93 |
return WC()->cart->calculate_shipping(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param array $args |
| 98 |
* |
| 99 |
* @return stdClass|WP_Error Either a fee object if added, or a WP_Error if it failed. |
| 100 |
*/ |
| 101 |
public function cartFeesApiAddFee( array $args ): object { |
| 102 |
return WC()->cart->fees_api()->add_fee( $args ); |
| 103 |
} |
| 104 |
} |
| 105 |
|