PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Addons / RoleBasedPricing / RoleBasedPricingRule.php

RoleBasedPricingRule.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Addons/RoleBasedPricing/RoleBasedPricingRule.php

221 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
19 public function __construct( $productId, $role ) {
20 $this->productId = $productId;
21 $this->role = $role;
22 }
23
24 public function getProductId(): ?int {
25 return $this->productId;
26 }
27
28 public function setProductId( int $productId ) {
29 $this->productId = $productId;
30 }
31
32 public function getRole(): string {
33 return $this->role;
34 }
35
36 public function setRole( string $role ) {
37 $this->role = $role;
38 }
39
40 public function getPricingType(): string {
41 return $this->pricingType ? $this->pricingType : 'flat';
42 }
43
44 public function setPricingType( string $pricingType ) {
45 $this->pricingType = in_array( $pricingType, array( 'flat', 'percentage' ) ) ? $pricingType : 'flat';
46 }
47
48 public function getDiscount(): ?float {
49 return $this->discount;
50 }
51
52 public function setDiscount( ?float $discount ) {
53 $this->discount = $discount;
54 }
55
56 public function getDiscountType(): string {
57 return $this->discountType;
58 }
59
60 public function setDiscountType( string $discountType ) {
61 $this->discountType = in_array( $discountType,
62 array( 'sale_price', 'regular_price' ) ) ? $discountType : 'sale_price';
63 }
64
65 public function getSalePrice(): ?float {
66 return $this->salePrice;
67 }
68
69 public function setSalePrice( ?float $salePrice ) {
70 $this->salePrice = $salePrice;
71 }
72
73 public function getRegularPrice(): ?float {
74 return $this->regularPrice;
75 }
76
77 public function setRegularPrice( ?float $regularPrice ) {
78 $this->regularPrice = $regularPrice;
79 }
80
81 public function getMinimumOrderQuantity(): ?int {
82 return $this->minimumOrderQuantity;
83 }
84
85 public function setMinimumOrderQuantity( ?int $minimumOrderQuantity ) {
86 $this->minimumOrderQuantity = intval( $minimumOrderQuantity ) > 1 ? $minimumOrderQuantity : null;
87 }
88
89 public function getTieredPricingType(): string {
90 return $this->tieredPricingType ? $this->tieredPricingType : 'fixed';
91 }
92
93 public function setTieredPricingType( string $tieredPricingType ) {
94 $this->tieredPricingType = in_array( $tieredPricingType,
95 array( 'fixed', 'percentage' ) ) ? $tieredPricingType : 'fixed';
96 }
97
98 public function getPercentageTieredPricingRules(): array {
99 return $this->percentageTieredPricingRules;
100 }
101
102 public function setPercentageTieredPricingRules( array $percentageTieredPricingRules ) {
103 $this->percentageTieredPricingRules = $percentageTieredPricingRules;
104 }
105
106 public function getFixedTieredPricingRules(): array {
107 return $this->fixedTieredPricingRules;
108 }
109
110 public function setFixedTieredPricingRules( array $fixedTieredPricingRules ) {
111 $this->fixedTieredPricingRules = $fixedTieredPricingRules;
112 }
113
114 public function getTieredPricingRules(): array {
115 return $this->getTieredPricingType() === 'percentage' ? $this->getPercentageTieredPricingRules() : $this->getFixedTieredPricingRules();
116 }
117
118 public function asArray(): array {
119 return array(
120 // main
121 'product_id' => $this->getProductId(),
122 'role' => $this->getRole(),
123
124 // Pricing
125 'pricing_type' => $this->getPricingType(),
126 'regular_price' => $this->getRegularPrice(),
127 'sale_price' => $this->getSalePrice(),
128 'discount' => $this->getDiscount(),
129 'discount_type' => $this->getDiscountType(),
130
131 // Tiered Pricing
132 'tiered_pricing_type' => $this->getTieredPricingType(),
133 'percentage_rules' => $this->getPercentageTieredPricingRules(),
134 'fixed_rules' => $this->getFixedTieredPricingRules(),
135
136 // MOQ
137 'minimum' => $this->getMinimumOrderQuantity(),
138 );
139 }
140
141 /**
142 * Save
143 *
144 * @throws Exception
145 */
146 public function save() {
147
148 if ( ! $this->getProductId() || ! $this->getRole() ) {
149 throw new Exception( 'Rule requires product id and role to be saved' );
150 }
151
152
153 $dataToUpdate = array(
154 // Pricing
155 'tiered_price_pricing_type' => $this->getPricingType(),
156 'tiered_price_regular_price' => $this->getRegularPrice(),
157 'tiered_price_sale_price' => $this->getSalePrice(),
158 'tiered_price_discount' => $this->getDiscount(),
159 'tiered_price_discount_type' => $this->getDiscountType(),
160
161 // Tiered Pricing
162 'tiered_price_rules_type' => $this->getTieredPricingType(),
163 'percentage_price_rules' => $this->getPercentageTieredPricingRules(),
164 'fixed_price_rules' => $this->getFixedTieredPricingRules(),
165
166 // MOQ
167 'tiered_price_minimum_qty' => $this->getMinimumOrderQuantity(),
168 );
169
170 $role = $this->getRole();
171
172 foreach ( $dataToUpdate as $key => $value ) {
173 $metaKey = "_{$role}_" . $key;
174 update_post_meta( $this->getProductId(), $metaKey, $value );
175 }
176 }
177
178 public static function build( int $productId, string $role ): self {
179
180 $rule = new self( $productId, $role );
181
182 // Regular pricing
183 $rule->setPricingType( RoleBasedPriceManager::getProductPricingType( $productId, $role ) );
184 $rule->setSalePrice( RoleBasedPriceManager::getProductSaleRolePrice( $productId, $role ) );
185 $rule->setRegularPrice( RoleBasedPriceManager::getProductRegularRolePrice( $productId, $role ) );
186 $rule->setDiscount( RoleBasedPriceManager::getProductDiscount( $productId, $role ) );
187 $rule->setDiscountType( RoleBasedPriceManager::getProductDiscountType( $productId, $role ) );
188
189 // Tiered Pricing
190 $rule->setTieredPricingType( RoleBasedPriceManager::getPricingType( $productId, $role ) );
191 $rule->setPercentageTieredPricingRules( RoleBasedPriceManager::getPercentagePriceRules( $productId, $role ) );
192 $rule->setFixedTieredPricingRules( RoleBasedPriceManager::getFixedPriceRules( $productId, $role ) );
193
194 // MOQ
195 $rule->setMinimumOrderQuantity( RoleBasedPriceManager::getProductQtyMin( $productId, $role ) );
196
197 return apply_filters( 'tiered_pricing_table/role_based/after_built_rule', $rule );
198 }
199
200 public static function buildFromArray( int $productId, string $role, array $data ): self {
201 $rule = new self( $productId, $role );
202
203 // Regular pricing
204 $rule->setPricingType( isset( $data['pricing_type'] ) ? (string) $data['pricing_type'] : 'flat' );
205 $rule->setRegularPrice( isset( $data['regular_price'] ) ? (float) $data['regular_price'] : null );
206 $rule->setSalePrice( isset( $data['sale_price'] ) ? (float) $data['sale_price'] : null );
207 $rule->setDiscount( isset( $data['discount'] ) ? (float) min( 100, $data['discount'] ) : null );
208 $rule->setDiscountType( isset( $data['discount_type'] ) ? (string) $data['discount_type'] : 'sale_price' );
209
210 // Tiered Pricing
211 $rule->setTieredPricingType( isset( $data['tiered_pricing_type'] ) ? (string) $data['tiered_pricing_type'] : 'fixed' );
212 $rule->setPercentageTieredPricingRules( isset( $data['percentage_tiered_pricing_rules'] ) ? (array) $data['percentage_tiered_pricing_rules'] : array() );
213 $rule->setFixedTieredPricingRules( isset( $data['fixed_tiered_pricing_rules'] ) ? (array) $data['fixed_tiered_pricing_rules'] : array() );
214
215 // MOQ
216 $rule->setMinimumOrderQuantity( isset( $data['minimum_order_quantity'] ) ? (int) $data['minimum_order_quantity'] : null );
217
218 return apply_filters( 'tiered_pricing_table/role_based/after_built_rule_from_array', $rule, $role, $data );
219 }
220 }
221