PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.13
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.13
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.2.13, at app/Attributes/Product.php

671 lines 12.7 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 $attribute Product attribute object.
154 * @return string
155 */
156 static function ( $attribute ) {
157 // Check if the attribute is an object and has the method `get_name`
158 if ( is_object( $attribute ) && method_exists( $attribute, 'get_name' ) ) {
159 return str_replace( 'pa_', '', $attribute->get_name() );
160 }
161
162 // If it's a string, return it as-is after removing the prefix 'pa_'
163 if ( is_string( $attribute ) ) {
164 return str_replace( 'pa_', '', $attribute );
165 }
166
167 // Return an empty string for unexpected cases
168 return '';
169 },
170 $attributes
171 )
172 );
173 }
174
175 /**
176 * Retrieve the category IDs the product belongs to.
177 *
178 * @return array
179 */
180 public function categories() {
181 // Get the product object
182 $product = $this->product;
183
184 // If the product doesn't exist, return an empty array
185 if ( !$product ) {
186 return array();
187 }
188
189 // Get category IDs for the product
190 $category_ids = $product->get_category_ids();
191
192 if ( empty( $category_ids ) ) {
193 return array();
194 }
195
196 $categories = array();
197
198 foreach ( $category_ids as $category_id ) {
199 $term = get_term( $category_id, 'product_cat', OBJECT );
200
201 if ( !is_object( $term ) || is_wp_error( $term ) ) {
202 continue;
203 }
204
205 $categories[] = $term->name;
206 }
207
208 return $categories;
209 }
210
211 /**
212 * Retrieve the tag IDs the product is tagged with.
213 *
214 * @return array
215 */
216 public function tags() {
217 // Get the product object
218 $product = $this->product;
219
220 // If the product doesn't exist, return an empty array
221 if ( !$product ) {
222 return array();
223 }
224
225 // Get tag IDs for the product
226 $tag_ids = $product->get_tag_ids();
227
228 if ( empty( $tag_ids ) ) {
229 return array();
230 }
231
232 $tags = array();
233
234 foreach ( $tag_ids as $tag_id ) {
235 $term = get_term( $tag_id, 'product_tag', OBJECT );
236
237 if ( !is_object( $term ) || is_wp_error( $term ) ) {
238 continue;
239 }
240
241 $tags[] = $term->name;
242 }
243
244 return $tags;
245 }
246
247 /**
248 * Get the product's permalink.
249 *
250 * @return string
251 */
252 public function link() {
253 return $this->product->get_permalink();
254 }
255
256 /**
257 * Get the parent product's permalink.
258 *
259 * @return string
260 */
261 public function parent_link() {
262 return $this->parent->get_permalink();
263 }
264
265 /**
266 * Get the product's add to cart URL.
267 *
268 * @return string
269 */
270 public function add_to_cart_link() {
271 return $this->product->add_to_cart_url();
272 }
273
274 /**
275 * Get the product's availability.
276 *
277 * @return array
278 */
279 public function availability() {
280 return $this->product->get_availability();
281 }
282
283 /**
284 * Get the product's stock quantity.
285 *
286 * @return int|null
287 */
288 public function quantity() {
289 return $this->product->get_stock_quantity();
290 }
291
292 /**
293 * Get the product's stock status.
294 *
295 * @return string
296 */
297 public function stock_status() {
298 return $this->product->get_stock_status();
299 }
300
301 /**
302 * Get the product's weight.
303 *
304 * @return string|null
305 */
306 public function weight() {
307 return $this->product->get_weight();
308 }
309
310 /**
311 * Get the weight unit used in WooCommerce settings.
312 *
313 * @return string
314 */
315 public function weight_unit() {
316 // @phpstan-ignore-next-line
317 $weight_unit = get_option( 'woocommerce_weight_unit' );
318
319 if ( $weight_unit ) {
320 return $weight_unit;
321 }
322
323 return 'kg';
324 }
325
326 /**
327 * Get the product's width.
328 *
329 * @return string|null
330 */
331 public function width() {
332 return $this->product->get_width();
333 }
334
335 /**
336 * Get the product's height.
337 *
338 * @return string|null
339 */
340 public function height() {
341 return $this->product->get_height();
342 }
343
344 /**
345 * Get the product's length.
346 *
347 * @return string|null
348 */
349 public function length() {
350 return $this->product->get_length();
351 }
352
353 /**
354 * Get the product type.
355 *
356 * @return string
357 */
358 public function type() {
359 return $this->product->get_type();
360 }
361
362 /**
363 * Get the product's catalog visibility.
364 *
365 * @return string
366 */
367 public function visibility() {
368 return $this->product->get_catalog_visibility();
369 }
370
371 /**
372 * Get the total rating count of the product.
373 *
374 * @return int
375 */
376 public function rating_total() {
377 return $this->product->get_rating_count();
378 }
379
380 /**
381 * Get the average rating of the product.
382 *
383 * @return float
384 */
385 public function rating_average() {
386 return $this->product->get_average_rating();
387 }
388
389 /**
390 * Get the author's name of the product.
391 *
392 * @return string
393 */
394 public function author_name() {
395 $post = get_post( $this->product->get_id() );
396
397 if ( ! ( $post instanceof \WP_Post ) ) {
398 return '';
399 }
400
401 return get_the_author_meta( 'display_name', (int) $post->post_author );
402 }
403
404 /**
405 * Get the email of the author of the product.
406 *
407 * @return string
408 */
409 public function author_email() {
410 $post = get_post( $this->product->get_id() );
411
412 if ( ! ( $post instanceof \WP_Post ) ) {
413 return '';
414 }
415
416 return get_the_author_meta( 'user_email', (int) $post->post_author );
417 }
418
419 /**
420 * Get the date when the product was created.
421 *
422 * @return string
423 */
424 public function date_created() {
425 $date_created = $this->product->get_date_created();
426
427 if ( $date_created === null && $this->product->is_type( 'variation' ) ) {
428 $date_created = $this->parent->get_date_created();
429 }
430
431 if ( ! ( $date_created instanceof \WC_DateTime ) ) {
432 return '';
433 }
434
435 return $date_created->date_i18n();
436 }
437
438 /**
439 * Get the date when the product was last updated.
440 *
441 * @return string
442 */
443 public function date_updated() {
444 $date_created = $this->product->get_date_modified();
445
446 if ( $date_created === null && $this->product->is_type( 'variation' ) ) {
447 $date_created = $this->parent->get_date_modified();
448 }
449
450 if ( ! ( $date_created instanceof \WC_DateTime ) ) {
451 return '';
452 }
453
454 return $date_created->date_i18n();
455 }
456
457 /**
458 * Get the current status of the product.
459 *
460 * @return string
461 */
462 public function product_status() {
463 return $this->product->get_status();
464 }
465
466 /**
467 * Determine if the product is featured.
468 *
469 * @return string
470 */
471 public function featured_status() {
472 if ( $this->product->is_featured() ) {
473 return 'yes';
474 }
475
476 return 'no';
477 }
478
479 /**
480 * Get the currency used for the product.
481 *
482 * @return string
483 */
484 public function currency() {
485 return get_woocommerce_currency();
486 }
487
488 /**
489 * Get the product's regular price.
490 */
491 public function regular_price(): float {
492 return apply_filters( 'disco_convert_price', (float) $this->product->get_regular_price() );
493 }
494
495 /**
496 * Get the product's regular price.
497 *
498 * @return string
499 */
500 public function price() {
501 return apply_filters( 'disco_convert_price', (float) $this->product->get_price() );
502 }
503
504 /**
505 * Get the product's sale price.
506 *
507 * @return string
508 */
509 public function sale_price() {
510 return apply_filters( 'disco_convert_price', (float) $this->product->get_sale_price() );
511 }
512
513 /**
514 * Get the product's regular price including tax.
515 *
516 * @return float|string
517 */
518 public function regular_price_with_tax() {
519 $regular_price_with_tax = wc_get_price_including_tax(
520 $this->product,
521 array(
522 'price' => $this->product->get_regular_price(),
523 'qty' => 1,
524 )
525 );
526
527 return apply_filters( 'disco_convert_price', (float) $regular_price_with_tax );
528 }
529
530 /**
531 * Get the product's price including tax.
532 *
533 * @return float|string
534 */
535 public function price_with_tax() {
536 $price_with_tax = wc_get_price_including_tax(
537 $this->product,
538 array(
539 'price' => $this->product->get_price(),
540 'qty' => 1,
541 )
542 );
543
544 return apply_filters( 'disco_convert_price', (float) $price_with_tax );
545 }
546
547 /**
548 * Get the product's sale price including tax.
549 *
550 * @return float|string
551 */
552 public function sale_price_with_tax() {
553 $sale_price_with_tax = wc_get_price_including_tax(
554 $this->product,
555 array(
556 'price' => $this->product->get_sale_price(),
557 'qty' => 1,
558 )
559 );
560
561 return apply_filters( 'disco_convert_price', (float) $sale_price_with_tax );
562 }
563
564 /**
565 * Get the starting date of the product's sale.
566 *
567 * @return string
568 */
569 public function sale_price_sdate() {
570 $sdate = $this->product->get_date_on_sale_from();
571
572 if ( ! ( $sdate instanceof \WC_DateTime ) ) {
573 return '';
574 }
575
576 return $sdate->date_i18n();
577 }
578
579 /**
580 * Get the ending date of the product's sale.
581 *
582 * @return string|false
583 */
584 public function sale_price_edate() {
585 $edate = $this->product->get_date_on_sale_to();
586
587 if ( ! ( $edate instanceof \WC_DateTime ) ) {
588 return '';
589 }
590
591 return $edate->date_i18n();
592 }
593
594 /**
595 * Get the tax class of the product.
596 *
597 * @return string
598 */
599 public function tax_class() {
600 $tax_class = $this->product->get_tax_class();
601
602 if ( empty( $tax_class ) ) {
603 $tax_class = 'standard';
604 }
605
606 return $tax_class;
607 }
608
609 /**
610 * Get the tax status of the product.
611 *
612 * @return string
613 */
614 public function tax_status() {
615 return $this->product->get_tax_status();
616 }
617
618 /**
619 * Get the shipping class of the product.
620 *
621 * @return string
622 */
623 public function shipping_class() {
624 return $this->product->get_shipping_class();
625 }
626
627 /**
628 * Get a specific global attribute of the product.
629 *
630 * @param string $attribute Name of the attribute to retrieve.
631 * @return array
632 */
633 public function get_attribute( $attribute = '' ) {
634 $options = $this->product->get_attribute( $attribute );
635
636 // If the attribute is empty, return an empty array
637 if ( empty( $options ) ) {
638 return array();
639 }
640
641 return explode( ',', $options );
642 }
643
644 /**
645 * Get a custom attribute or meta of the product.
646 *
647 * If the product is a variation and doesn't have the specified meta, retrieve it from the parent product.
648 *
649 * @param string $meta Name of the meta to retrieve.
650 * @return mixed
651 */
652 public function get_product_meta( $meta = '' ) {
653 // Remove the underscore prefix from the meta-key.
654 $first = $meta[0];
655
656 if ( $first === '_' ) {
657 $meta = substr( $meta, 1 );
658 }
659
660 $value = $this->product->get_meta( $meta, true );
661
662 if ( $value === '' && $this->product->is_type( 'variation' ) ) {
663 $value = $this->parent->get_meta( $meta, true );
664 }
665
666 return $value;
667 }
668 // ... End of Class ...
669
670 }
671