PluginProbe
Packeta / trunk
Packeta vtrunk
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 trunk, at src/Packetery/Module/Product/DataTab.php

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