PluginProbe
Packeta / 2.1
Packeta v2.1
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 / DataTab.php

DataTab.php in Packeta 2.1, at src/Packetery/Module/Product/DataTab.php

206 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Packetery product tab.
4 *
5 * @package Packetery\Module\Product
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Product;
11
12 use Packetery\Latte\Engine;
13 use Packetery\Module\Carrier\CarDeliveryConfig;
14 use Packetery\Module\Carrier\EntityRepository;
15 use Packetery\Module\Carrier\OptionPrefixer;
16 use Packetery\Module\Exception\ProductNotFoundException;
17 use Packetery\Module\FormFactory;
18 use Packetery\Module\Product;
19 use Packetery\Nette\Forms\Form;
20
21 /**
22 * Class Tab
23 *
24 * @package Packetery\Module\Product
25 */
26 class DataTab {
27
28 const NAME = 'packetery-tab';
29
30 /**
31 * Form factory.
32 *
33 * @var FormFactory
34 */
35 private $formFactory;
36
37 /**
38 * Latte engine.
39 *
40 * @var Engine
41 */
42 private $latteEngine;
43
44 /**
45 * Carrier repository
46 *
47 * @var EntityRepository
48 */
49 private $carrierRepository;
50
51 /**
52 * Car delivery config.
53 *
54 * @var CarDeliveryConfig
55 */
56 private $carDeliveryConfig;
57
58 /**
59 * Product entity factory.
60 *
61 * @var ProductEntityFactory
62 */
63 private $productEntityFactory;
64
65 /**
66 * Tab constructor.
67 *
68 * @param FormFactory $formFactory Factory engine.
69 * @param Engine $latteEngine Latte engine.
70 * @param EntityRepository $carrierRepository Carrier repository.
71 * @param CarDeliveryConfig $carDeliveryConfig Car Delivery config.
72 * @param ProductEntityFactory $productEntityFactory Product entity factory.
73 */
74 public function __construct(
75 FormFactory $formFactory,
76 Engine $latteEngine,
77 EntityRepository $carrierRepository,
78 CarDeliveryConfig $carDeliveryConfig,
79 ProductEntityFactory $productEntityFactory
80 ) {
81 $this->formFactory = $formFactory;
82 $this->latteEngine = $latteEngine;
83 $this->carrierRepository = $carrierRepository;
84 $this->carDeliveryConfig = $carDeliveryConfig;
85 $this->productEntityFactory = $productEntityFactory;
86 }
87
88 /**
89 * Register component.
90 *
91 * @return void
92 */
93 public function register(): void {
94 add_filter( 'woocommerce_product_data_tabs', [ $this, 'registerTab' ], 1, 1 );
95 add_action( 'woocommerce_product_data_panels', [ $this, 'render' ] );
96 add_action( 'woocommerce_process_product_meta', [ $this, 'saveData' ] );
97 }
98
99 /**
100 * Registers tab.
101 *
102 * @param string[] $tabs Tabs definition array.
103 *
104 * @return array<array<string,string[]|string>|string>
105 */
106 public function registerTab( array $tabs ): array {
107 $tabs[ self::NAME ] = [
108 'label' => __( 'Packeta', 'packeta' ),
109 'target' => self::NAME,
110 'class' => [ 'hide_if_virtual', 'hide_if_downloadable' ],
111 ];
112
113 return $tabs;
114 }
115
116 /**
117 * Creates form instance.
118 *
119 * @param Product\Entity $product Related product.
120 *
121 * @return Form
122 */
123 private function createForm( Product\Entity $product ): Form {
124 $form = $this->formFactory->create();
125 $form->addCheckbox( Product\Entity::META_AGE_VERIFICATION_18_PLUS, __( 'Age verification 18+', 'packeta' ) );
126
127 $carriersContainer = $form->addContainer( Product\Entity::META_DISALLOWED_SHIPPING_RATES );
128 $carriersList = $this->carrierRepository->getAllActiveCarriersList();
129 foreach ( $carriersList as $carrier ) {
130 if ( $this->carDeliveryConfig->isCarDeliveryCarrierDisabled( OptionPrefixer::removePrefix( $carrier['option_id'] ) ) ) {
131 continue;
132 }
133 $carriersContainer->addCheckbox( $carrier['option_id'], $carrier['label'] );
134 }
135
136 $form->setDefaults(
137 [
138 Product\Entity::META_AGE_VERIFICATION_18_PLUS => $product->isAgeVerificationRequired(),
139 Product\Entity::META_DISALLOWED_SHIPPING_RATES => $product->getDisallowedShippingRateChoices(),
140 ]
141 );
142
143 return $form;
144 }
145
146 /**
147 * Renders tab.
148 *
149 * @return void
150 */
151 public function render(): void {
152 $this->latteEngine->render(
153 PACKETERY_PLUGIN_DIR . '/template/product/data-tab-panel.latte',
154 [
155 'form' => $this->createForm( $this->productEntityFactory->fromGlobals() ),
156 'translations' => [
157 'disallowedShippingRatesHeading' => __( 'Packeta shipping methods disabled for this product.', 'packeta' ),
158 ],
159 ]
160 );
161 }
162
163 /**
164 * Saves product data.
165 *
166 * @param int|string $postId Post ID.
167 *
168 * @throws ProductNotFoundException Product not found.
169 */
170 public function saveData( $postId ): void {
171 $product = $this->productEntityFactory->fromPostId( $postId );
172 if ( $product->isPhysical() === false ) {
173 return;
174 }
175
176 $form = $this->createForm( $product );
177 $form->onSuccess[] = function ( Form $form, array $values ) use ( $product ) {
178 $this->processFormData( $product->getId(), $values );
179 };
180
181 if ( $form->isSubmitted() ) {
182 $form->fireEvents();
183 }
184 }
185
186 /**
187 * Process form data.
188 *
189 * @param int $productId Product ID.
190 * @param array<string, bool|array<string,bool>> $values Form values.
191 */
192 public function processFormData( int $productId, array $values ): void {
193 foreach ( $values as $attr => $value ) {
194 if ( is_bool( $value ) ) {
195 $value = $value ? '1' : '0';
196 }
197
198 if ( $attr === Product\Entity::META_DISALLOWED_SHIPPING_RATES ) {
199 $value = array_filter( $value );
200 }
201
202 update_post_meta( $productId, $attr, $value );
203 }
204 }
205 }
206