| 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 |
|