0

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;
1
  • any thoughts on this?
    – devhs
    Mar 9 at 7:07
0

try this

foreach ($order->getAllVisibleItems() as $item) {
     ////// your code
}
5
  • still getting 4 products
    – devhs
    Mar 9 at 7:19
  • i am getting both products, config & simple also
    – devhs
    Mar 9 at 7:20
  • any thoughts ??
    – devhs
    Mar 9 at 7:28
  • Please show order product or tell me which product type ?
    – Msquare
    Mar 9 at 9:26
  • I needed to get only ordered products i.e simple products
    – devhs
    Mar 9 at 9:57
0

You can try like this:

$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
    if (!$item->getParentItem()) continue;
    // rest of work
}
1
  • Thanks for the answer. let me check
    – devhs
    Mar 9 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.