| 1 |
<?php namespace TierPricingTable\Addons\RoleBasedPricing; |
| 2 |
|
| 3 |
use Exception; |
| 4 |
|
| 5 |
class RoleBasedPricingRule { |
| 6 |
|
| 7 |
protected $productId; |
| 8 |
protected $role; |
| 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, $role ) { |
| 22 |
$this->productId = $productId; |
| 23 |
$this->role = $role; |
| 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 getRole(): string { |
| 35 |
return $this->role; |
| 36 |
} |
| 37 |
|
| 38 |
public function setRole( string $role ) { |
| 39 |
$this->role = $role; |
| 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 |
'role' => $this->getRole(), |
| 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->getRole() ) { |
| 171 |
throw new Exception( 'Rule requires product id and role to be saved' ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
$dataToUpdate = array( |
| 176 |
// Pricing |
| 177 |
'tiered_price_pricing_type' => $this->getPricingType(), |
| 178 |
'tiered_price_regular_price' => $this->getRegularPrice(), |
| 179 |
'tiered_price_sale_price' => $this->getSalePrice(), |
| 180 |
'tiered_price_discount' => $this->getDiscount(), |
| 181 |
'tiered_price_discount_type' => $this->getDiscountType(), |
| 182 |
|
| 183 |
// Tiered Pricing |
| 184 |
'tiered_price_rules_type' => $this->getTieredPricingType(), |
| 185 |
'percentage_price_rules' => $this->getPercentageTieredPricingRules(), |
| 186 |
'fixed_price_rules' => $this->getFixedTieredPricingRules(), |
| 187 |
|
| 188 |
// MOQ |
| 189 |
'tiered_price_minimum_qty' => $this->getMinimumOrderQuantity(), |
| 190 |
|
| 191 |
// Tax |
| 192 |
'tiered_price_tax_status' => $this->getTaxStatus(), |
| 193 |
'tiered_price_tax_class' => $this->getTaxClass(), |
| 194 |
); |
| 195 |
|
| 196 |
$role = $this->getRole(); |
| 197 |
|
| 198 |
foreach ( $dataToUpdate as $key => $value ) { |
| 199 |
$metaKey = "_{$role}_" . $key; |
| 200 |
update_post_meta( $this->getProductId(), $metaKey, $value ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
public static function build( int $productId, string $role ): self { |
| 205 |
|
| 206 |
$rule = new self( $productId, $role ); |
| 207 |
|
| 208 |
// Regular pricing |
| 209 |
$rule->setPricingType( RoleBasedPriceManager::getProductPricingType( $productId, $role ) ); |
| 210 |
$rule->setSalePrice( RoleBasedPriceManager::getProductSaleRolePrice( $productId, $role ) ); |
| 211 |
$rule->setRegularPrice( RoleBasedPriceManager::getProductRegularRolePrice( $productId, $role ) ); |
| 212 |
$rule->setDiscount( RoleBasedPriceManager::getProductDiscount( $productId, $role ) ); |
| 213 |
$rule->setDiscountType( RoleBasedPriceManager::getProductDiscountType( $productId, $role ) ); |
| 214 |
|
| 215 |
// Tiered Pricing |
| 216 |
$rule->setTieredPricingType( RoleBasedPriceManager::getPricingType( $productId, $role ) ); |
| 217 |
$rule->setPercentageTieredPricingRules( RoleBasedPriceManager::getPercentagePriceRules( $productId, $role ) ); |
| 218 |
$rule->setFixedTieredPricingRules( RoleBasedPriceManager::getFixedPriceRules( $productId, $role ) ); |
| 219 |
|
| 220 |
// MOQ |
| 221 |
$rule->setMinimumOrderQuantity( RoleBasedPriceManager::getProductQtyMin( $productId, $role ) ); |
| 222 |
|
| 223 |
// Tax |
| 224 |
$rule->setTaxStatus( RoleBasedPriceManager::getProductTaxStatus( $productId, $role ) ); |
| 225 |
$rule->setTaxClass( RoleBasedPriceManager::getProductTaxClass( $productId, $role ) ); |
| 226 |
|
| 227 |
return apply_filters( 'tiered_pricing_table/role_based/after_built_rule', $rule ); |
| 228 |
} |
| 229 |
|
| 230 |
public static function buildFromArray( int $productId, string $role, array $data ): self { |
| 231 |
$rule = new self( $productId, $role ); |
| 232 |
|
| 233 |
// Regular pricing |
| 234 |
$rule->setPricingType( isset( $data['pricing_type'] ) ? (string) $data['pricing_type'] : 'flat' ); |
| 235 |
$rule->setRegularPrice( isset( $data['regular_price'] ) ? (float) $data['regular_price'] : null ); |
| 236 |
$rule->setSalePrice( isset( $data['sale_price'] ) ? (float) $data['sale_price'] : null ); |
| 237 |
$rule->setDiscount( isset( $data['discount'] ) ? (float) min( 100, $data['discount'] ) : null ); |
| 238 |
$rule->setDiscountType( isset( $data['discount_type'] ) ? (string) $data['discount_type'] : 'sale_price' ); |
| 239 |
|
| 240 |
// Tiered Pricing |
| 241 |
$rule->setTieredPricingType( isset( $data['tiered_pricing_type'] ) ? (string) $data['tiered_pricing_type'] : 'fixed' ); |
| 242 |
$rule->setPercentageTieredPricingRules( isset( $data['percentage_tiered_pricing_rules'] ) ? (array) $data['percentage_tiered_pricing_rules'] : array() ); |
| 243 |
$rule->setFixedTieredPricingRules( isset( $data['fixed_tiered_pricing_rules'] ) ? (array) $data['fixed_tiered_pricing_rules'] : array() ); |
| 244 |
|
| 245 |
// MOQ |
| 246 |
$rule->setMinimumOrderQuantity( isset( $data['minimum_order_quantity'] ) ? (int) $data['minimum_order_quantity'] : null ); |
| 247 |
|
| 248 |
// Tax |
| 249 |
$rule->setTaxStatus( isset( $data['tax_status'] ) ? (string) $data['tax_status'] : '' ); |
| 250 |
$rule->setTaxClass( isset( $data['tax_class'] ) ? (string) $data['tax_class'] : '' ); |
| 251 |
|
| 252 |
return apply_filters( 'tiered_pricing_table/role_based/after_built_rule_from_array', $rule, $role, $data ); |
| 253 |
} |
| 254 |
} |
| 255 |
|