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

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