PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.1.9
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.1.9
1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 All 178 releases
disco / app / Attributes / Product.php

Product.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.1.9, at app/Attributes/Product.php

655 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore
2 /**
3 * Product Attributes
4 *
5 * @package Disco
6 * @subpackage \App\Attributes
7 */
8
9 namespace Disco\App\Attributes;
10
11 /**
12 * Class Product
13 *
14 * This class provides methods for retrieving various attributes of a WooCommerce product.
15 *
16 * @package Disco
17 * @subpackage \App\Attributes
18 * @author Ohidul Islam <wahid0003@gmail.com>
19 * @link https://webappick.com
20 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
21 * @category Attributes
22 */
23 class Product { //phpcs:ignore
24
25 /**
26 * @var \WC_Product $product Product Object.
27 */
28 private $product;
29
30 /**
31 * @var \WC_Product $parent Parent Product Object.
32 */
33 private $parent;
34
35 /**
36 * ProductInfo constructor.
37 *
38 * @param \WC_Product $product Product Object.
39 */
40 public function __construct( $product ) {
41 $this->product = $product;
42 $this->parent = $product;
43
44 if ( $product->is_type( 'variation' ) ) {
45 return;
46 }
47
48 $parent = wc_get_product( $product->get_parent_id() );
49
50 if ( ! ( $parent instanceof \WC_Product ) ) {
51 return;
52 }
53
54 $this->parent = $parent;
55 }
56
57 /**
58 * Get the ID of the product.
59 *
60 * @return int
61 */
62 public function id() {
63 return $this->product->get_id();
64 }
65
66 /**
67 * Get the ID of the parent product.
68 *
69 * @return int
70 */
71 public function parent_id() {
72 return $this->parent->get_id();
73 }
74
75 /**
76 * Get the SKU of the product.
77 *
78 * @return string
79 */
80 public function sku() {
81 return $this->product->get_sku();
82 }
83
84 /**
85 * Get the SKU of the parent product.
86 *
87 * @return string
88 */
89 public function parent_sku() {
90 return $this->parent->get_sku();
91 }
92
93 /**
94 * Get the title or name of the product.
95 *
96 * @return string
97 */
98 public function title() {
99 return $this->product->get_name();
100 }
101
102 /**
103 * Get the title or name of the parent product.
104 *
105 * @return string
106 */
107 public function parent_title() {
108 return $this->parent->get_name();
109 }
110
111 /**
112 * Get the product's description.
113 *
114 * If the product is a variation and doesn't have its own description, retrieves the parent product's description.
115 *
116 * @return string
117 */
118 public function description() {
119 $description = $this->product->get_description();
120
121 if ( empty( $description ) && $this->product->is_type( 'variation' ) ) {
122 $description = $this->parent->get_description();
123 }
124
125 return $description;
126 }
127
128 /**
129 * Get the product's short description.
130 *
131 * @return string
132 */
133 public function short_description() {
134 return $this->product->get_short_description();
135 }
136
137 /**
138 * Retrieve the attributes of the product.
139 *
140 * @return array
141 */
142 public function attributes() {
143 $attributes = $this->product->get_attributes();
144
145 if ( empty( $attributes ) ) {
146 return array();
147 }
148
149 // Get an array of attribute name without the prefix 'pa_'
150 return array_values(
151 array_map(
152 /**
153 * @param \WC_Product_Attribute $attribute Product attribute object.
154 * @return string
155 */
156 static function( $attribute ) {
157 return str_replace( 'pa_', '', $attribute->get_name() );
158 },
159 $attributes
160 )
161 );
162 }
163
164 /**
165 * Retrieve the category IDs the product belongs to.
166 *
167 * @return array
168 */
169 public function categories() {
170 // Get the product object
171 $product = $this->product;
172
173 // If the product doesn't exist, return an empty array
174 if ( !$product ) {
175 return array();
176 }
177
178 // Get category IDs for the product
179 $category_ids = $product->get_category_ids();
180
181 if ( empty( $category_ids ) ) {
182 return array();
183 }
184
185 $categories = array();
186
187 foreach ( $category_ids as $category_id ) {
188 $term = get_term( $category_id, 'product_cat', OBJECT );
189
190 if ( !is_object( $term ) || is_wp_error( $term ) ) {
191 continue;
192 }
193
194 $categories[] = $term->name;
195 }
196
197 return $categories;
198 }
199
200 /**
201 * Retrieve the tag IDs the product is tagged with.
202 *
203 * @return array
204 */
205 public function tags() {
206 // Get the product object
207 $product = $this->product;
208
209 // If the product doesn't exist, return an empty array
210 if ( !$product ) {
211 return array();
212 }
213
214 // Get tag IDs for the product
215 $tag_ids = $product->get_tag_ids();
216
217 if ( empty( $tag_ids ) ) {
218 return array();
219 }
220
221 $tags = array();
222
223 foreach ( $tag_ids as $tag_id ) {
224 $term = get_term( $tag_id, 'product_tag', OBJECT );
225
226 if ( !is_object( $term ) || is_wp_error( $term ) ) {
227 continue;
228 }
229
230 $tags[] = $term->name;
231 }
232
233 return $tags;
234 }
235
236 /**
237 * Get the product's permalink.
238 *
239 * @return string
240 */
241 public function link() {
242 return $this->product->get_permalink();
243 }
244
245 /**
246 * Get the parent product's permalink.
247 *
248 * @return string
249 */
250 public function parent_link() {
251 return $this->parent->get_permalink();
252 }
253
254 /**
255 * Get the product's add to cart URL.
256 *
257 * @return string
258 */
259 public function add_to_cart_link() {
260 return $this->product->add_to_cart_url();
261 }
262
263 /**
264 * Get the product's availability.
265 *
266 * @return array
267 */
268 public function availability() {
269 return $this->product->get_availability();
270 }
271
272 /**
273 * Get the product's stock quantity.
274 *
275 * @return int|null
276 */
277 public function quantity() {
278 return $this->product->get_stock_quantity();
279 }
280
281 /**
282 * Get the product's stock status.
283 *
284 * @return string
285 */
286 public function stock_status() {
287 return $this->product->get_stock_status();
288 }
289
290 /**
291 * Get the product's weight.
292 *
293 * @return string|null
294 */
295 public function weight() {
296 return $this->product->get_weight();
297 }
298
299 /**
300 * Get the weight unit used in WooCommerce settings.
301 *
302 * @return string
303 */
304 public function weight_unit() {
305 $weight_unit = get_option( 'woocommerce_weight_unit' );// @phpstan-ignore-line
306
307 if ( $weight_unit ) {
308 return $weight_unit;
309 }
310
311 return 'kg';
312 }
313
314 /**
315 * Get the product's width.
316 *
317 * @return string|null
318 */
319 public function width() {
320 return $this->product->get_width();
321 }
322
323 /**
324 * Get the product's height.
325 *
326 * @return string|null
327 */
328 public function height() {
329 return $this->product->get_height();
330 }
331
332 /**
333 * Get the product's length.
334 *
335 * @return string|null
336 */
337 public function length() {
338 return $this->product->get_length();
339 }
340
341 /**
342 * Get the product type.
343 *
344 * @return string
345 */
346 public function type() {
347 return $this->product->get_type();
348 }
349
350 /**
351 * Get the product's catalog visibility.
352 *
353 * @return string
354 */
355 public function visibility() {
356 return $this->product->get_catalog_visibility();
357 }
358
359 /**
360 * Get the total rating count of the product.
361 *
362 * @return int
363 */
364 public function rating_total() {
365 return $this->product->get_rating_count();
366 }
367
368 /**
369 * Get the average rating of the product.
370 *
371 * @return float
372 */
373 public function rating_average() {
374 return $this->product->get_average_rating();
375 }
376
377 /**
378 * Get the author's name of the product.
379 *
380 * @return string
381 */
382 public function author_name() {
383 $post = get_post( $this->product->get_id() );
384
385 if ( ! ( $post instanceof \WP_Post ) ) {
386 return '';
387 }
388
389 return get_the_author_meta( 'display_name', (int) $post->post_author );
390 }
391
392 /**
393 * Get the email of the author of the product.
394 *
395 * @return string
396 */
397 public function author_email() {
398 $post = get_post( $this->product->get_id() );
399
400 if ( ! ( $post instanceof \WP_Post ) ) {
401 return '';
402 }
403
404 return get_the_author_meta( 'user_email', (int) $post->post_author );
405 }
406
407 /**
408 * Get the date when the product was created.
409 *
410 * @return string
411 */
412 public function date_created() {
413 $date_created = $this->product->get_date_created();
414
415 if ( $date_created === null && $this->product->is_type( 'variation' ) ) {
416 $date_created = $this->parent->get_date_created();
417 }
418
419 if ( ! ( $date_created instanceof \WC_DateTime ) ) {
420 return '';
421 }
422
423 return $date_created->date_i18n();
424 }
425
426 /**
427 * Get the date when the product was last updated.
428 *
429 * @return string
430 */
431 public function date_updated() {
432 $date_created = $this->product->get_date_modified();
433
434 if ( $date_created === null && $this->product->is_type( 'variation' ) ) {
435 $date_created = $this->parent->get_date_modified();
436 }
437
438 if ( ! ( $date_created instanceof \WC_DateTime ) ) {
439 return '';
440 }
441
442 return $date_created->date_i18n();
443 }
444
445 /**
446 * Get the current status of the product.
447 *
448 * @return string
449 */
450 public function product_status() {
451 return $this->product->get_status();
452 }
453
454 /**
455 * Determine if the product is featured.
456 *
457 * @return string
458 */
459 public function featured_status() {
460 if ( $this->product->is_featured() ) {
461 return 'yes';
462 }
463
464 return 'no';
465 }
466
467 /**
468 * Get the currency used for the product.
469 *
470 * @return string
471 */
472 public function currency() {
473 return get_woocommerce_currency();
474 }
475
476 /**
477 * Get the product's regular price.
478 *
479 * @return string
480 */
481 public function regular_price() {
482 return $this->product->get_regular_price();
483 }
484
485 /**
486 * Get the product's regular price.
487 *
488 * @return string
489 */
490 public function price() {
491 return $this->product->get_price();
492 }
493
494 /**
495 * Get the product's sale price.
496 *
497 * @return string
498 */
499 public function sale_price() {
500 return $this->product->get_sale_price();
501 }
502
503 /**
504 * Get the product's regular price including tax.
505 *
506 * @return float|string
507 */
508 public function regular_price_with_tax() {
509 return wc_get_price_including_tax(
510 $this->product,
511 array(
512 'price' => $this->product->get_regular_price(),
513 'qty' => 1,
514 )
515 );
516 }
517
518 /**
519 * Get the product's price including tax.
520 *
521 * @return float|string
522 */
523 public function price_with_tax() {
524 return wc_get_price_including_tax(
525 $this->product,
526 array(
527 'price' => $this->product->get_price(),
528 'qty' => 1,
529 )
530 );
531 }
532
533 /**
534 * Get the product's sale price including tax.
535 *
536 * @return float|string
537 */
538 public function sale_price_with_tax() {
539 return wc_get_price_including_tax(
540 $this->product,
541 array(
542 'price' => $this->product->get_sale_price(),
543 'qty' => 1,
544 )
545 );
546 }
547
548 /**
549 * Get the starting date of the product's sale.
550 *
551 * @return string
552 */
553 public function sale_price_sdate() {
554 $sdate = $this->product->get_date_on_sale_from();
555
556 if ( ! ( $sdate instanceof \WC_DateTime ) ) {
557 return '';
558 }
559
560 return $sdate->date_i18n();
561 }
562
563 /**
564 * Get the ending date of the product's sale.
565 *
566 * @return string|false
567 */
568 public function sale_price_edate() {
569 $edate = $this->product->get_date_on_sale_to();
570
571 if ( ! ( $edate instanceof \WC_DateTime ) ) {
572 return '';
573 }
574
575 return $edate->date_i18n();
576 }
577
578 /**
579 * Get the tax class of the product.
580 *
581 * @return string
582 */
583 public function tax_class() {
584 $tax_class = $this->product->get_tax_class();
585
586 if ( empty( $tax_class ) ) {
587 $tax_class = 'standard';
588 }
589
590 return $tax_class;
591 }
592
593 /**
594 * Get the tax status of the product.
595 *
596 * @return string
597 */
598 public function tax_status() {
599 return $this->product->get_tax_status();
600 }
601
602 /**
603 * Get the shipping class of the product.
604 *
605 * @return string
606 */
607 public function shipping_class() {
608 return $this->product->get_shipping_class();
609 }
610
611 /**
612 * Get a specific global attribute of the product.
613 *
614 * @param string $attribute Name of the attribute to retrieve.
615 * @return array
616 */
617 public function get_attribute( $attribute = '' ) {
618 $options = $this->product->get_attribute( $attribute );
619
620 // If the attribute is empty, return an empty array
621 if ( empty( $options ) ) {
622 return array();
623 }
624
625 return explode( ',', $options );
626 }
627
628 /**
629 * Get a custom attribute or meta of the product.
630 *
631 * If the product is a variation and doesn't have the specified meta, retrieve it from the parent product.
632 *
633 * @param string $meta Name of the meta to retrieve.
634 * @return mixed
635 */
636 public function get_product_meta( $meta = '' ) {
637 // Remove the underscore prefix from the meta-key.
638 $first = $meta[0];
639
640 if ( $first === '_' ) {
641 $meta = substr( $meta, 1 );
642 }
643
644 $value = $this->product->get_meta( $meta, true );
645
646 if ( $value === '' && $this->product->is_type( 'variation' ) ) {
647 $value = $this->parent->get_meta( $meta, true );
648 }
649
650 return $value;
651 }
652 // ... End of Class ...
653
654 }
655