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

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