| @@ -1,31 +1,43 @@ | ||
| 1 | 1 | <?php namespace TierPricingTable; |
| 2 | 2 | |
| 3 | +use WC_Product; | |
| 4 | + | |
| 3 | 5 | class PricingRule { |
| 4 | 6 | |
| 5 | - protected $minimum = null; | |
| 6 | - protected $rules = array(); | |
| 7 | - protected $type = 'fixed'; | |
| 8 | - protected $productId; | |
| 7 | + protected ?int $minimum = null; | |
| 8 | + protected ?bool $mixAndMatchMinQuantity = null; | |
| 9 | + protected array $rules = array(); | |
| 10 | + protected string $type = 'fixed'; | |
| 11 | + protected int $productId; | |
| 9 | 12 | |
| 10 | - public $provider = 'product'; | |
| 11 | - public $providerData = array(); | |
| 13 | + protected ?WC_product $product = null; | |
| 12 | 14 | |
| 13 | - protected $modificationLog = array(); | |
| 15 | + public string $provider = 'product'; | |
| 16 | + public array $providerData = array(); | |
| 14 | 17 | |
| 15 | - public $data = array(); | |
| 18 | + protected array $modificationLog = array(); | |
| 16 | 19 | |
| 17 | - public $customColumnsData = array(); | |
| 20 | + public array $data = array(); | |
| 18 | 21 | |
| 19 | - public $pricingData = array( | |
| 22 | + public array $customColumnsData = array(); | |
| 23 | + | |
| 24 | + public array $pricingData = array( | |
| 20 | 25 | 'regular_price' => null, |
| 21 | 26 | 'sale_price' => null, |
| 22 | 27 | 'discount' => null, |
| 23 | 28 | 'pricing_type' => null, // fixed or percentage |
| 29 | + 'tax_status' => null, | |
| 30 | + 'tax_class' => null, | |
| 24 | 31 | ); |
| 25 | 32 | |
| 26 | - public function __construct( $productId ) { | |
| 27 | - $this->productId = intval( $productId ); | |
| 33 | + public function __construct( $productOrId ) { | |
| 34 | + if ( $productOrId instanceof WC_Product ) { | |
| 35 | + $this->product = $productOrId; | |
| 36 | + $this->productId = $productOrId->get_id(); | |
| 37 | + } else { | |
| 38 | + $this->productId = intval( $productOrId ); | |
| 39 | + } | |
| 28 | 40 | } |
| 29 | 41 | |
| 30 | 42 | public function getProductId(): int { |
| 31 | 43 | return $this->productId; |
| @@ -30,8 +42,17 @@ | ||
| 30 | 42 | public function getProductId(): int { |
| 31 | 43 | return $this->productId; |
| 32 | 44 | } |
| 33 | 45 | |
| 46 | + public function getProduct(): ?WC_Product { | |
| 47 | + if ( $this->product === null ) { | |
| 48 | + $product = wc_get_product( $this->productId ); | |
| 49 | + $this->product = $product ?: null; | |
| 50 | + } | |
| 51 | + | |
| 52 | + return $this->product; | |
| 53 | + } | |
| 54 | + | |
| 34 | 55 | public function getMinimum( $forceValue = false ): ?int { |
| 35 | 56 | |
| 36 | 57 | if ( $forceValue ) { |
| 37 | 58 | return $this->minimum ?: 1; |
| @@ -43,8 +64,23 @@ | ||
| 43 | 64 | public function setMinimum( ?int $minimum ) { |
| 44 | 65 | $this->minimum = $minimum > 0 ? $minimum : null; |
| 45 | 66 | } |
| 46 | 67 | |
| 68 | + public function isMixAndMatchMinQuantity(): ?bool { | |
| 69 | + if ( $this->mixAndMatchMinQuantity ) { | |
| 70 | + $product = $this->getProduct(); | |
| 71 | + if ( ! $product || ( ! $product->is_type( 'variable' ) && ! $product->is_type( 'variation' ) ) ) { | |
| 72 | + return false; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + return $this->mixAndMatchMinQuantity; | |
| 77 | + } | |
| 78 | + | |
| 79 | + public function setMixAndMatchMinQuantity( ?bool $mixAndMatch ) { | |
| 80 | + $this->mixAndMatchMinQuantity = $mixAndMatch; | |
| 81 | + } | |
| 82 | + | |
| 47 | 83 | public function getRules(): array { |
| 48 | 84 | return $this->rules; |
| 49 | 85 | } |
| 50 | 86 | |
| @@ -76,8 +112,8 @@ | ||
| 76 | 112 | public function logPricingModification( string $modification ) { |
| 77 | 113 | $this->modificationLog[] = $modification; |
| 78 | 114 | } |
| 79 | 115 | |
| 80 | - public function getPricingLog() { | |
| 116 | + public function getPricingLog(): array { | |
| 81 | 117 | return $this->modificationLog; |
| 82 | 118 | } |
| 83 | 119 | } |