PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.1.5
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.1.5
4.7.2 4.7.1 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 All 281 releases
surecart / app / src / Models / LineItem.php
LineItem.php
222 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Models;
4
5 use SureCart\Models\Traits\HasCheckout;
6 use SureCart\Models\Traits\HasPrice;
7 use SureCart\Support\Currency;
8 use SureCart\Models\Traits\HasProduct;
9
10 /**
11 * Price model
12 */
13 class LineItem extends Model {
14 use HasPrice;
15 use HasCheckout;
16 use HasProduct;
17
18 /**
19 * Rest API endpoint
20 *
21 * @var string
22 */
23 protected $endpoint = 'line_items';
24
25 /**
26 * Object name
27 *
28 * @var string
29 */
30 protected $object_name = 'line_item';
31
32 /**
33 * Set the variant attribute.
34 *
35 * @param string $value Variant properties.
36 * @return void
37 */
38 public function setVariantAttribute( $value ) {
39 $this->setRelation( 'variant', $value, Variant::class );
40 }
41
42 /**
43 * Upsell a line item.
44 *
45 * @param array $attributes The attributes to update.
46 * @return \WP_Error|mixed
47 */
48 protected function upsell( $attributes = [] ) {
49 if ( $this->fireModelEvent( 'upselling' ) === false ) {
50 return false;
51 }
52
53 $updated = $this->makeRequest(
54 [
55 'method' => 'POST',
56 'query' => $this->query,
57 'body' => [
58 $this->object_name => $attributes,
59 ],
60 ],
61 'line_items/upsell'
62 );
63
64 if ( $this->isError( $updated ) ) {
65 return $updated;
66 }
67
68 $this->resetAttributes();
69
70 $this->fill( $updated );
71
72 $this->fireModelEvent( 'upsold' );
73
74 // clear account cache.
75 if ( $this->cachable || $this->clears_account_cache ) {
76 \SureCart::account()->clearCache();
77 }
78
79 return $this;
80 }
81
82 /**
83 * Get the currency attribute.
84 *
85 * TODO: Remove this method once currency added on line item.
86 *
87 * @return string
88 */
89 public function getCurrencyAttribute() {
90 return $this->checkout->currency ?? \SureCart::account()->currency;
91 }
92
93 /**
94 * Get the display ad hoc amount attribute.
95 *
96 * @return string
97 */
98 public function getAdHocDisplayAmountAttribute() {
99 return ! empty( $this->ad_hoc_amount ) ? Currency::format( $this->ad_hoc_amount, $this->currency ) : '';
100 }
101
102 /**
103 * Get the bump amount display attribute.
104 *
105 * @return string
106 */
107 public function getBumpDisplayAmountAttribute() {
108 return ! empty( $this->bump_amount ) ? Currency::format( $this->bump_amount, $this->currency ) : '';
109 }
110
111 /**
112 * Get the display scratch amount attribute.
113 *
114 * @return string
115 */
116 public function getScratchDisplayAmountAttribute() {
117 return ! empty( $this->scratch_amount ) ? Currency::format( $this->scratch_amount, $this->currency ) : '';
118 }
119
120 /**
121 * Get the display tax amount attribute.
122 *
123 * @return string
124 */
125 public function getTaxDisplayAmountAttribute() {
126 return ! empty( $this->tax_amount ) ? Currency::format( $this->tax_amount, $this->currency ) : '';
127 }
128
129 /**
130 * Get the display subtotal amount attribute.
131 *
132 * @return string
133 */
134 public function getSubtotalDisplayAmountAttribute() {
135 return Currency::format( (int) $this->subtotal_amount, $this->currency );
136 }
137
138 /**
139 * Get the display total amount attribute.
140 *
141 * @return string
142 */
143 public function getTotalDisplayAmountAttribute() {
144 return Currency::format( (int) $this->total_amount, $this->currency );
145 }
146
147 /**
148 * Get the display full amount attribute.
149 *
150 * @return string
151 */
152 public function getFullDisplayAmountAttribute() {
153 return ! empty( $this->full_amount ) ? Currency::format( $this->full_amount, $this->currency ) : '';
154 }
155
156 /**
157 * Get the display trial amount attribute.
158 *
159 * @return string
160 */
161 public function getTrialDisplayAmountAttribute() {
162 return ! empty( $this->trial_amount ) ? Currency::format( $this->trial_amount, $this->currency ) : '';
163 }
164
165 /**
166 * Get the line item image.
167 */
168 public function getImageAttribute() {
169 // if we have a variant, use the variant image.
170 if ( ! empty( $this->variant ) && is_a( $this->variant, Variant::class ) && ! empty( (array) $this->variant->line_item_image ) ) {
171 return $this->variant->line_item_image;
172 }
173
174 // if we have a product, use the product image.
175 if ( isset( $this->price->product->line_item_image ) ) {
176 return $this->price->product->line_item_image;
177 }
178
179 return null;
180 }
181
182 /**
183 * Get the converted discount amount attribute.
184 *
185 * @return string
186 */
187 public function getConvertedDiscountAmountAttribute() {
188 if ( $this->is_zero_decimal || empty( $this->discount_amount ) ) {
189 return $this->discount_amount;
190 }
191 return $this->discount_amount / 100;
192 }
193
194 /**
195 * Purchasable status display
196 *
197 * @return string
198 */
199 public function getPurchasableStatusDisplayAttribute() {
200 if ( 'purchasable' === $this->purchasable_status ) {
201 return;
202 }
203
204 // translations for purchaseable status.
205 $translations = array(
206 'price_gone' => __( 'No longer available', 'surecart' ),
207 'price_old_version' => __( 'Price has changed', 'surecart' ),
208 'variant_missing' => __( 'Options no longer available', 'surecart' ),
209 'variant_old_version' => __( 'Price has changed', 'surecart' ),
210 'variant_gone' => __( 'Item no longer available', 'surecart' ),
211 'out_of_stock' => __( 'Out of stock', 'surecart' ),
212 'exceeds_purchase_limit' => __( 'Exceeds purchase limit', 'surecart' ),
213 );
214
215 if ( $this->quantity > 1 ) {
216 $translations['out_of_stock'] = __( 'Quantity unavailable', 'surecart' );
217 }
218
219 return $translations[ $this->purchasable_status ] ?? '';
220 }
221 }
222