| 1 |
<?php namespace TierPricingTable; |
| 2 |
|
| 3 |
use WC_Product; |
| 4 |
|
| 5 |
class PricingRule { |
| 6 |
|
| 7 |
protected ?int $minimum = null; |
| 8 |
protected ?bool $mixAndMatchMinQuantity = null; |
| 9 |
protected array $rules = array(); |
| 10 |
protected string $type = 'fixed'; |
| 11 |
protected int $productId; |
| 12 |
|
| 13 |
protected ?WC_product $product = null; |
| 14 |
|
| 15 |
public string $provider = 'product'; |
| 16 |
public array $providerData = array(); |
| 17 |
|
| 18 |
protected array $modificationLog = array(); |
| 19 |
|
| 20 |
public array $data = array(); |
| 21 |
|
| 22 |
public array $customColumnsData = array(); |
| 23 |
|
| 24 |
public array $pricingData = array( |
| 25 |
'regular_price' => null, |
| 26 |
'sale_price' => null, |
| 27 |
'discount' => null, |
| 28 |
'pricing_type' => null, // fixed or percentage |
| 29 |
'tax_status' => null, |
| 30 |
'tax_class' => null, |
| 31 |
); |
| 32 |
|
| 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 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public function getProductId(): int { |
| 43 |
return $this->productId; |
| 44 |
} |
| 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 |
|
| 55 |
public function getMinimum( $forceValue = false ): ?int { |
| 56 |
|
| 57 |
if ( $forceValue ) { |
| 58 |
return $this->minimum ?: 1; |
| 59 |
} |
| 60 |
|
| 61 |
return $this->minimum; |
| 62 |
} |
| 63 |
|
| 64 |
public function setMinimum( ?int $minimum ) { |
| 65 |
$this->minimum = $minimum > 0 ? $minimum : null; |
| 66 |
} |
| 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 |
|
| 83 |
public function getRules(): array { |
| 84 |
return $this->rules; |
| 85 |
} |
| 86 |
|
| 87 |
public function setRules( array $rules ) { |
| 88 |
$this->rules = $rules; |
| 89 |
} |
| 90 |
|
| 91 |
public function getType(): string { |
| 92 |
return $this->type; |
| 93 |
} |
| 94 |
|
| 95 |
public function setType( string $type ) { |
| 96 |
$this->type = in_array( $type, array( 'fixed', 'percentage' ) ) ? $type : 'fixed'; |
| 97 |
} |
| 98 |
|
| 99 |
public function isPercentage(): bool { |
| 100 |
return $this->getType() === 'percentage'; |
| 101 |
} |
| 102 |
|
| 103 |
public function isFixed(): bool { |
| 104 |
return $this->getType() === 'fixed'; |
| 105 |
} |
| 106 |
|
| 107 |
public function getTierPrice( $quantity, bool $withTaxes = true, $place = 'shop', ?bool $round = null ) { |
| 108 |
return PriceManager::getPriceByRules( $quantity, $this->getProductId(), 'view', $place, $withTaxes, $this, |
| 109 |
$round ); |
| 110 |
} |
| 111 |
|
| 112 |
public function logPricingModification( string $modification ) { |
| 113 |
$this->modificationLog[] = $modification; |
| 114 |
} |
| 115 |
|
| 116 |
public function getPricingLog(): array { |
| 117 |
return $this->modificationLog; |
| 118 |
} |
| 119 |
} |
| 120 |
|