From 4f91e78591a60837e2b5024bfa63859bbe793696 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 13 Jun 2022 18:21:40 +0300 Subject: [PATCH] Do not fail if no WC_Product in order item It is possible for an order item to not have product, such as if created by a plugin. --- .../src/Factory/ItemFactory.php | 13 ++++--------- .../ApiClient/Factory/ItemFactoryTest.php | 18 +++++++++--------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/modules/ppcp-api-client/src/Factory/ItemFactory.php b/modules/ppcp-api-client/src/Factory/ItemFactory.php index ea639e704..ca21f4769 100644 --- a/modules/ppcp-api-client/src/Factory/ItemFactory.php +++ b/modules/ppcp-api-client/src/Factory/ItemFactory.php @@ -124,11 +124,6 @@ class ItemFactory { * @return Item */ private function from_wc_order_line_item( \WC_Order_Item_Product $item, \WC_Order $order ): Item { - /** - * The WooCommerce product. - * - * @var WC_Product $product - */ $product = $item->get_product(); $currency = $order->get_currency(); $quantity = (int) $item->get_quantity(); @@ -139,13 +134,13 @@ class ItemFactory { $tax = new Money( $tax, $currency ); return new Item( - mb_substr( $product->get_name(), 0, 127 ), + mb_substr( $item->get_name(), 0, 127 ), new Money( $price_without_tax_rounded, $currency ), $quantity, - substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ) ?: '', + substr( wp_strip_all_tags( $product instanceof WC_Product ? $product->get_description() : '' ), 0, 127 ) ?: '', $tax, - $product->get_sku(), - ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS + $product instanceof WC_Product ? $product->get_sku() : '', + ( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS ); } diff --git a/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php b/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php index 0decea0f8..6b380e50c 100644 --- a/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php +++ b/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php @@ -130,9 +130,6 @@ class ItemFactoryTest extends TestCase $testee = new ItemFactory($this->currency); $product = Mockery::mock(\WC_Product::class); - $product - ->expects('get_name') - ->andReturn('name'); $product ->expects('get_description') ->andReturn('description'); @@ -150,6 +147,9 @@ class ItemFactoryTest extends TestCase $item ->expects('get_product') ->andReturn($product); + $item + ->expects('get_name') + ->andReturn('name'); $item ->expects('get_quantity') ->andReturn(1); @@ -193,9 +193,6 @@ class ItemFactoryTest extends TestCase $testee = new ItemFactory($this->currency); $product = Mockery::mock(\WC_Product::class); - $product - ->expects('get_name') - ->andReturn('name'); $product ->expects('get_description') ->andReturn('description'); @@ -213,6 +210,9 @@ class ItemFactoryTest extends TestCase $item ->expects('get_product') ->andReturn($product); + $item + ->expects('get_name') + ->andReturn('name'); $item ->expects('get_quantity') ->andReturn(1); @@ -251,9 +251,6 @@ class ItemFactoryTest extends TestCase $testee = new ItemFactory($this->currency); $product = Mockery::mock(\WC_Product::class); - $product - ->expects('get_name') - ->andReturn($name); $product ->expects('get_description') ->andReturn($description); @@ -271,6 +268,9 @@ class ItemFactoryTest extends TestCase $item ->expects('get_product') ->andReturn($product); + $item + ->expects('get_name') + ->andReturn($name); $item ->expects('get_quantity') ->andReturn(1);