Blocks
1 year ago
Concerns
1 year ago
Traits
1 year ago
AbandonedCheckout.php
2 years ago
AbandonedCheckoutProtocol.php
3 years ago
Account.php
3 years ago
AccountPortalSession.php
3 years ago
Activation.php
2 years ago
Affiliation.php
2 years ago
AffiliationProduct.php
1 year ago
AffiliationProtocol.php
2 years ago
AffiliationRequest.php
2 years ago
ApiToken.php
3 years ago
BalanceTransaction.php
3 years ago
Brand.php
3 years ago
BulkAction.php
2 years ago
Bump.php
1 year ago
BuyLink.php
2 years ago
CancellationAct.php
3 years ago
CancellationReason.php
3 years ago
Charge.php
1 year ago
Checkout.php
1 year ago
Click.php
2 years ago
Collection.php
2 years ago
CommissionStructure.php
2 years ago
Component.php
3 years ago
Coupon.php
1 year ago
Customer.php
2 years ago
CustomerLink.php
3 years ago
CustomerNotificationProtocol.php
3 years ago
DatabaseModel.php
1 year ago
Discount.php
1 year ago
Download.php
3 years ago
Event.php
3 years ago
Export.php
2 years ago
Form.php
3 years ago
Fulfillment.php
3 years ago
FulfillmentItem.php
1 year ago
GalleryItem.php
1 year ago
GalleryItemAttachment.php
1 year ago
GalleryItemMedia.php
1 year ago
GalleryItemProductMedia.php
1 year ago
IncomingWebhook.php
1 year ago
Integration.php
2 years ago
Invoice.php
1 year ago
License.php
2 years ago
LineItem.php
1 year ago
ManualPaymentMethod.php
1 year ago
Media.php
1 year ago
Model.php
1 year ago
ModelInterface.php
3 years ago
Order.php
2 years ago
OrderProtocol.php
3 years ago
PaymentInstrument.php
1 year ago
PaymentIntent.php
3 years ago
PaymentMethod.php
1 year ago
Payout.php
2 years ago
PayoutGroup.php
2 years ago
Period.php
3 years ago
PortalProtocol.php
3 years ago
PortalSession.php
3 years ago
Price.php
1 year ago
Processor.php
1 year ago
Product.php
1 year ago
ProductCollection.php
1 year ago
ProductGroup.php
2 years ago
ProductMedia.php
1 year ago
Promotion.php
1 year ago
ProvisionalAccount.php
2 years ago
Purchase.php
2 years ago
Referral.php
2 years ago
ReferralItem.php
2 years ago
Refund.php
3 years ago
RegisteredWebhook.php
2 years ago
ReturnItem.php
2 years ago
ReturnReason.php
2 years ago
ReturnRequest.php
2 years ago
ShippingMethod.php
3 years ago
ShippingProfile.php
3 years ago
ShippingProtocol.php
3 years ago
ShippingRate.php
3 years ago
ShippingZone.php
3 years ago
Statistic.php
3 years ago
Subscription.php
1 year ago
SubscriptionProtocol.php
3 years ago
TaxOverride.php
2 years ago
TaxProtocol.php
3 years ago
TaxRegistration.php
3 years ago
TaxZone.php
3 years ago
Upload.php
3 years ago
Upsell.php
1 year ago
UpsellFunnel.php
2 years ago
User.php
2 years ago
Variant.php
1 year ago
VariantOption.php
2 years ago
VariantOptionValue.php
1 year ago
VariantValue.php
2 years ago
VerificationCode.php
3 years ago
Webhook.php
1 year ago
WebhookRegistration.php
2 years ago
Price.php
363 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Models\Traits\HasProduct; |
| 6 | use SureCart\Support\Currency; |
| 7 | |
| 8 | /** |
| 9 | * Price model |
| 10 | */ |
| 11 | class Price extends Model { |
| 12 | use HasProduct; |
| 13 | |
| 14 | /** |
| 15 | * Rest API endpoint |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $endpoint = 'prices'; |
| 20 | |
| 21 | /** |
| 22 | * Object name |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $object_name = 'price'; |
| 27 | |
| 28 | /** |
| 29 | * Is this cachable? |
| 30 | * |
| 31 | * @var boolean |
| 32 | */ |
| 33 | protected $cachable = true; |
| 34 | |
| 35 | /** |
| 36 | * Clear cache when products are updated. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | protected $cache_key = 'products'; |
| 41 | |
| 42 | /** |
| 43 | * Set the product attribute |
| 44 | * |
| 45 | * @param array $attributes Attributes to update. |
| 46 | * |
| 47 | * @return $this|false |
| 48 | */ |
| 49 | protected function create( $attributes = array() ) { |
| 50 | // update parent. |
| 51 | $updated = parent::create( $attributes ); |
| 52 | |
| 53 | // sync the product. |
| 54 | $this->sync(); |
| 55 | |
| 56 | // return. |
| 57 | return $updated; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Update a model |
| 62 | * |
| 63 | * @param array $attributes Attributes to update. |
| 64 | * |
| 65 | * @return $this|false |
| 66 | */ |
| 67 | protected function update( $attributes = array() ) { |
| 68 | // update parent. |
| 69 | $updated = parent::update( $attributes ); |
| 70 | |
| 71 | // sync the product. |
| 72 | $this->sync(); |
| 73 | |
| 74 | // return. |
| 75 | return $updated; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Update a model |
| 80 | * |
| 81 | * @param string $id ID to delete. |
| 82 | * |
| 83 | * @return $this|false |
| 84 | */ |
| 85 | protected function delete( $id = '' ) { |
| 86 | // find price as we need the product id. |
| 87 | $this->find( $id ); |
| 88 | |
| 89 | // update parent. |
| 90 | $response = parent::delete( $id ); |
| 91 | |
| 92 | // sync product. |
| 93 | $this->sync(); |
| 94 | |
| 95 | // return. |
| 96 | return $response; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Sync the product |
| 101 | * |
| 102 | * @return $this |
| 103 | */ |
| 104 | protected function sync() { |
| 105 | $product = Product::sync( $this->product_id ); |
| 106 | |
| 107 | // if the product is an error, return it. |
| 108 | if ( is_wp_error( $product ) ) { |
| 109 | return $product; |
| 110 | } |
| 111 | |
| 112 | return $this; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the display amount attribute |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | public function getDisplayAmountAttribute() { |
| 121 | if ( $this->ad_hoc ) { |
| 122 | return esc_html__( 'Custom Amount', 'surecart' ); |
| 123 | } |
| 124 | if ( empty( $this->amount ) && empty( $this->recurring_interval ) ) { |
| 125 | return apply_filters( 'surecart/display_amount/free', esc_html__( 'Free', 'surecart' ) ); |
| 126 | } |
| 127 | return Currency::format( $this->amount, $this->currency ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the formatted amount attribute |
| 132 | * |
| 133 | * @return string |
| 134 | */ |
| 135 | public function getConvertedAmountAttribute() { |
| 136 | if ( $this->is_zero_decimal || empty( $this->amount ) ) { |
| 137 | return $this->amount; |
| 138 | } |
| 139 | return $this->amount / 100; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get the converted_ad_hoc_min_amount attribute |
| 144 | * |
| 145 | * @return string |
| 146 | */ |
| 147 | public function getConvertedAdHocMinAmountAttribute() { |
| 148 | if ( $this->is_zero_decimal || empty( $this->ad_hoc_min_amount ) ) { |
| 149 | return $this->ad_hoc_min_amount; |
| 150 | } |
| 151 | return $this->ad_hoc_min_amount / 100; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the converted_ad_hoc_max_amount attribute |
| 156 | * |
| 157 | * @return string |
| 158 | */ |
| 159 | public function getConvertedAdHocMaxAmountAttribute() { |
| 160 | if ( $this->is_zero_decimal || empty( $this->ad_hoc_max_amount ) ) { |
| 161 | return $this->ad_hoc_max_amount; |
| 162 | } |
| 163 | return $this->ad_hoc_max_amount / 100; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * The the scratch display amount attribute. |
| 168 | * |
| 169 | * @return string |
| 170 | */ |
| 171 | public function getScratchDisplayAmountAttribute() { |
| 172 | return empty( $this->scratch_amount ) ? '' : Currency::format( $this->scratch_amount, $this->currency ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the display price attribute |
| 177 | * |
| 178 | * @return string |
| 179 | */ |
| 180 | public function getIsOnSaleAttribute() { |
| 181 | if ( $this->ad_hoc ) { |
| 182 | return false; |
| 183 | } |
| 184 | return empty( $this->scratch_amount ) ? false : $this->scratch_amount > $this->amount; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get the trial display text attribute |
| 189 | * |
| 190 | * @return string |
| 191 | */ |
| 192 | public function getTrialTextAttribute() { |
| 193 | return $this->trial_duration_days ? sprintf( |
| 194 | // translators: %s is the number of days. |
| 195 | _n( |
| 196 | 'Starting in %s day.', |
| 197 | 'Starting in %s days.', |
| 198 | $this->trial_duration_days, |
| 199 | 'surecart' |
| 200 | ), |
| 201 | $this->trial_duration_days |
| 202 | ) |
| 203 | : ''; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get the setup fee text attribute |
| 208 | */ |
| 209 | public function getSetupFeeTextAttribute() { |
| 210 | if ( empty( $this->setup_fee_enabled ) || empty( $this->setup_fee_amount ) ) { |
| 211 | return ''; |
| 212 | } |
| 213 | return sprintf( |
| 214 | // translators: %1$1s is the setup fee amount, %2$2s is the setup fee name. |
| 215 | __( '%1$1s %2$2s.', 'surecart' ), |
| 216 | Currency::format( $this->setup_fee_amount, $this->currency ), |
| 217 | $this->setup_fee_name ?? __( 'Setup Fee', 'surecart' ) |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get the payments text attribute |
| 223 | * |
| 224 | * @return string |
| 225 | */ |
| 226 | public function getPaymentsTextAttribute() { |
| 227 | return ! empty( $this->recurring_period_count ) && $this->recurring_period_count > 1 ? sprintf( |
| 228 | // translators: %d is the number of payments. |
| 229 | _n( |
| 230 | '%d payment', |
| 231 | '%d payments', |
| 232 | $this->recurring_period_count, |
| 233 | 'surecart' |
| 234 | ), |
| 235 | $this->recurring_period_count |
| 236 | ) : ''; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get the interval text attribute |
| 241 | * |
| 242 | * @return string |
| 243 | */ |
| 244 | public function getIntervalTextAttribute() { |
| 245 | $intervals = array( |
| 246 | 'day' => [ |
| 247 | 'single' => __( 'day', 'surecart' ), |
| 248 | 'plural' => __( 'days', 'surecart' ), |
| 249 | ], |
| 250 | 'week' => [ |
| 251 | 'single' => __( 'week', 'surecart' ), |
| 252 | 'plural' => __( 'weeks', 'surecart' ), |
| 253 | ], |
| 254 | 'month' => [ |
| 255 | 'single' => __( 'month', 'surecart' ), |
| 256 | 'plural' => __( 'months', 'surecart' ), |
| 257 | ], |
| 258 | 'year' => [ |
| 259 | 'single' => __( 'year', 'surecart' ), |
| 260 | 'plural' => __( 'years', 'surecart' ), |
| 261 | ], |
| 262 | ); |
| 263 | |
| 264 | $key = (int) $this->recurring_interval_count > 1 ? 'plural' : 'single'; |
| 265 | |
| 266 | if ( empty( $intervals[ $this->recurring_interval ][ $key ] ) ) { |
| 267 | return ''; |
| 268 | } |
| 269 | |
| 270 | return sprintf( |
| 271 | // translators: %1$d is the number of intervals, %2$s is the interval. |
| 272 | _n( 'every %2$2s', 'every %1$1s %2$2s', $this->recurring_interval_count, 'surecart' ), |
| 273 | (int) $this->recurring_interval_count, |
| 274 | $intervals[ $this->recurring_interval ][ $key ], |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Get the interval text attribute |
| 280 | * |
| 281 | * @return string |
| 282 | */ |
| 283 | public function getShortIntervalTextAttribute() { |
| 284 | $intervals = array( |
| 285 | 'day' => [ |
| 286 | 'single' => __( 'day', 'surecart' ), |
| 287 | 'plural' => __( 'days', 'surecart' ), |
| 288 | ], |
| 289 | 'week' => [ |
| 290 | 'single' => __( 'wk', 'surecart' ), |
| 291 | 'plural' => __( 'wks', 'surecart' ), |
| 292 | ], |
| 293 | 'month' => [ |
| 294 | 'single' => __( 'mo', 'surecart' ), |
| 295 | 'plural' => __( 'mos', 'surecart' ), |
| 296 | ], |
| 297 | 'year' => [ |
| 298 | 'single' => __( 'yr', 'surecart' ), |
| 299 | 'plural' => __( 'yrs', 'surecart' ), |
| 300 | ], |
| 301 | ); |
| 302 | |
| 303 | $key = (int) $this->recurring_interval_count > 1 ? 'plural' : 'single'; |
| 304 | |
| 305 | if ( empty( $intervals[ $this->recurring_interval ][ $key ] ) ) { |
| 306 | return ''; |
| 307 | } |
| 308 | |
| 309 | return sprintf( |
| 310 | // translators: %1$d is the number of intervals, %2$s is the interval. |
| 311 | _n( '/ %2$2s', '/ %1$1s %2$2s', $this->recurring_interval_count, 'surecart' ), |
| 312 | (int) $this->recurring_interval_count, |
| 313 | $intervals[ $this->recurring_interval ][ $key ], |
| 314 | ); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Get the interval count text attribute |
| 319 | * |
| 320 | * @return string |
| 321 | */ |
| 322 | public function getShortIntervalCountTextAttribute() { |
| 323 | if ( (int) $this->recurring_period_count < 2 ) { |
| 324 | return ''; |
| 325 | } |
| 326 | |
| 327 | // translators: %d is the number of intervals. |
| 328 | return sprintf( __( ' x %s', 'surecart' ), (int) $this->recurring_period_count ); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Get the interval count text attribute |
| 333 | * |
| 334 | * @return string |
| 335 | */ |
| 336 | public function getIntervalCountTextAttribute() { |
| 337 | if ( (int) $this->recurring_period_count < 2 ) { |
| 338 | return ''; |
| 339 | } |
| 340 | |
| 341 | // translators: %d is the number of intervals. |
| 342 | return sprintf( __( '(%d payments)', 'surecart' ), (int) $this->recurring_period_count ); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Is this a zero decimal currency price? |
| 347 | * |
| 348 | * @return boolean |
| 349 | */ |
| 350 | public function getIsZeroDecimalAttribute() { |
| 351 | return Currency::isZeroDecimal( $this->currency ); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Get the currency symbol attribute |
| 356 | * |
| 357 | * @return string |
| 358 | */ |
| 359 | public function getCurrencySymbolAttribute() { |
| 360 | return html_entity_decode( Currency::getCurrencySymbol( $this->currency ) ); |
| 361 | } |
| 362 | } |
| 363 |