PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
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 1.3.45 All 177 releases
disco / app / Attributes / Product.php

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

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