PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.2
WooCommerce Square v3.8.2
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Handlers / Product / Woo_SOR.php
woocommerce-square / includes / Handlers / Product Last commit date
Woo_SOR.php 3 years ago
Woo_SOR.php
300 lines
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
22 */
23
24 namespace WooCommerce\Square\Handlers\Product;
25
26 use Square\Models\CatalogObject;
27
28 class Woo_SOR extends \WooCommerce\Square\Handlers\Product {
29
30 /**
31 * Updates a Square catalog item with a WooCommerce product's data.
32 *
33 * @since 2.0.0
34 *
35 * @param CatalogObject $catalog_object Square SDK catalog object
36 * @param \WC_Product $product WooCommerce product
37 * @return CatalogObject
38 * @throws \Exception
39 */
40 public static function update_catalog_item( CatalogObject $catalog_object, \WC_Product $product ) {
41
42 if ( 'ITEM' !== $catalog_object->getType() || ! $catalog_object->getItemData() ) {
43 throw new \Exception( 'Type of $catalog_object must be an ITEM' );
44 }
45
46 // ensure the product meta is persisted
47 self::update_product( $product, $catalog_object );
48
49 if ( ! $catalog_object->getId() ) {
50 $catalog_object->setId( self::get_square_item_id( $product ) );
51 }
52
53 $is_delete = 'trash' === $product->get_status();
54
55 $catalog_object = self::set_catalog_object_location_ids( $catalog_object, $is_delete );
56
57 $item_data = $catalog_object->getItemData();
58
59 $item_data->setName( $product->get_name() );
60
61 $square_category_id = 0;
62
63 foreach ( $product->get_category_ids() as $category_id ) {
64
65 $map = \WooCommerce\Square\Handlers\Category::get_mapping( $category_id );
66
67 if ( ! empty( $map['square_id'] ) ) {
68
69 $square_category_id = $map['square_id'];
70 break;
71 }
72 }
73
74 // if a category with a Square ID was found
75 if ( $square_category_id ) {
76 $item_data->setCategoryId( $square_category_id );
77 }
78
79 $catalog_variations = $item_data->getVariations() ?: array();
80
81 // if dealing with a variable product, try and match the variations
82 if ( $product->is_type( 'variable' ) ) {
83
84 $product_variation_ids = $product->get_children();
85
86 if ( is_array( $catalog_variations ) ) {
87
88 foreach ( $catalog_variations as $object_key => $variation_object ) {
89
90 $product_variation_id = self::get_product_id_by_square_variation_id( $variation_object->getId() );
91
92 // ID might not be set, so try the SKU
93 if ( ! $product_variation_id ) {
94 $product_variation_id = wc_get_product_id_by_sku( $variation_object->getItemVariationData()->getSku() );
95 }
96
97 // if a product was found and belongs to the parent, use it
98 if ( false !== ( $key = array_search( $product_variation_id, $product_variation_ids, false ) ) ) {
99
100 $product_variation = wc_get_product( $product_variation_id );
101
102 if ( $product_variation instanceof \WC_Product ) {
103
104 $catalog_variations[ $object_key ] = self::update_catalog_variation( $variation_object, $product_variation );
105
106 // consider this variation taken care of
107 unset( $product_variation_ids[ $key ] );
108 }
109 } else {
110
111 unset( $catalog_variations[ $object_key ] );
112 }
113 }
114 }
115
116 // all that's left are variations that didn't have a match, so create new variations
117 foreach ( $product_variation_ids as $product_variation_id ) {
118
119 $product_variation = wc_get_product( $product_variation_id );
120
121 if ( ! $product_variation instanceof \WC_Product ) {
122 continue;
123 }
124
125 $variation_object = new CatalogObject(
126 'ITEM_VARIATION',
127 ''
128 );
129
130 $catalog_item_variation = new \Square\Models\CatalogItemVariation();
131 $catalog_item_variation->setItemId( $catalog_object->getId() );
132 $variation_object->setItemVariationData( $catalog_item_variation );
133
134 $catalog_variations[] = self::update_catalog_variation( $variation_object, $product_variation );
135 }
136 } else { // otherwise, we have a simple product
137
138 if ( ! empty( $catalog_variations ) ) {
139
140 $variation_object = $catalog_variations[0];
141
142 } else {
143
144 $variation_object = new CatalogObject(
145 'ITEM_VARIATION',
146 ''
147 );
148
149 $catalog_item_variation = new \Square\Models\CatalogItemVariation();
150 $catalog_item_variation->setItemId( $catalog_object->getId() );
151 $variation_object->setItemVariationData( $catalog_item_variation );
152 }
153
154 $catalog_variations = array( self::update_catalog_variation( $variation_object, $product ) );
155 }
156
157 $item_data->setVariations( array_values( $catalog_variations ) );
158
159 $catalog_object->setItemData( $item_data );
160
161 /**
162 * Fires when updating a Square catalog item with WooCommerce product data.
163 *
164 * @since 2.0.0
165 *
166 * @param CatalogObject $catalog_object Square SDK catalog object
167 * @param \WC_Product $product WooCommerce product
168 */
169 $catalog_object = apply_filters( 'wc_square_update_catalog_item', $catalog_object, $product );
170
171 return $catalog_object;
172 }
173
174
175 /**
176 * Updates a Square catalog item variation with a WooCommerce product's data.
177 *
178 * @since 2.0.0
179 *
180 * @param CatalogObject $catalog_object Square SDK catalog object
181 * @param \WC_Product $product WooCommerce product
182 * @return CatalogObject
183 * @throws \Exception
184 */
185 public static function update_catalog_variation( CatalogObject $catalog_object, \WC_Product $product ) {
186
187 if ( 'ITEM_VARIATION' !== $catalog_object->getType() || ! $catalog_object->getItemVariationData() ) {
188 throw new \Exception( 'Type of $catalog_object must be an ITEM_VARIATION' );
189 }
190
191 // ensure the variation meta is persisted
192 self::update_variation( $product, $catalog_object );
193
194 if ( ! $catalog_object->getId() ) {
195 $catalog_object->setId( self::get_square_item_variation_id( $product ) );
196 }
197
198 if ( ! $catalog_object->getVersion() ) {
199 $catalog_object->setVersion( self::get_square_variation_version( $product ) );
200 }
201
202 $catalog_object = self::set_catalog_object_location_ids( $catalog_object, 'trash' === $product->get_status() );
203
204 $variation_data = $catalog_object->getItemVariationData();
205
206 if ( $product->get_regular_price() || $product->get_sale_price() ) {
207 $variation_data->setPriceMoney( self::price_to_money( $product->get_sale_price() ?: $product->get_regular_price() ) );
208 } else {
209 $variation_data->setPriceMoney( self::price_to_money( 0 ) );
210 }
211
212 $variation_data->setPricingType( 'FIXED_PRICING' );
213
214 /**
215 * Simple products have only 1 variation and the name of the variation
216 * is derived from CatalogItem::name. For variable products, each variation
217 * can have its own name, so we put a condition to only set the name for
218 * variation.
219 *
220 * @see https://github.com/woocommerce/woocommerce-square/issues/570
221 */
222 if ( 'variation' === $product->get_type() ) {
223 $variation_data->setName( $product->get_name() );
224 }
225
226 if ( wc_square()->get_settings_handler()->is_inventory_sync_enabled() ) {
227 $variation_data->setTrackInventory( true );
228 }
229
230 $variation_data->setSku( $product->get_sku() );
231
232 if ( ! $variation_data->getItemId() ) {
233
234 $parent_product = $product->get_parent_id() ? wc_get_product( $product->get_parent_id() ) : $product;
235
236 if ( ! $parent_product instanceof \WC_Product ) {
237 $variation_data->setItemId( self::get_square_item_id( $parent_product ) );
238 }
239 }
240
241 $catalog_object->setItemVariationData( $variation_data );
242
243 /**
244 * Fires when updating a Square catalog item variation with WooCommerce product data.
245 *
246 * @since 2.0.0
247 *
248 * @param CatalogObject $catalog_object Square SDK catalog object
249 * @param \WC_Product $product WooCommerce product
250 */
251 $catalog_object = apply_filters( 'wc_square_update_catalog_item_variation', $catalog_object, $product );
252
253 return $catalog_object;
254 }
255
256
257 /**
258 * Sets the present/absent location IDs to a catalog object.
259 *
260 * @since 2.0.0
261 *
262 * @param CatalogObject $catalog_object Square SDK catalog object
263 * @param bool $is_delete whether the product is being deleted
264 * @return CatalogObject
265 */
266 public static function set_catalog_object_location_ids( CatalogObject $catalog_object, $is_delete = false ) {
267
268 $location_id = wc_square()->get_settings_handler()->get_location_id();
269
270 $present_location_ids = $catalog_object->getPresentAtLocationIds() ?: array();
271 $absent_location_ids = $catalog_object->getAbsentAtLocationIds() ?: array();
272
273 // if trashed, set as absent at our location
274 if ( $is_delete ) {
275
276 $absent_location_ids[] = $location_id;
277
278 if ( false !== ( $key = array_search( $location_id, $present_location_ids, true ) ) ) {
279 unset( $present_location_ids[ $key ] );
280 }
281 } else { // otherwise, it's present
282
283 $present_location_ids[] = $location_id;
284
285 if ( false !== ( $key = array_search( $location_id, $absent_location_ids, true ) ) ) {
286 unset( $absent_location_ids[ $key ] );
287 }
288 }
289
290 $catalog_object->setAbsentAtLocationIds( array_unique( array_values( $absent_location_ids ) ) );
291 $catalog_object->setPresentAtLocationIds( array_unique( array_values( $present_location_ids ) ) );
292
293 $catalog_object->setPresentAtAllLocations( false );
294
295 return $catalog_object;
296 }
297
298
299 }
300