PluginProbe
Packeta / 1.6.2
Packeta v1.6.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Product / Entity.php

Entity.php in Packeta 1.6.2, at src/Packetery/Module/Product/Entity.php

114 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Product entity.
4 *
5 * @package Packetery\Module\Product
6 */
7
8 declare( strict_types=1 );
9
10
11 namespace Packetery\Module\Product;
12
13 /**
14 * Class Entity
15 *
16 * @package Packetery\Module\Product
17 */
18 class Entity {
19
20 public const META_AGE_VERIFICATION_18_PLUS = 'packetery_age_verification_18_plus';
21 public const META_DISALLOWED_SHIPPING_RATES = 'packetery_disallowed_shipping_rates';
22
23 /**
24 * Product.
25 *
26 * @var \WC_Product
27 */
28 private $product;
29
30 /**
31 * Entity constructor.
32 *
33 * @param \WC_Product $product Product.
34 */
35 public function __construct( \WC_Product $product ) {
36 $this->product = $product;
37 }
38
39 /**
40 * Creates instance using global variables.
41 *
42 * @return static
43 */
44 public static function fromGlobals(): self {
45 global $post;
46
47 return self::fromPostId( $post->ID );
48 }
49
50 /**
51 * Create instance from post ID.
52 *
53 * @param int|string $postId Post ID.
54 *
55 * @return static
56 */
57 public static function fromPostId( $postId ): self {
58 $product = wc_get_product( $postId );
59
60 return new self( $product );
61 }
62
63 /**
64 * Is product relevant for Packeta processing?
65 *
66 * @return bool
67 */
68 public function isPhysical(): bool {
69 return false === $this->product->is_virtual() && false === $this->product->is_downloadable();
70 }
71
72 /**
73 * Is age verification required?
74 *
75 * @return bool
76 */
77 public function isAgeVerification18PlusRequired(): bool {
78 return (string) $this->product->get_meta( self::META_AGE_VERIFICATION_18_PLUS ) === '1';
79 }
80
81 /**
82 * Disallowed carrier choices.
83 *
84 * @return array
85 */
86 public function getDisallowedShippingRateChoices(): array {
87 $choices = $this->product->get_meta( self::META_DISALLOWED_SHIPPING_RATES );
88 if ( ! is_array( $choices ) ) {
89 // TODO: log this event, really happened.
90 return [];
91 }
92
93 return $choices;
94 }
95
96 /**
97 * Disallowed carrier ids.
98 *
99 * @return array
100 */
101 public function getDisallowedShippingRateIds(): array {
102 return array_keys( $this->getDisallowedShippingRateChoices() );
103 }
104
105 /**
106 * Gets product ID.
107 *
108 * @return int
109 */
110 public function getId(): int {
111 return $this->product->get_id();
112 }
113 }
114