| @@ -6,10 +6,12 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | +namespace Packetery\Module\Product; | |
| 10 | 11 | |
| 11 | -namespace Packetery\Module\Product; | |
| 12 | +use Packetery\Core\CoreHelper; | |
| 13 | +use Packetery\Module\Framework\WpAdapter; | |
| 12 | 14 | |
| 13 | 15 | /** |
| 14 | 16 | * Class Entity |
| 15 | 17 | * |
| @@ -16,9 +18,10 @@ | ||
| 16 | 18 | * @package Packetery\Module\Product |
| 17 | 19 | */ |
| 18 | 20 | class Entity { |
| 19 | 21 | |
| 20 | - public const META_AGE_VERIFICATION_18_PLUS = 'packetery_age_verification_18_plus'; | |
| 22 | + public const META_AGE_VERIFICATION_18_PLUS = 'packetery_age_verification_18_plus'; | |
| 23 | + public const META_DISALLOWED_SHIPPING_RATES = 'packetery_disallowed_shipping_rates'; | |
| 21 | 24 | |
| 22 | 25 | /** |
| 23 | 26 | * Product. |
| 24 | 27 | * |
| @@ -35,47 +38,46 @@ | ||
| 35 | 38 | $this->product = $product; |
| 36 | 39 | } |
| 37 | 40 | |
| 38 | 41 | /** |
| 39 | - * Creates instance using global variables. | |
| 42 | + * Is product relevant for Packeta processing? | |
| 40 | 43 | * |
| 41 | - * @return static | |
| 44 | + * @return bool | |
| 42 | 45 | */ |
| 43 | - public static function fromGlobals(): self { | |
| 44 | - global $post; | |
| 45 | - | |
| 46 | - return self::fromPostId( $post->ID ); | |
| 46 | + public function isPhysical(): bool { | |
| 47 | + return $this->product->is_virtual() === false && $this->product->is_downloadable() === false; | |
| 47 | 48 | } |
| 48 | 49 | |
| 49 | 50 | /** |
| 50 | - * Create instance from post ID. | |
| 51 | + * Is age verification required? | |
| 51 | 52 | * |
| 52 | - * @param int|string $postId Post ID. | |
| 53 | - * | |
| 54 | - * @return static | |
| 53 | + * @return bool | |
| 55 | 54 | */ |
| 56 | - public static function fromPostId( $postId ): self { | |
| 57 | - $product = wc_get_product( $postId ); | |
| 58 | - | |
| 59 | - return new self( $product ); | |
| 55 | + public function isAgeVerificationRequired(): bool { | |
| 56 | + return (string) $this->product->get_meta( self::META_AGE_VERIFICATION_18_PLUS ) === '1'; | |
| 60 | 57 | } |
| 61 | 58 | |
| 62 | 59 | /** |
| 63 | - * Is product relevant for Packeta processing? | |
| 60 | + * Disallowed carrier choices. | |
| 64 | 61 | * |
| 65 | - * @return bool | |
| 62 | + * @return string[] | |
| 66 | 63 | */ |
| 67 | - public function isPhysical(): bool { | |
| 68 | - return false === $this->product->is_virtual() && false === $this->product->is_downloadable(); | |
| 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; | |
| 69 | 71 | } |
| 70 | 72 | |
| 71 | 73 | /** |
| 72 | - * Is age verification required? | |
| 74 | + * Disallowed carrier ids. | |
| 73 | 75 | * |
| 74 | - * @return bool | |
| 76 | + * @return string[] | |
| 75 | 77 | */ |
| 76 | - public function isAgeVerification18PlusRequired(): bool { | |
| 77 | - return $this->product->get_meta( self::META_AGE_VERIFICATION_18_PLUS ) === '1'; | |
| 78 | + public function getDisallowedShippingRateIds(): array { | |
| 79 | + return array_keys( $this->getDisallowedShippingRateChoices() ); | |
| 78 | 80 | } |
| 79 | 81 | |
| 80 | 82 | /** |
| 81 | 83 | * Gets product ID. |
| @@ -83,6 +85,33 @@ | ||
| 83 | 85 | * @return int |
| 84 | 86 | */ |
| 85 | 87 | public function getId(): int { |
| 86 | 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 | + ); | |
| 87 | 116 | } |
| 88 | 117 | } |