| 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 $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 ( ! $choices ) { |
| 89 |
return []; |
| 90 |
} |
| 91 |
|
| 92 |
return $choices; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Disallowed carrier ids. |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function getDisallowedShippingRateIds(): array { |
| 101 |
return array_keys( $this->getDisallowedShippingRateChoices() ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Gets product ID. |
| 106 |
* |
| 107 |
* @return int |
| 108 |
*/ |
| 109 |
public function getId(): int { |
| 110 |
return $this->product->get_id(); |
| 111 |
} |
| 112 |
} |
| 113 |
|