Skip to main content
edited title
Link
devhs
  • 527
  • 10
  • 36

Magento 2 : Getting more products thenGet ordered item

Source Link
devhs
  • 527
  • 10
  • 36

Magento 2 : Getting more products then ordered

I have used sales_order_place_after observer .

i am trying to get product items but i get more items in data for exg. i have ordered 2 items but i am getting 4 items in data

foreach ($order->getAllItems() as $item) {
            $imageUrl = $helperImport->init($item->getProduct(), 'product_page_image_small')
                ->setImageFile($item->getProduct()->getSmallImage()) // image,small_image,thumbnail
                ->resize(380)
                ->getUrl();
            $product    = $item->getProduct();
            $products[] = array(
                'magento_product_id' => $product->getId(),
                'product_id'         => $product->getSku(),
                'brand'              => $product->getAttributeText('display_brand'),
                'price'              => $product->getFinalPrice(),
                'regular_price'      => $product->getPrice(),
                'special_price'      => $product->getSpecialPrice(),
                'url'                => $product->getProductUrl(),
                'size_type'          => (strpos($item->getProduct()->getProductUrl(), "plus-size") === false) ? 'Regular' : 'Plus Size',
                'manufacturer'       => $product->getManufacturer(),
                'imageUrl'           => $imageUrl,
                'category'           => $product->getName(),
                'special_category'   => $product->getName(),
                'doll_category'      => $product->getName(),
                'stock_quantity'     => $product->getQty(),
                'name'               => $product->getName(),
                'price'              => $product->getPrice(),
                'sku'                => $product->getSku(),
                'size'               => $product->getAttributeText('size'),
                'swatch'             => $product->getAttributeText('color'),
                'trend'              => $product->getAttributeText('trend'),
                'quantity'           => (float) $item->getQtyOrdered(),
            );

            // or you can retrieve the product object
            $product[] = $item->getProduct();
        }
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/products.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($products);
        print_r($data);exit;