cancel
Showing results for 
Search instead for 
Did you mean: 

getAllVisibleItems shows both configurable and parent products?

getAllVisibleItems shows both configurable and parent products?

We have a custom observer that executes after "sales_order_place_after" that compiles a list of purchased items from the order and sends them to a 3rd party.

 

This script worked fine in Magento 1, but since upgrading to Magento 2, we're seeing both the configurable product and its parent product being listed - the code is as follows:

 

public function execute(\Magento\Framework\Event\Observer $observer) {
$order = $observer->getOrder(); foreach($order->getAllVisibleItems() as $item) { // Build XML } }

 

The resulting XML looks like this:

 

<item>
  <price>7.90</price>
  <sku>THE_SKU_NUMBER</sku>
  <qty>1</qty>
</item>
<item>
  <price>0.00</price>
  <sku>THE_SKU_NUMBER</sku>
  <qty>1</qty>
</item>

 

 Again, the "getAllVisibleItems" function worked fine in Magento 1, and as outlined by this stack exchange post:

 

https://magento.stackexchange.com/questions/111112/magento2-correct-way-to-get-order-items

 

...I understand that it should work similarly in Magento 2, however the user in that post also had a similar issue with duplicate items being listed - is there a different function we should be using to NOT show the parent product, and only show the configurable product that was actually purchased?

 

Note that I'd like to not have to check for a "0.00" price here - that seems like a workaround and not an actual solution to the issue.

 

Any information is helpful, thank you.

3 REPLIES

Re: getAllVisibleItems shows both configurable and parent products?

We have experienced the same issue. Would be good to get an answer from Magento.

Re: getAllVisibleItems shows both configurable and parent products?

Does anyone have any suggestions on how to restore this functionality or change our code to make it work as expected?

 

We currently have the fix in place that I didn't want to do originally (only including products with a price greater than "0.00"), but this is becoming an issue since we will be offering free products via coupon codes, and they will be skipped with this in place.

 

Please advise.

Re: getAllVisibleItems shows both configurable and parent products?

Hi, 

Here is the solution to your issue: 

getAllVisibleItems: How To Show Only Configurable Products & Hide Parent Products?

Hope that you find it useful.

 

[ Tigren - Elite Magento E-commerce Solutions Provider - www.tigren.com ]