| @@ -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 | * |
| @@ -36,38 +38,14 @@ | ||
| 36 | 38 | $this->product = $product; |
| 37 | 39 | } |
| 38 | 40 | |
| 39 | 41 | /** |
| 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 | 42 | * Is product relevant for Packeta processing? |
| 65 | 43 | * |
| 66 | 44 | * @return bool |
| 67 | 45 | */ |
| 68 | 46 | public function isPhysical(): bool { |
| 69 | - return false === $this->product->is_virtual() && false === $this->product->is_downloadable(); | |
| 47 | + return $this->product->is_virtual() === false && $this->product->is_downloadable() === false; | |
| 70 | 48 | } |
| 71 | 49 | |
| 72 | 50 | /** |
| 73 | 51 | * Is age verification required? |
| @@ -73,9 +51,9 @@ | ||
| 73 | 51 | * Is age verification required? |
| 74 | 52 | * |
| 75 | 53 | * @return bool |
| 76 | 54 | */ |
| 77 | - public function isAgeVerification18PlusRequired(): bool { | |
| 55 | + public function isAgeVerificationRequired(): bool { | |
| 78 | 56 | return (string) $this->product->get_meta( self::META_AGE_VERIFICATION_18_PLUS ) === '1'; |
| 79 | 57 | } |
| 80 | 58 | |
| 81 | 59 | /** |
| @@ -80,14 +58,13 @@ | ||
| 80 | 58 | |
| 81 | 59 | /** |
| 82 | 60 | * Disallowed carrier choices. |
| 83 | 61 | * |
| 84 | - * @return array | |
| 62 | + * @return string[] | |
| 85 | 63 | */ |
| 86 | 64 | public function getDisallowedShippingRateChoices(): array { |
| 87 | 65 | $choices = $this->product->get_meta( self::META_DISALLOWED_SHIPPING_RATES ); |
| 88 | 66 | if ( ! is_array( $choices ) ) { |
| 89 | - // TODO: log this event, really happened. | |
| 90 | 67 | return []; |
| 91 | 68 | } |
| 92 | 69 | |
| 93 | 70 | return $choices; |
| @@ -95,9 +72,9 @@ | ||
| 95 | 72 | |
| 96 | 73 | /** |
| 97 | 74 | * Disallowed carrier ids. |
| 98 | 75 | * |
| 99 | - * @return array | |
| 76 | + * @return string[] | |
| 100 | 77 | */ |
| 101 | 78 | public function getDisallowedShippingRateIds(): array { |
| 102 | 79 | return array_keys( $this->getDisallowedShippingRateChoices() ); |
| 103 | 80 | } |
| @@ -108,6 +85,33 @@ | ||
| 108 | 85 | * @return int |
| 109 | 86 | */ |
| 110 | 87 | public function getId(): int { |
| 111 | 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 | + ); | |
| 112 | 116 | } |
| 113 | 117 | } |