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