PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.0-beta2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.0-beta2
4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Models / Price.php
surecart / app / src / Models Last commit date
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 4 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 4 years ago Brand.php 4 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 4 years ago Checkout.php 1 year ago Click.php 2 years ago Collection.php 2 years ago CommissionStructure.php 2 years ago Component.php 4 years ago Coupon.php 1 year ago Customer.php 2 years ago CustomerLink.php 4 years ago CustomerNotificationProtocol.php 4 years ago DatabaseModel.php 1 year ago Discount.php 1 year ago Download.php 4 years ago Event.php 4 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 2 years ago Integration.php 2 years ago Invoice.php 4 years ago License.php 2 years ago LineItem.php 1 year ago ManualPaymentMethod.php 3 years ago Media.php 1 year ago Model.php 1 year ago ModelInterface.php 4 years ago Order.php 2 years ago OrderProtocol.php 4 years ago PaymentIntent.php 4 years ago PaymentMethod.php 4 years ago Payout.php 2 years ago PayoutGroup.php 2 years ago Period.php 3 years ago PortalProtocol.php 4 years ago PortalSession.php 4 years ago Price.php 1 year ago Processor.php 3 years ago Product.php 1 year ago ProductCollection.php 1 year ago ProductGroup.php 2 years ago ProductMedia.php 1 year ago Promotion.php 4 years ago ProvisionalAccount.php 2 years ago Purchase.php 2 years ago Referral.php 2 years ago ReferralItem.php 2 years ago Refund.php 4 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 4 years ago Subscription.php 1 year ago SubscriptionProtocol.php 4 years ago TaxOverride.php 2 years ago TaxProtocol.php 4 years ago TaxRegistration.php 4 years ago TaxZone.php 4 years ago Upload.php 4 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 2 years ago WebhookRegistration.php 2 years ago
Price.php
364 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_updated_at';
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 already attached, and syncable, use that. Otherwise, find it.
108 $product = ! empty( $this->product->id ) ? $this->product : Product::withSyncableExpands()->where( array( 'cached' => $args['cached'] ) )->find( $this->product_id );
109 if ( is_wp_error( $product ) ) {
110 return $product;
111 }
112
113 return $this;
114 }
115
116 /**
117 * Get the display amount attribute
118 *
119 * @return string
120 */
121 public function getDisplayAmountAttribute() {
122 if ( $this->ad_hoc ) {
123 return esc_html__( 'Custom Amount', 'surecart' );
124 }
125 if ( empty( $this->amount ) && empty( $this->recurring_interval ) ) {
126 return apply_filters( 'surecart/display_amount/free', esc_html__( 'Free', 'surecart' ) );
127 }
128 return Currency::format( $this->amount, $this->currency );
129 }
130
131 /**
132 * Get the formatted amount attribute
133 *
134 * @return string
135 */
136 public function getConvertedAmountAttribute() {
137 if ( $this->is_zero_decimal || empty( $this->amount ) ) {
138 return $this->amount;
139 }
140 return $this->amount / 100;
141 }
142
143 /**
144 * Get the converted_ad_hoc_min_amount attribute
145 *
146 * @return string
147 */
148 public function getConvertedAdHocMinAmountAttribute() {
149 if ( $this->is_zero_decimal || empty( $this->ad_hoc_min_amount ) ) {
150 return $this->ad_hoc_min_amount;
151 }
152 return $this->ad_hoc_min_amount / 100;
153 }
154
155 /**
156 * Get the converted_ad_hoc_max_amount attribute
157 *
158 * @return string
159 */
160 public function getConvertedAdHocMaxAmountAttribute() {
161 if ( $this->is_zero_decimal || empty( $this->ad_hoc_max_amount ) ) {
162 return $this->ad_hoc_max_amount;
163 }
164 return $this->ad_hoc_max_amount / 100;
165 }
166
167 /**
168 * The the scratch display amount attribute.
169 *
170 * @return string
171 */
172 public function getScratchDisplayAmountAttribute() {
173 return empty( $this->scratch_amount ) ? '' : Currency::format( $this->scratch_amount, $this->currency );
174 }
175
176 /**
177 * Get the display price attribute
178 *
179 * @return string
180 */
181 public function getIsOnSaleAttribute() {
182 if ( $this->ad_hoc ) {
183 return false;
184 }
185 return empty( $this->scratch_amount ) ? false : $this->scratch_amount > $this->amount;
186 }
187
188 /**
189 * Get the trial display text attribute
190 *
191 * @return string
192 */
193 public function getTrialTextAttribute() {
194 return $this->trial_duration_days ? sprintf(
195 // translators: %s is the number of days.
196 _n(
197 'Starting in %s day.',
198 'Starting in %s days.',
199 $this->trial_duration_days,
200 'surecart'
201 ),
202 $this->trial_duration_days
203 )
204 : '';
205 }
206
207 /**
208 * Get the setup fee text attribute
209 */
210 public function getSetupFeeTextAttribute() {
211 if ( empty( $this->setup_fee_enabled ) || empty( $this->setup_fee_amount ) ) {
212 return '';
213 }
214 return sprintf(
215 // translators: %1$1s is the setup fee amount, %2$2s is the setup fee name.
216 __( '%1$1s %2$2s.', 'surecart' ),
217 Currency::format( $this->setup_fee_amount, $this->currency ),
218 $this->setup_fee_name ?? __( 'Setup Fee', 'surecart' )
219 );
220 }
221
222 /**
223 * Get the payments text attribute
224 *
225 * @return string
226 */
227 public function getPaymentsTextAttribute() {
228 return ! empty( $this->recurring_period_count ) && $this->recurring_period_count > 1 ? sprintf(
229 // translators: %d is the number of payments.
230 _n(
231 '%d payment',
232 '%d payments',
233 $this->recurring_period_count,
234 'your_text_domain'
235 ),
236 $this->recurring_period_count
237 ) : '';
238 }
239
240 /**
241 * Get the interval text attribute
242 *
243 * @return string
244 */
245 public function getIntervalTextAttribute() {
246 $intervals = array(
247 'day' => [
248 'single' => __( 'day', 'surecart' ),
249 'plural' => __( 'days', 'surecart' ),
250 ],
251 'week' => [
252 'single' => __( 'week', 'surecart' ),
253 'plural' => __( 'weeks', 'surecart' ),
254 ],
255 'month' => [
256 'single' => __( 'month', 'surecart' ),
257 'plural' => __( 'months', 'surecart' ),
258 ],
259 'year' => [
260 'single' => __( 'year', 'surecart' ),
261 'plural' => __( 'years', 'surecart' ),
262 ],
263 );
264
265 $key = (int) $this->recurring_interval_count > 1 ? 'plural' : 'single';
266
267 if ( empty( $intervals[ $this->recurring_interval ][ $key ] ) ) {
268 return '';
269 }
270
271 return sprintf(
272 // translators: %1$d is the number of intervals, %2$s is the interval.
273 _n( 'every %2$2s', 'every %1$1s %2$2s', $this->recurring_interval_count, 'surecart' ),
274 (int) $this->recurring_interval_count,
275 $intervals[ $this->recurring_interval ][ $key ],
276 );
277 }
278
279 /**
280 * Get the interval text attribute
281 *
282 * @return string
283 */
284 public function getShortIntervalTextAttribute() {
285 $intervals = array(
286 'day' => [
287 'single' => __( 'day', 'surecart' ),
288 'plural' => __( 'days', 'surecart' ),
289 ],
290 'week' => [
291 'single' => __( 'wk', 'surecart' ),
292 'plural' => __( 'wks', 'surecart' ),
293 ],
294 'month' => [
295 'single' => __( 'mo', 'surecart' ),
296 'plural' => __( 'mos', 'surecart' ),
297 ],
298 'year' => [
299 'single' => __( 'yr', 'surecart' ),
300 'plural' => __( 'yrs', 'surecart' ),
301 ],
302 );
303
304 $key = (int) $this->recurring_interval_count > 1 ? 'plural' : 'single';
305
306 if ( empty( $intervals[ $this->recurring_interval ][ $key ] ) ) {
307 return '';
308 }
309
310 return sprintf(
311 // translators: %1$d is the number of intervals, %2$s is the interval.
312 _n( '/ %2$2s', '/ %1$1s %2$2s', $this->recurring_interval_count, 'surecart' ),
313 (int) $this->recurring_interval_count,
314 $intervals[ $this->recurring_interval ][ $key ],
315 );
316 }
317
318 /**
319 * Get the interval count text attribute
320 *
321 * @return string
322 */
323 public function getShortIntervalCountTextAttribute() {
324 if ( (int) $this->recurring_period_count < 2 ) {
325 return '';
326 }
327
328 // translators: %d is the number of intervals.
329 return sprintf( __( ' x %s', 'surecart' ), (int) $this->recurring_period_count );
330 }
331
332 /**
333 * Get the interval count text attribute
334 *
335 * @return string
336 */
337 public function getIntervalCountTextAttribute() {
338 if ( (int) $this->recurring_period_count < 2 ) {
339 return '';
340 }
341
342 // translators: %d is the number of intervals.
343 return sprintf( __( '(%d payments)', 'surecart' ), (int) $this->recurring_period_count );
344 }
345
346 /**
347 * Is this a zero decimal currency price?
348 *
349 * @return boolean
350 */
351 public function getIsZeroDecimalAttribute() {
352 return Currency::isZeroDecimal( $this->currency );
353 }
354
355 /**
356 * Get the currency symbol attribute
357 *
358 * @return string
359 */
360 public function getCurrencySymbolAttribute() {
361 return html_entity_decode( Currency::getCurrencySymbol( $this->currency ) );
362 }
363 }
364