-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathWCProductAdapter.php
1159 lines (998 loc) · 36.7 KB
/
WCProductAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Product;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\AttributeMapping\AttributeMappingHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\Condition;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\SizeSystem;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\SizeType;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AgeGroup;
use Automattic\WooCommerce\GoogleListingsAndAds\Validator\GooglePriceConstraint;
use Automattic\WooCommerce\GoogleListingsAndAds\Validator\ImageUrlConstraint;
use Automattic\WooCommerce\GoogleListingsAndAds\Validator\Validatable;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Price as GooglePrice;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Product as GoogleProduct;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\ProductShipping as GoogleProductShipping;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\ProductShippingDimension as GoogleProductShippingDimension;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\ProductShippingWeight as GoogleProductShippingWeight;
use DateInterval;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use WC_DateTime;
use WC_Product;
use WC_Product_Variable;
use WC_Product_Variation;
defined( 'ABSPATH' ) || exit;
/**
* Class WCProductAdapter
*
* This class adapts the WooCommerce Product class to the Google's Product class by mapping their attributes.
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product
*/
class WCProductAdapter extends GoogleProduct implements Validatable {
use PluginHelper;
public const AVAILABILITY_IN_STOCK = 'in_stock';
public const AVAILABILITY_OUT_OF_STOCK = 'out_of_stock';
public const AVAILABILITY_BACKORDER = 'backorder';
public const AVAILABILITY_PREORDER = 'preorder';
public const IMAGE_SIZE_FULL = 'full';
public const CHANNEL_ONLINE = 'online';
/**
* @var WC_Product WooCommerce product object
*/
protected $wc_product;
/**
* @var WC_Product WooCommerce parent product object if $wc_product is a variation
*/
protected $parent_wc_product;
/**
* @var bool Whether tax is excluded from product price
*/
protected $tax_excluded;
/**
* @var array Product category ids
*/
protected $product_category_ids;
/**
* Initialize this object's properties from an array.
*
* @param array $properties Used to seed this object's properties.
*
* @return void
*
* @throws InvalidValue When a WooCommerce product is not provided or it is invalid.
*/
public function mapTypes( $properties ) {
if ( empty( $properties['wc_product'] ) || ! $properties['wc_product'] instanceof WC_Product ) {
throw InvalidValue::not_instance_of( WC_Product::class, 'wc_product' );
}
// throw an exception if the parent product isn't provided and this is a variation
if ( $properties['wc_product'] instanceof WC_Product_Variation &&
( empty( $properties['parent_wc_product'] ) || ! $properties['parent_wc_product'] instanceof WC_Product_Variable )
) {
throw InvalidValue::not_instance_of( WC_Product_Variable::class, 'parent_wc_product' );
}
if ( empty( $properties['targetCountry'] ) ) {
throw InvalidValue::is_empty( 'targetCountry' );
}
$this->wc_product = $properties['wc_product'];
$this->parent_wc_product = $properties['parent_wc_product'] ?? null;
$mapping_rules = $properties['mapping_rules'] ?? [];
$gla_attributes = $properties['gla_attributes'] ?? [];
// Google doesn't expect extra fields, so it's best to remove them
unset( $properties['wc_product'] );
unset( $properties['parent_wc_product'] );
unset( $properties['gla_attributes'] );
unset( $properties['mapping_rules'] );
parent::mapTypes( $properties );
$this->map_woocommerce_product();
$this->map_attribute_mapping_rules( $mapping_rules );
$this->map_gla_attributes( $gla_attributes );
// Allow users to override the product's attributes using a WordPress filter.
$this->override_attributes();
}
/**
* Map the WooCommerce product attributes to the current class.
*
* @return void
*/
protected function map_woocommerce_product() {
$this->setChannel( self::CHANNEL_ONLINE );
$content_language = empty( get_locale() ) ? 'en' : strtolower( substr( get_locale(), 0, 2 ) ); // ISO 639-1.
$this->setContentLanguage( $content_language );
$this->map_wc_product_id()
->map_wc_general_attributes()
->map_product_categories()
->map_wc_product_image( self::IMAGE_SIZE_FULL )
->map_wc_availability()
->map_wc_product_shipping()
->map_wc_prices();
}
/**
* Overrides the product attributes by applying a filter and setting the provided values.
*
* @since 1.4.0
*/
protected function override_attributes() {
/**
* Filters the list of overridden attributes to set for this product.
*
* Note: This filter takes precedence over any other filter that modify products attributes. Including
* `woocommerce_gla_product_attribute_value_{$attribute_id}` defined in self::map_gla_attributes.
*
* @param array $attributes An array of values for the product properties. All properties of the
* `\Google\Service\ShoppingContent\Product` class can be set by providing
* the property name as key and its value as array item.
* For example:
* [ 'imageLink' => 'https://example.com/image.jpg' ] overrides the product's
* main image.
*
* @param WC_Product $wc_product The WooCommerce product object.
* @param WCProductAdapter $this The Adapted Google product object. All WooCommerce product properties
* are already mapped to this object.
*
* @see \Google\Service\ShoppingContent\Product for the list of product properties that can be overriden.
* @see WCProductAdapter::map_gla_attributes for the docuementation of `woocommerce_gla_product_attribute_value_{$attribute_id}`
* filter, which allows modifying some attributes such as GTIN, MPN, etc.
*
* @since 1.4.0
*/
$attributes = apply_filters( 'woocommerce_gla_product_attribute_values', [], $this->wc_product, $this );
if ( ! empty( $attributes ) ) {
parent::mapTypes( $attributes );
}
}
/**
* Map the general WooCommerce product attributes.
*
* @return $this
*/
protected function map_wc_general_attributes() {
$this->setTitle( $this->wc_product->get_title() );
$this->setDescription( $this->get_wc_product_description() );
$this->setLink( $this->wc_product->get_permalink() );
// set item group id for variations
if ( $this->is_variation() ) {
$this->setItemGroupId( $this->parent_wc_product->get_id() );
}
return $this;
}
/**
* Map WooCommerce product categories to Google product types.
*
* @return $this
*/
protected function map_product_categories() {
// set product type using merchants defined product categories
$base_product_id = $this->is_variation() ? $this->parent_wc_product->get_id() : $this->wc_product->get_id();
// Fetch only selected term ids without parents.
$this->product_category_ids = wc_get_product_term_ids( $base_product_id, 'product_cat' );
if ( ! empty( $this->product_category_ids ) ) {
$google_product_types = self::convert_product_types( $this->product_category_ids );
do_action(
'woocommerce_gla_debug_message',
sprintf(
'Product category (ID: %s): %s.',
$base_product_id,
json_encode( $google_product_types )
),
__METHOD__
);
$this->setProductTypes( $google_product_types );
}
return $this;
}
/**
* Covert WooCommerce product categories to product_type, which follows Google requirements:
* https://support.google.com/merchants/answer/6324406?hl=en#
*
* @param int[] $category_ids
*
* @return array
*/
public static function convert_product_types( $category_ids ): array {
$product_types = [];
foreach ( array_unique( $category_ids ) as $category_id ) {
if ( ! is_int( $category_id ) ) {
continue;
}
$product_type = self::get_product_type_by_id( $category_id );
array_push( $product_types, $product_type );
}
return $product_types;
}
/**
* Return category names including ancestors, separated by ">"
*
* @param int $category_id
*
* @return string
*/
protected static function get_product_type_by_id( int $category_id ): string {
$category_names = [];
do {
$term = get_term_by( 'id', $category_id, 'product_cat', 'ARRAY_A' );
array_push( $category_names, $term['name'] );
$category_id = $term['parent'];
} while ( ! empty( $term['parent'] ) );
return implode( ' > ', array_reverse( $category_names ) );
}
/**
* Map the WooCommerce product ID.
*
* @return $this
*/
protected function map_wc_product_id(): WCProductAdapter {
$this->setOfferId( self::get_google_product_offer_id( $this->get_slug(), $this->wc_product->get_id() ) );
return $this;
}
/**
*
* @param string $slug
* @param int $product_id
* @return string
*/
public static function get_google_product_offer_id( string $slug, int $product_id ): string {
/**
* Filters a WooCommerce product ID to be used as the Merchant Center product ID.
*
* @param string $mc_product_id Default generated Merchant Center product ID.
* @param int $product_id WooCommerce product ID.
* @since 2.4.6
*
* @return string Merchant Center product ID corresponding to the given WooCommerce product ID.
*/
return apply_filters( 'woocommerce_gla_get_google_product_offer_id', "{$slug}_{$product_id}", $product_id );
}
/**
* Get the description for the WooCommerce product.
*
* @return string
*/
protected function get_wc_product_description(): string {
/**
* Filters whether the short product description should be used for the synced product.
*
* @param bool $use_short_description
*/
$use_short_description = apply_filters( 'woocommerce_gla_use_short_description', false );
$description = ! empty( $this->wc_product->get_description() ) && ! $use_short_description ?
$this->wc_product->get_description() :
$this->wc_product->get_short_description();
// prepend the parent product description to the variation product
if ( $this->is_variation() ) {
$parent_description = ! empty( $this->parent_wc_product->get_description() ) && ! $use_short_description ?
$this->parent_wc_product->get_description() :
$this->parent_wc_product->get_short_description();
$new_line = ! empty( $description ) && ! empty( $parent_description ) ? PHP_EOL : '';
$description = $parent_description . $new_line . $description;
}
/**
* Filters whether the shortcodes should be applied for product descriptions when syncing a product or be stripped out.
*
* @since 1.4.0
*
* @param bool $apply_shortcodes Shortcodes are applied if set to `true` and stripped out if set to `false`.
* @param WC_Product $wc_product WooCommerce product object.
*/
$apply_shortcodes = apply_filters( 'woocommerce_gla_product_description_apply_shortcodes', false, $this->wc_product );
if ( $apply_shortcodes ) {
// Apply active shortcodes
$description = do_shortcode( $description );
} else {
// Strip out active shortcodes
$description = strip_shortcodes( $description );
}
// Strip out invalid unicode.
$description = mb_convert_encoding( $description, 'UTF-8', 'UTF-8' );
$description = preg_replace(
'/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u',
'',
$description
);
// Strip out invalid HTML tags (e.g. script, style, canvas, etc.) along with attributes of all tags.
$valid_html_tags = array_keys( wp_kses_allowed_html( 'post' ) );
$kses_allowed_tags = array_fill_keys( $valid_html_tags, [] );
$description = wp_kses( $description, $kses_allowed_tags );
// Trim the description if it's more than 5000 characters.
$description = mb_substr( $description, 0, 5000, 'utf-8' );
/**
* Filters the product's description.
*
* @param string $description Product description.
* @param WC_Product $wc_product WooCommerce product object.
*/
return apply_filters( 'woocommerce_gla_product_attribute_value_description', $description, $this->wc_product );
}
/**
* Map the WooCommerce product images.
*
* @param string $image_size
*
* @return $this
*/
protected function map_wc_product_image( string $image_size ) {
$image_id = $this->wc_product->get_image_id();
$gallery_image_ids = $this->wc_product->get_gallery_image_ids() ?: [];
// check if we can use the parent product image if it's a variation
if ( $this->is_variation() ) {
$image_id = $image_id ?? $this->parent_wc_product->get_image_id();
$parent_gallery_images = $this->parent_wc_product->get_gallery_image_ids() ?: [];
$gallery_image_ids = ! empty( $gallery_image_ids ) ? $gallery_image_ids : $parent_gallery_images;
}
// use a gallery image as the main product image if no main image is available
if ( empty( $image_id ) && ! empty( $gallery_image_ids[0] ) ) {
$image_id = $gallery_image_ids[0];
// remove the recently set main image from the list of gallery images
unset( $gallery_image_ids[0] );
}
// set main image
$image_link = wp_get_attachment_image_url( $image_id, $image_size, false );
$this->setImageLink( $image_link );
// set additional images
$gallery_image_links = array_map(
function ( $gallery_image_id ) use ( $image_size ) {
return wp_get_attachment_image_url( $gallery_image_id, $image_size, false );
},
$gallery_image_ids
);
// Uniquify the set of additional images
$gallery_image_links = array_unique( $gallery_image_links, SORT_REGULAR );
// Limit additional image links up to 10
$gallery_image_links = array_slice( $gallery_image_links, 0, 10 );
$this->setAdditionalImageLinks( $gallery_image_links );
return $this;
}
/**
* Map the general WooCommerce product attributes.
*
* @return $this
*/
protected function map_wc_availability() {
if ( ! $this->wc_product->is_in_stock() ) {
$availability = self::AVAILABILITY_OUT_OF_STOCK;
} elseif ( $this->wc_product->is_on_backorder( 1 ) ) {
$availability = self::AVAILABILITY_BACKORDER;
} else {
$availability = self::AVAILABILITY_IN_STOCK;
}
$this->setAvailability( $availability );
return $this;
}
/**
* Map the shipping information for WooCommerce product.
*
* @return $this
*/
protected function map_wc_product_shipping(): WCProductAdapter {
$this->add_shipping_country( $this->getTargetCountry() );
if ( ! $this->is_virtual() ) {
$dimension_unit = apply_filters( 'woocommerce_gla_dimension_unit', get_option( 'woocommerce_dimension_unit' ) );
$weight_unit = apply_filters( 'woocommerce_gla_weight_unit', get_option( 'woocommerce_weight_unit' ) );
$this->map_wc_shipping_dimensions( $dimension_unit )
->map_wc_shipping_weight( $weight_unit );
}
// Set the product's shipping class slug as the shipping label.
$shipping_class = $this->wc_product->get_shipping_class();
if ( ! empty( $shipping_class ) ) {
$this->setShippingLabel( $shipping_class );
}
return $this;
}
/**
* Add a shipping country for the product.
*
* @param string $country
*/
public function add_shipping_country( string $country ): void {
$product_shipping = [
'country' => $country,
];
// Virtual products should override any country shipping cost.
if ( $this->is_virtual() ) {
$product_shipping['price'] = [
'currency' => get_woocommerce_currency(),
'value' => 0,
];
}
$new_shipping = [
new GoogleProductShipping( $product_shipping ),
];
if ( ! $this->shipping_country_exists( $country ) ) {
$current_shipping = $this->getShipping() ?? [];
$this->setShipping( array_merge( $current_shipping, $new_shipping ) );
}
}
/**
* Remove a shipping country from the product.
*
* @param string $country
*
* @since 1.2.0
*/
public function remove_shipping_country( string $country ): void {
$product_shippings = $this->getShipping() ?? [];
foreach ( $product_shippings as $index => $shipping ) {
if ( $country === $shipping->getCountry() ) {
unset( $product_shippings[ $index ] );
}
}
$this->setShipping( $product_shippings );
}
/**
* @param string $country
*
* @return bool
*/
protected function shipping_country_exists( string $country ): bool {
$current_shipping = $this->getShipping() ?? [];
foreach ( $current_shipping as $shipping ) {
if ( $country === $shipping->getCountry() ) {
return true;
}
}
return false;
}
/**
* Map the measurements for the WooCommerce product.
*
* @param string $unit
*
* @return $this
*/
protected function map_wc_shipping_dimensions( string $unit = 'cm' ): WCProductAdapter {
$length = $this->wc_product->get_length();
$width = $this->wc_product->get_width();
$height = $this->wc_product->get_height();
// Use cm if the unit isn't supported.
if ( ! in_array( $unit, [ 'in', 'cm' ], true ) ) {
$unit = 'cm';
}
$length = wc_get_dimension( (float) $length, $unit );
$width = wc_get_dimension( (float) $width, $unit );
$height = wc_get_dimension( (float) $height, $unit );
if ( $length > 0 && $width > 0 && $height > 0 ) {
$this->setShippingLength(
new GoogleProductShippingDimension(
[
'unit' => $unit,
'value' => $length,
]
)
);
$this->setShippingWidth(
new GoogleProductShippingDimension(
[
'unit' => $unit,
'value' => $width,
]
)
);
$this->setShippingHeight(
new GoogleProductShippingDimension(
[
'unit' => $unit,
'value' => $height,
]
)
);
}
return $this;
}
/**
* Map the weight for the WooCommerce product.
*
* @param string $unit
*
* @return $this
*/
protected function map_wc_shipping_weight( string $unit = 'g' ): WCProductAdapter {
// Use g if the unit isn't supported.
if ( ! in_array( $unit, [ 'g', 'lbs', 'oz' ], true ) ) {
$unit = 'g';
}
$weight = wc_get_weight( $this->wc_product->get_weight(), $unit );
$this->setShippingWeight(
new GoogleProductShippingWeight(
[
'unit' => $unit,
'value' => $weight,
]
)
);
return $this;
}
/**
* Sets whether tax is excluded from product price.
*
* @return $this
*/
protected function map_tax_excluded(): WCProductAdapter {
// tax is excluded from price in US and CA
$this->tax_excluded = in_array( $this->getTargetCountry(), [ 'US', 'CA' ], true );
$this->tax_excluded = boolval( apply_filters( 'woocommerce_gla_tax_excluded', $this->tax_excluded ) );
return $this;
}
/**
* Map the prices (base and sale price) for the product.
*
* @return $this
*/
protected function map_wc_prices(): WCProductAdapter {
$this->map_tax_excluded();
$this->map_wc_product_price( $this->wc_product );
return $this;
}
/**
* Map the prices (base and sale price) for a given WooCommerce product.
*
* @param WC_Product $product
*
* @return $this
*/
protected function map_wc_product_price( WC_Product $product ): WCProductAdapter {
// set regular price
$regular_price = $product->get_regular_price();
if ( '' !== $regular_price ) {
$price = $this->tax_excluded ?
wc_get_price_excluding_tax( $product, [ 'price' => $regular_price ] ) :
wc_get_price_including_tax( $product, [ 'price' => $regular_price ] );
/**
* Filters the calculated product price.
*
* @param float $price Calculated price of the product
* @param WC_Product $product WooCommerce product
* @param bool $tax_excluded Whether tax is excluded from product price
*/
$price = apply_filters( 'woocommerce_gla_product_attribute_value_price', $price, $product, $this->tax_excluded );
$this->setPrice(
new GooglePrice(
[
'currency' => get_woocommerce_currency(),
'value' => $price,
]
)
);
}
// set sale price
$this->map_wc_product_sale_price( $product );
return $this;
}
/**
* Map the sale price and sale effective date for a given WooCommerce product.
*
* @param WC_Product $product
*
* @return $this
*/
protected function map_wc_product_sale_price( WC_Product $product ): WCProductAdapter {
// Grab the sale price of the base product. Some plugins (Dynamic
// pricing as an example) filter the active price, but not the sale
// price. If the active price < the regular price treat it as a sale
// price.
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$active_price = $product->get_price();
if (
( empty( $sale_price ) && $active_price < $regular_price ) ||
( ! empty( $sale_price ) && $active_price < $sale_price )
) {
$sale_price = $active_price;
}
// set sale price and sale effective date if any
if ( '' !== $sale_price ) {
$sale_price = $this->tax_excluded ?
wc_get_price_excluding_tax( $product, [ 'price' => $sale_price ] ) :
wc_get_price_including_tax( $product, [ 'price' => $sale_price ] );
/**
* Filters the calculated product sale price.
*
* @param float $sale_price Calculated sale price of the product
* @param WC_Product $product WooCommerce product
* @param bool $tax_excluded Whether tax is excluded from product price
*/
$sale_price = apply_filters( 'woocommerce_gla_product_attribute_value_sale_price', $sale_price, $product, $this->tax_excluded );
// If the sale price dates no longer apply, make sure we don't include a sale price.
$now = new WC_DateTime();
$sale_price_end_date = $product->get_date_on_sale_to();
if ( empty( $sale_price_end_date ) || $sale_price_end_date >= $now ) {
$this->setSalePrice(
new GooglePrice(
[
'currency' => get_woocommerce_currency(),
'value' => $sale_price,
]
)
);
$this->setSalePriceEffectiveDate( $this->get_wc_product_sale_price_effective_date( $product ) );
}
}
return $this;
}
/**
* Return the sale effective dates for the WooCommerce product.
*
* @param WC_Product $product
*
* @return string|null
*/
protected function get_wc_product_sale_price_effective_date( WC_Product $product ): ?string {
$start_date = $product->get_date_on_sale_from();
$end_date = $product->get_date_on_sale_to();
$now = new WC_DateTime();
// if we have a sale end date in the future, but no start date, set the start date to now()
if (
! empty( $end_date ) &&
$end_date > $now &&
empty( $start_date )
) {
$start_date = $now;
}
// if we have a sale start date in the past, but no end date, do not include the start date.
if (
! empty( $start_date ) &&
$start_date < $now &&
empty( $end_date )
) {
$start_date = null;
}
// if we have a start date in the future, but no end date, assume a one-day sale.
if (
! empty( $start_date ) &&
$start_date > $now &&
empty( $end_date )
) {
$end_date = clone $start_date;
$end_date->add( new DateInterval( 'P1D' ) );
}
if ( empty( $start_date ) && empty( $end_date ) ) {
return null;
}
return sprintf( '%s/%s', (string) $start_date, (string) $end_date );
}
/**
* Return whether the WooCommerce product is a variation.
*
* @return bool
*/
public function is_variation(): bool {
return $this->wc_product instanceof WC_Product_Variation;
}
/**
* Return whether the WooCommerce product is virtual.
*
* @return bool
*/
public function is_virtual(): bool {
$is_virtual = $this->wc_product->is_virtual();
/**
* Filters the virtual property value of a product.
*
* @param bool $is_virtual Whether a product is virtual
* @param WC_Product $product WooCommerce product
*/
$is_virtual = apply_filters( 'woocommerce_gla_product_property_value_is_virtual', $is_virtual, $this->wc_product );
return false !== $is_virtual;
}
/**
* @param ClassMetadata $metadata
*/
public static function load_validator_metadata( ClassMetadata $metadata ) {
$metadata->addPropertyConstraint( 'offerId', new Assert\NotBlank() );
$metadata->addPropertyConstraint( 'title', new Assert\NotBlank() );
$metadata->addPropertyConstraint( 'description', new Assert\NotBlank() );
$metadata->addPropertyConstraint( 'link', new Assert\NotBlank() );
$metadata->addPropertyConstraint( 'link', new Assert\Url() );
$metadata->addPropertyConstraint( 'imageLink', new Assert\NotBlank() );
$metadata->addPropertyConstraint( 'imageLink', new ImageUrlConstraint() );
$metadata->addPropertyConstraint(
'additionalImageLinks',
new Assert\All(
[
'constraints' => [ new ImageUrlConstraint() ],
]
)
);
$metadata->addGetterConstraint( 'price', new Assert\NotNull() );
$metadata->addGetterConstraint( 'price', new GooglePriceConstraint() );
$metadata->addGetterConstraint( 'salePrice', new GooglePriceConstraint() );
$metadata->addConstraint( new Assert\Callback( 'validate_item_group_id' ) );
$metadata->addConstraint( new Assert\Callback( 'validate_availability' ) );
$metadata->addPropertyConstraint( 'gtin', new Assert\Regex( '/^\d{8}(?:\d{4,6})?$/' ) );
$metadata->addPropertyConstraint( 'mpn', new Assert\Type( 'string' ) );
$metadata->addPropertyConstraint( 'mpn', new Assert\Length( null, 0, 70 ) ); // maximum 70 characters
$metadata->addPropertyConstraint(
'sizes',
new Assert\All(
[
'constraints' => [
new Assert\Type( 'string' ),
new Assert\Length( null, 0, 100 ), // maximum 100 characters
],
]
)
);
$metadata->addPropertyConstraint( 'sizeSystem', new Assert\Choice( array_keys( SizeSystem::get_value_options() ) ) );
$metadata->addPropertyConstraint( 'sizeType', new Assert\Choice( array_keys( SizeType::get_value_options() ) ) );
$metadata->addPropertyConstraint( 'color', new Assert\Length( null, 0, 100 ) ); // maximum 100 characters
$metadata->addPropertyConstraint( 'material', new Assert\Length( null, 0, 200 ) ); // maximum 200 characters
$metadata->addPropertyConstraint( 'pattern', new Assert\Length( null, 0, 100 ) ); // maximum 200 characters
$metadata->addPropertyConstraint( 'ageGroup', new Assert\Choice( array_keys( AgeGroup::get_value_options() ) ) );
$metadata->addPropertyConstraint( 'adult', new Assert\Type( 'boolean' ) );
$metadata->addPropertyConstraint( 'condition', new Assert\Choice( array_keys( Condition::get_value_options() ) ) );
$metadata->addPropertyConstraint( 'multipack', new Assert\Type( 'integer' ) );
$metadata->addPropertyConstraint( 'multipack', new Assert\PositiveOrZero() );
$metadata->addPropertyConstraint( 'isBundle', new Assert\Type( 'boolean' ) );
}
/**
* Used by the validator to check if the variation product has an itemGroupId
*
* @param ExecutionContextInterface $context
*/
public function validate_item_group_id( ExecutionContextInterface $context ) {
if ( $this->is_variation() && empty( $this->getItemGroupId() ) ) {
$context->buildViolation( 'ItemGroupId needs to be set for variable products.' )
->atPath( 'itemGroupId' )
->addViolation();
}
}
/**
* Used by the validator to check if the availability date is set for product available as `backorder` or
* `preorder`.
*
* @param ExecutionContextInterface $context
*/
public function validate_availability( ExecutionContextInterface $context ) {
if (
( self::AVAILABILITY_BACKORDER === $this->getAvailability() || self::AVAILABILITY_PREORDER === $this->getAvailability() ) &&
empty( $this->getAvailabilityDate() )
) {
$context->buildViolation( 'Availability date is required if you set the product\'s availability to backorder or pre-order.' )
->atPath( 'availabilityDate' )
->addViolation();
}
}
/**
* @return WC_Product
*/
public function get_wc_product(): WC_Product {
return $this->wc_product;
}
/**
* @param array $attributes Attribute values
*
* @return $this
*/
protected function map_gla_attributes( array $attributes ): WCProductAdapter {
$gla_attributes = [];
foreach ( $attributes as $attribute_id => $attribute_value ) {
if ( property_exists( $this, $attribute_id ) ) {
/**
* Filters a product attribute's value.
*
* This only applies to the extra attributes defined in `AttributeManager::ATTRIBUTES`
* like GTIN, MPN, Brand, Size, etc. and it cannot modify other product attributes.
*
* This filter also cannot add or set a new attribute or modify one that isn't currently
* set for the product through WooCommerce's edit product page
*
* In order to override all product attributes and/or set new ones for the product use the
* `woocommerce_gla_product_attribute_values` filter.
*
* Note that the `woocommerce_gla_product_attribute_values` filter takes precedence over
* this filter, and it can be used to override any values defined here.
*
* @param mixed $attribute_value The attribute's current value
* @param WC_Product $wc_product The WooCommerce product object.
*
* @see AttributeManager::ATTRIBUTES for the list of attributes that their values can be modified using this filter.
* @see WCProductAdapter::override_attributes for the docuemntation of the `woocommerce_gla_product_attribute_values` filter.
*/
$gla_attributes[ $attribute_id ] = apply_filters( "woocommerce_gla_product_attribute_value_{$attribute_id}", $attribute_value, $this->get_wc_product() );
}
}
parent::mapTypes( $gla_attributes );
// Size
if ( ! empty( $attributes['size'] ) ) {
$this->setSizes( [ $attributes['size'] ] );
}
return $this;
}
/**
* @param string $targetCountry
*
* phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
*/
public function setTargetCountry( $targetCountry ) {
// remove shipping for current target country
$this->remove_shipping_country( $this->getTargetCountry() );
// set the new target country
parent::setTargetCountry( $targetCountry );
// we need to reset the prices because tax is based on the country
$this->map_wc_prices();
// product shipping information is also country based
$this->map_wc_product_shipping();
}
/**
* Performs the attribute mapping.
* This function applies rules setting values for the different attributes in the product.
*
* @param array $mapping_rules The set of rules to apply
*/
protected function map_attribute_mapping_rules( array $mapping_rules ) {
$attributes = [];
if ( empty( $mapping_rules ) ) {
return $this;
}
foreach ( $mapping_rules as $mapping_rule ) {
if ( $this->rule_match_conditions( $mapping_rule ) ) {
$attribute_id = $mapping_rule['attribute'];
$attributes[ $attribute_id ] = $this->format_attribute(
apply_filters(
"woocommerce_gla_product_attribute_value_{$attribute_id}",
$this->get_source( $mapping_rule['source'] ),
$this->get_wc_product()
),
$attribute_id
);
}
}
parent::mapTypes( $attributes );
// Size
if ( ! empty( $attributes['size'] ) ) {
$this->setSizes( [ $attributes['size'] ] );
}
return $this;
}
/**
* Get a source value for attribute mapping
*
* @param string $source The source to get the value
* @return string The source value for this product
*/
protected function get_source( string $source ) {
$source_type = null;
$type_separator = strpos( $source, ':' );
if ( $type_separator ) {
$source_type = substr( $source, 0, $type_separator );
$source_value = substr( $source, $type_separator + 1 );
}
// Detect if the source_type is kind of product, taxonomy or attribute. Otherwise, we take it the full source as a static value.
switch ( $source_type ) {
case 'product':
return $this->get_product_field( $source_value );
case 'taxonomy':