| 1 |
<?php namespace TierPricingTable\Addons\UserBasedPricing; |
| 2 |
|
| 3 |
use Exception; |
| 4 |
|
| 5 |
class UserBasedPricingRule { |
| 6 |
|
| 7 |
protected $productId; |
| 8 |
protected $userId; |
| 9 |
protected $pricingType = 'flat'; |
| 10 |
protected $discount = null; |
| 11 |
protected $discountType = 'sale_price'; |
| 12 |
protected $salePrice = null; |
| 13 |
protected $regularPrice = null; |
| 14 |
protected $minimumOrderQuantity = null; |
| 15 |
protected $tieredPricingType = 'fixed'; |
| 16 |
protected $percentageTieredPricingRules = array(); |
| 17 |
protected $fixedTieredPricingRules = array(); |
| 18 |
protected $taxStatus = ''; |
| 19 |
protected $taxClass = ''; |
| 20 |
|
| 21 |
public function __construct( $productId, $userId ) { |
| 22 |
$this->productId = $productId; |
| 23 |
$this->userId = $userId; |
| 24 |
} |
| 25 |
|
| 26 |
public function getProductId(): ?int { |
| 27 |
return $this->productId; |
| 28 |
} |
| 29 |
|
| 30 |
public function setProductId( int $productId ) { |
| 31 |
$this->productId = $productId; |
| 32 |
} |
| 33 |
|
| 34 |
public function getUserId(): int { |
| 35 |
return $this->userId; |
| 36 |
} |
| 37 |
|
| 38 |
public function setUserId( int $userId ) { |
| 39 |
$this->userId = $userId; |
| 40 |
} |
| 41 |
|
| 42 |
public function getPricingType(): string { |
| 43 |
return $this->pricingType ? $this->pricingType : 'flat'; |
| 44 |
} |
| 45 |
|
| 46 |
public function setPricingType( string $pricingType ) { |
| 47 |
$this->pricingType = in_array( $pricingType, array( 'flat', 'percentage' ) ) ? $pricingType : 'flat'; |
| 48 |
} |
| 49 |
|
| 50 |
public function getDiscount(): ?float { |
| 51 |
return $this->discount; |
| 52 |
} |
| 53 |
|
| 54 |
public function setDiscount( ?float $discount ) { |
| 55 |
$this->discount = $discount; |
| 56 |
} |
| 57 |
|
| 58 |
public function getDiscountType(): string { |
| 59 |
return $this->discountType; |
| 60 |
} |
| 61 |
|
| 62 |
public function setDiscountType( string $discountType ) { |
| 63 |
$this->discountType = in_array( $discountType, |
| 64 |
array( 'sale_price', 'regular_price' ) ) ? $discountType : 'sale_price'; |
| 65 |
} |
| 66 |
|
| 67 |
public function getSalePrice(): ?float { |
| 68 |
return $this->salePrice; |
| 69 |
} |
| 70 |
|
| 71 |
public function setSalePrice( ?float $salePrice ) { |
| 72 |
$this->salePrice = $salePrice; |
| 73 |
} |
| 74 |
|
| 75 |
public function getRegularPrice(): ?float { |
| 76 |
return $this->regularPrice; |
| 77 |
} |
| 78 |
|
| 79 |
public function setRegularPrice( ?float $regularPrice ) { |
| 80 |
$this->regularPrice = $regularPrice; |
| 81 |
} |
| 82 |
|
| 83 |
public function getMinimumOrderQuantity(): ?int { |
| 84 |
return $this->minimumOrderQuantity; |
| 85 |
} |
| 86 |
|
| 87 |
public function setMinimumOrderQuantity( ?int $minimumOrderQuantity ) { |
| 88 |
$this->minimumOrderQuantity = intval( $minimumOrderQuantity ) > 1 ? $minimumOrderQuantity : null; |
| 89 |
} |
| 90 |
|
| 91 |
public function getTieredPricingType(): string { |
| 92 |
return $this->tieredPricingType ? $this->tieredPricingType : 'fixed'; |
| 93 |
} |
| 94 |
|
| 95 |
public function setTieredPricingType( string $tieredPricingType ) { |
| 96 |
$this->tieredPricingType = in_array( $tieredPricingType, |
| 97 |
array( 'fixed', 'percentage' ) ) ? $tieredPricingType : 'fixed'; |
| 98 |
} |
| 99 |
|
| 100 |
public function getPercentageTieredPricingRules(): array { |
| 101 |
return $this->percentageTieredPricingRules; |
| 102 |
} |
| 103 |
|
| 104 |
public function setPercentageTieredPricingRules( array $percentageTieredPricingRules ) { |
| 105 |
$this->percentageTieredPricingRules = $percentageTieredPricingRules; |
| 106 |
} |
| 107 |
|
| 108 |
public function getFixedTieredPricingRules(): array { |
| 109 |
return $this->fixedTieredPricingRules; |
| 110 |
} |
| 111 |
|
| 112 |
public function setFixedTieredPricingRules( array $fixedTieredPricingRules ) { |
| 113 |
$this->fixedTieredPricingRules = $fixedTieredPricingRules; |
| 114 |
} |
| 115 |
|
| 116 |
public function getTieredPricingRules(): array { |
| 117 |
return $this->getTieredPricingType() === 'percentage' ? $this->getPercentageTieredPricingRules() : $this->getFixedTieredPricingRules(); |
| 118 |
} |
| 119 |
|
| 120 |
public function getTaxStatus(): string { |
| 121 |
return $this->taxStatus; |
| 122 |
} |
| 123 |
|
| 124 |
public function setTaxStatus( string $taxStatus ) { |
| 125 |
$this->taxStatus = $taxStatus; |
| 126 |
} |
| 127 |
|
| 128 |
public function getTaxClass(): string { |
| 129 |
return $this->taxClass; |
| 130 |
} |
| 131 |
|
| 132 |
public function setTaxClass( string $taxClass ) { |
| 133 |
$this->taxClass = $taxClass; |
| 134 |
} |
| 135 |
|
| 136 |
public function asArray(): array { |
| 137 |
return array( |
| 138 |
// main |
| 139 |
'product_id' => $this->getProductId(), |
| 140 |
'user_id' => $this->getUserId(), |
| 141 |
|
| 142 |
// Pricing |
| 143 |
'pricing_type' => $this->getPricingType(), |
| 144 |
'regular_price' => $this->getRegularPrice(), |
| 145 |
'sale_price' => $this->getSalePrice(), |
| 146 |
'discount' => $this->getDiscount(), |
| 147 |
'discount_type' => $this->getDiscountType(), |
| 148 |
|
| 149 |
// Tiered Pricing |
| 150 |
'tiered_pricing_type' => $this->getTieredPricingType(), |
| 151 |
'percentage_rules' => $this->getPercentageTieredPricingRules(), |
| 152 |
'fixed_rules' => $this->getFixedTieredPricingRules(), |
| 153 |
|
| 154 |
// MOQ |
| 155 |
'minimum' => $this->getMinimumOrderQuantity(), |
| 156 |
|
| 157 |
// Tax |
| 158 |
'tax_status' => $this->getTaxStatus(), |
| 159 |
'tax_class' => $this->getTaxClass(), |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Save |
| 165 |
* |
| 166 |
* @throws Exception |
| 167 |
*/ |
| 168 |
public function save() { |
| 169 |
|
| 170 |
if ( ! $this->getProductId() || ! $this->getUserId() ) { |
| 171 |
throw new Exception( 'Rule requires product id and user id to be saved' ); |
| 172 |
} |
| 173 |
|
| 174 |
$dataToUpdate = array( |
| 175 |
// Pricing |
| 176 |
'tiered_price_pricing_type' => $this->getPricingType(), |
| 177 |
'tiered_price_regular_price' => $this->getRegularPrice(), |
| 178 |
'tiered_price_sale_price' => $this->getSalePrice(), |
| 179 |
'tiered_price_discount' => $this->getDiscount(), |
| 180 |
'tiered_price_discount_type' => $this->getDiscountType(), |
| 181 |
|
| 182 |
// Tiered Pricing |
| 183 |
'tiered_price_rules_type' => $this->getTieredPricingType(), |
| 184 |
'percentage_price_rules' => $this->getPercentageTieredPricingRules(), |
| 185 |
'fixed_price_rules' => $this->getFixedTieredPricingRules(), |
| 186 |
|
| 187 |
// MOQ |
| 188 |
'tiered_price_minimum_qty' => $this->getMinimumOrderQuantity(), |
| 189 |
|
| 190 |
// Tax |
| 191 |
'tiered_price_tax_status' => $this->getTaxStatus(), |
| 192 |
'tiered_price_tax_class' => $this->getTaxClass(), |
| 193 |
); |
| 194 |
|
| 195 |
$userId = $this->getUserId(); |
| 196 |
|
| 197 |
foreach ( $dataToUpdate as $key => $value ) { |
| 198 |
$metaKey = "_user_{$userId}_" . $key; |
| 199 |
update_post_meta( $this->getProductId(), $metaKey, $value ); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
public static function build( int $productId, int $userId ): self { |
| 204 |
|
| 205 |
$rule = new self( $productId, $userId ); |
| 206 |
|
| 207 |
// Regular pricing |
| 208 |
$rule->setPricingType( UserBasedPriceManager::getProductPricingType( $productId, $userId ) ); |
| 209 |
$rule->setSalePrice( UserBasedPriceManager::getProductSaleUserPrice( $productId, $userId ) ); |
| 210 |
$rule->setRegularPrice( UserBasedPriceManager::getProductRegularUserPrice( $productId, $userId ) ); |
| 211 |
$rule->setDiscount( UserBasedPriceManager::getProductDiscount( $productId, $userId ) ); |
| 212 |
$rule->setDiscountType( UserBasedPriceManager::getProductDiscountType( $productId, $userId ) ); |
| 213 |
|
| 214 |
// Tiered Pricing |
| 215 |
$rule->setTieredPricingType( UserBasedPriceManager::getPricingType( $productId, $userId ) ); |
| 216 |
$rule->setPercentageTieredPricingRules( UserBasedPriceManager::getPercentagePriceRules( $productId, $userId ) ); |
| 217 |
$rule->setFixedTieredPricingRules( UserBasedPriceManager::getFixedPriceRules( $productId, $userId ) ); |
| 218 |
|
| 219 |
// MOQ |
| 220 |
$rule->setMinimumOrderQuantity( UserBasedPriceManager::getProductQtyMin( $productId, $userId ) ); |
| 221 |
|
| 222 |
// Tax |
| 223 |
$rule->setTaxStatus( UserBasedPriceManager::getProductTaxStatus( $productId, $userId ) ); |
| 224 |
$rule->setTaxClass( UserBasedPriceManager::getProductTaxClass( $productId, $userId ) ); |
| 225 |
|
| 226 |
return apply_filters( 'tiered_pricing_table/user_based/after_built_rule', $rule ); |
| 227 |
} |
| 228 |
|
| 229 |
public static function buildFromArray( int $productId, int $userId, array $data ): self { |
| 230 |
$rule = new self( $productId, $userId ); |
| 231 |
|
| 232 |
// Regular pricing |
| 233 |
$rule->setPricingType( isset( $data['pricing_type'] ) ? (string) $data['pricing_type'] : 'flat' ); |
| 234 |
$rule->setRegularPrice( isset( $data['regular_price'] ) ? (float) $data['regular_price'] : null ); |
| 235 |
$rule->setSalePrice( isset( $data['sale_price'] ) ? (float) $data['sale_price'] : null ); |
| 236 |
$rule->setDiscount( isset( $data['discount'] ) ? (float) min( 100, $data['discount'] ) : null ); |
| 237 |
$rule->setDiscountType( isset( $data['discount_type'] ) ? (string) $data['discount_type'] : 'sale_price' ); |
| 238 |
|
| 239 |
// Tiered Pricing |
| 240 |
$rule->setTieredPricingType( isset( $data['tiered_pricing_type'] ) ? (string) $data['tiered_pricing_type'] : 'fixed' ); |
| 241 |
$rule->setPercentageTieredPricingRules( isset( $data['percentage_tiered_pricing_rules'] ) ? (array) $data['percentage_tiered_pricing_rules'] : array() ); |
| 242 |
$rule->setFixedTieredPricingRules( isset( $data['fixed_tiered_pricing_rules'] ) ? (array) $data['fixed_tiered_pricing_rules'] : array() ); |
| 243 |
|
| 244 |
// MOQ |
| 245 |
$rule->setMinimumOrderQuantity( isset( $data['minimum_order_quantity'] ) ? (int) $data['minimum_order_quantity'] : null ); |
| 246 |
|
| 247 |
// Tax |
| 248 |
$rule->setTaxStatus( isset( $data['tax_status'] ) ? (string) $data['tax_status'] : '' ); |
| 249 |
$rule->setTaxClass( isset( $data['tax_class'] ) ? (string) $data['tax_class'] : '' ); |
| 250 |
|
| 251 |
return apply_filters( 'tiered_pricing_table/user_based/after_built_rule_from_array', $rule, $userId, $data ); |
| 252 |
} |
| 253 |
} |
| 254 |
|