Skip to content

Commit 464894d

Browse files
authored
Merge pull request #159 from BeAPI/issue/84660
Check for reference alias in itemsReferencesAliases when importing order
2 parents 5deffb1 + 5dccbd7 commit 464894d

8 files changed

+615
-13
lines changed

src/Orders/Order/Products.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ public function __construct( $sf_order, $include_vat = false ) {
4545
* Set Products
4646
*/
4747
private function set_products() {
48-
$this->products = array();
48+
$this->products = array();
49+
$references_aliases = $this->sf_order->getItemsReferencesAliases();
4950
foreach ( $this->sf_order->getItems() as $sf_product ) {
50-
$product = $this->mapping_product( $sf_product );
51+
$product = $this->mapping_product( $sf_product, $references_aliases );
5152
if ( empty( $product ) ) {
5253
ShoppingFeedHelper::get_logger()->error(
5354
sprintf(
54-
/* translators: %1$1s: Product reference. %2$2s: Order id. */
55-
__( 'cant match product %1$1s => in order %2$2s', 'shopping-feed' ),
55+
/* translators: %1$s: product reference or alias, %2$s: original product reference, %3$s: order id. */
56+
__( 'Can\'t match product "%1$s" (original ref: %2$s) in order %3$s', 'shopping-feed' ),
57+
( $references_aliases[ $sf_product->getReference() ] ?? $sf_product->getReference() ),
5658
$sf_product->getReference(),
5759
$this->sf_order->getId()
5860
),
@@ -62,19 +64,22 @@ private function set_products() {
6264
);
6365
continue;
6466
}
65-
$this->products[] = $this->mapping_product( $sf_product );
67+
68+
$this->products[] = $product;
6669
}
6770
}
6871

6972
/**
70-
* @param $sf_product OrderItem
73+
* Map products in SF order to Woocommerce products.
74+
*
75+
* @param OrderItem $sf_product
76+
* @param array $references_aliases
7177
*
7278
* @return array
7379
*/
74-
private function mapping_product( $sf_product ) {
75-
80+
private function mapping_product( $sf_product, $references_aliases = [] ) {
7681
$product_identifier = ShoppingFeedHelper::get_sf_feed_product_identifier();
77-
$wc_product_id = $sf_product->getReference();
82+
$wc_product_id = $references_aliases[ $sf_product->getReference() ] ?? $sf_product->getReference();
7883

7984
if ( 'sku' === $product_identifier ) {
8085
$wc_product_id = wc_get_product_id_by_sku( $wc_product_id );
@@ -116,6 +121,7 @@ private function mapping_product( $sf_product ) {
116121
'args' => $args,
117122
'is_available' => $wc_product->is_in_stock() && $wc_product->has_enough_stock( $sf_product_quantity ),
118123
'sf_ref' => $sf_product->getReference(),
124+
'wc_ref' => $references_aliases[ $sf_product->getReference() ] ?? $sf_product->getReference(),
119125
);
120126
}
121127

tests/wpunit/Feed/ProductFeedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function test_variable_product_return_variation_array() {
9898
/**
9999
* @covers \ShoppingFeed\ShoppingFeedWC\Products\Product::get_ean
100100
*/
101-
public function test_get_ean_return_emty_string_for_empty_wc_product_ean() {
101+
public function test_get_ean_return_empty_string_for_empty_wc_product_ean() {
102102
$wc_product = wc_get_product( 13 );
103103
$sf_product = new Product( $wc_product );
104104
$this->assertEquals( '', $sf_product->get_ean() );

tests/wpunit/Order/OrderImportHposTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,102 @@ public function test_code_sn_nif_meta_exists(): void {
107107
$this->assertEmpty( $wc_order->get_meta( 'sf_nif' ) );
108108
}
109109

110+
/**
111+
* Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
112+
*
113+
* This test case is for when the feed is configured to use the ID as the product identifier.
114+
*
115+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
116+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
117+
*/
118+
public function test_order_with_id_reference_alias() {
119+
$order_resource = $this->get_order_resource( 'simple-order-id-reference-alias' );
120+
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
121+
$sf_order->add();
122+
123+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
124+
$wc_order = reset( $results );
125+
$items = $wc_order->get_items();
126+
127+
$this->assertEquals( 1, $wc_order->get_item_count(), 'Assert the order contain the same number of product from ShoppingFeed' );
128+
$this->assertEquals( 'Hoodie with Logo', reset( $items )->get_name(), 'Assert the order contain the same name of product from ShoppingFeed' );
129+
}
130+
131+
/**
132+
* Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
133+
*
134+
* This test case is for when the feed is configured to use the ID as the product identifier.
135+
*
136+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
137+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
138+
*/
139+
public function test_order_with_unknown_id_reference_alias() {
140+
$order_resource = $this->get_order_resource( 'simple-order-id-reference-alias-invalid' );
141+
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
142+
$sf_order->add();
143+
144+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
145+
146+
$this->assertEmpty( $results );
147+
}
148+
149+
/**
150+
* Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
151+
*
152+
* This test case is for when the feed is configured to use the SKU as the product identifier.
153+
*
154+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
155+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
156+
*/
157+
public function test_order_with_sku_reference_alias() {
158+
add_filter(
159+
'pre_option_sf_feed_options',
160+
function ( $value ) {
161+
return [
162+
'product_identifier' => 'sku',
163+
];
164+
}
165+
);
166+
167+
$order_resource = $this->get_order_resource( 'simple-order-sku-reference-alias' );
168+
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
169+
$sf_order->add();
170+
171+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
172+
$wc_order = reset( $results );
173+
$items = $wc_order->get_items();
174+
175+
$this->assertEquals( 1, $wc_order->get_item_count(), 'Assert the order contain the same number of product from ShoppingFeed' );
176+
$this->assertEquals( 'Hoodie with Logo', reset( $items )->get_name(), 'Assert the order contain the same name of product from ShoppingFeed' );
177+
}
178+
179+
/**
180+
* Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
181+
*
182+
* This test case is for when the feed is configured to use the SKU as the product identifier.
183+
*
184+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
185+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
186+
*/
187+
public function test_order_with_unknown_sku_reference_alias() {
188+
add_filter(
189+
'pre_option_sf_feed_options',
190+
function ( $value ) {
191+
return [
192+
'product_identifier' => 'sku',
193+
];
194+
}
195+
);
196+
197+
$order_resource = $this->get_order_resource( 'simple-order-sku-reference-alias-invalid' );
198+
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
199+
$sf_order->add();
200+
201+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
202+
203+
$this->assertEmpty( $results );
204+
}
205+
110206
public function custom_orders_table( $value ) {
111207
return 'yes';
112208
}

tests/wpunit/Order/OrderImportLegacyTest.php

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function test_orders_are_not_created_in_woocommerce_if_they_contain_out_o
5151
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
5252
$sf_order->add();
5353

54-
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
54+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
5555
$this->assertEmpty( $results );
5656
}
5757

@@ -84,15 +84,15 @@ public function test_order_exist() {
8484
$this->assertFalse( ShoppingFeed\ShoppingFeedWC\Orders\Order::exists( $order_resource_bis ) );
8585
}
8686

87-
public function test_code_nif_is_imported () : void {
87+
public function test_code_nif_is_imported(): void {
8888
$order_resource = $this->get_order_resource( 'order-sf-nif' );
8989
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
9090
$sf_order->add();
9191

9292
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
9393
$wc_order = reset( $results );
9494

95-
$this->assertEquals('210474114', $wc_order->get_meta( 'sf_nif' ));
95+
$this->assertEquals( '210474114', $wc_order->get_meta( 'sf_nif' ) );
9696
}
9797

9898
public function test_code_sn_nif_meta_exists(): void {
@@ -106,6 +106,102 @@ public function test_code_sn_nif_meta_exists(): void {
106106
$this->assertEmpty( $wc_order->get_meta( 'sf_nif' ) );
107107
}
108108

109+
/**
110+
* Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
111+
*
112+
* This test case is for when the feed is configured to use the ID as the product identifier.
113+
*
114+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
115+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
116+
*/
117+
public function test_order_with_id_reference_alias() {
118+
$order_resource = $this->get_order_resource( 'simple-order-id-reference-alias' );
119+
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
120+
$sf_order->add();
121+
122+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
123+
$wc_order = reset( $results );
124+
$items = $wc_order->get_items();
125+
126+
$this->assertEquals( 1, $wc_order->get_item_count(), 'Assert the order contain the same number of product from ShoppingFeed' );
127+
$this->assertEquals( 'Hoodie with Logo', reset( $items )->get_name(), 'Assert the order contain the same name of product from ShoppingFeed' );
128+
}
129+
130+
/**
131+
* Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
132+
*
133+
* This test case is for when the feed is configured to use the ID as the product identifier.
134+
*
135+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
136+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
137+
*/
138+
public function test_order_with_unknown_id_reference_alias() {
139+
$order_resource = $this->get_order_resource( 'simple-order-id-reference-alias-invalid' );
140+
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
141+
$sf_order->add();
142+
143+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
144+
145+
$this->assertEmpty( $results );
146+
}
147+
148+
/**
149+
* Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
150+
*
151+
* This test case is for when the feed is configured to use the SKU as the product identifier.
152+
*
153+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
154+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
155+
*/
156+
public function test_order_with_sku_reference_alias() {
157+
add_filter(
158+
'pre_option_sf_feed_options',
159+
function ( $value ) {
160+
return [
161+
'product_identifier' => 'sku',
162+
];
163+
}
164+
);
165+
166+
$order_resource = $this->get_order_resource( 'simple-order-sku-reference-alias' );
167+
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
168+
$sf_order->add();
169+
170+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
171+
$wc_order = reset( $results );
172+
$items = $wc_order->get_items();
173+
174+
$this->assertEquals( 1, $wc_order->get_item_count(), 'Assert the order contain the same number of product from ShoppingFeed' );
175+
$this->assertEquals( 'Hoodie with Logo', reset( $items )->get_name(), 'Assert the order contain the same name of product from ShoppingFeed' );
176+
}
177+
178+
/**
179+
* Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
180+
*
181+
* This test case is for when the feed is configured to use the SKU as the product identifier.
182+
*
183+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
184+
* @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
185+
*/
186+
public function test_order_with_unknown_sku_reference_alias() {
187+
add_filter(
188+
'pre_option_sf_feed_options',
189+
function ( $value ) {
190+
return [
191+
'product_identifier' => 'sku',
192+
];
193+
}
194+
);
195+
196+
$order_resource = $this->get_order_resource( 'simple-order-sku-reference-alias-invalid' );
197+
$sf_order = new ShoppingFeed\ShoppingFeedWC\Orders\Order( $order_resource );
198+
$sf_order->add();
199+
200+
$results = wc_get_orders( [ Query::WC_META_SF_REFERENCE => $order_resource->getReference() ] );
201+
202+
$this->assertEmpty( $results );
203+
}
204+
109205
public function custom_orders_table( $value ) {
110206
return 'no';
111207
}

0 commit comments

Comments
 (0)