PluginProbe
Packeta / trunk
Packeta vtrunk
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 trunk, at src/Packetery/Module/Product/Entity.php

118 lines 2.4 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 namespace Packetery\Module\Product;
11
12 use Packetery\Core\CoreHelper;
13 use Packetery\Module\Framework\WpAdapter;
14
15 /**
16 * Class Entity
17 *
18 * @package Packetery\Module\Product
19 */
20 class Entity {
21
22 public const META_AGE_VERIFICATION_18_PLUS = 'packetery_age_verification_18_plus';
23 public const META_DISALLOWED_SHIPPING_RATES = 'packetery_disallowed_shipping_rates';
24
25 /**
26 * Product.
27 *
28 * @var \WC_Product
29 */
30 private $product;
31
32 /**
33 * Entity constructor.
34 *
35 * @param \WC_Product $product Product.
36 */
37 public function __construct( \WC_Product $product ) {
38 $this->product = $product;
39 }
40
41 /**
42 * Is product relevant for Packeta processing?
43 *
44 * @return bool
45 */
46 public function isPhysical(): bool {
47 return $this->product->is_virtual() === false && $this->product->is_downloadable() === false;
48 }
49
50 /**
51 * Is age verification required?
52 *
53 * @return bool
54 */
55 public function isAgeVerificationRequired(): bool {
56 return (string) $this->product->get_meta( self::META_AGE_VERIFICATION_18_PLUS ) === '1';
57 }
58
59 /**
60 * Disallowed carrier choices.
61 *
62 * @return string[]
63 */
64 public function getDisallowedShippingRateChoices(): array {
65 $choices = $this->product->get_meta( self::META_DISALLOWED_SHIPPING_RATES );
66 if ( ! is_array( $choices ) ) {
67 return [];
68 }
69
70 return $choices;
71 }
72
73 /**
74 * Disallowed carrier ids.
75 *
76 * @return string[]
77 */
78 public function getDisallowedShippingRateIds(): array {
79 return array_keys( $this->getDisallowedShippingRateChoices() );
80 }
81
82 /**
83 * Gets product ID.
84 *
85 * @return int
86 */
87 public function getId(): int {
88 return $this->product->get_id();
89 }
90
91 public function getLengthInCm( WpAdapter $wpAdapter ): float {
92 return round(
93 CoreHelper::convertToCentimeters(
94 (float) $this->product->get_length(),
95 (string) $wpAdapter->getOption( 'woocommerce_dimension_unit' )
96 )
97 );
98 }
99
100 public function getWidthInCm( WpAdapter $wpAdapter ): float {
101 return round(
102 CoreHelper::convertToCentimeters(
103 (float) $this->product->get_width(),
104 (string) $wpAdapter->getOption( 'woocommerce_dimension_unit' )
105 )
106 );
107 }
108
109 public function getHeightInCm( WpAdapter $wpAdapter ): float {
110 return round(
111 CoreHelper::convertToCentimeters(
112 (float) $this->product->get_height(),
113 (string) $wpAdapter->getOption( 'woocommerce_dimension_unit' )
114 )
115 );
116 }
117 }
118