0

I need to display downloadable products on the customer account page. I create one tab after the order tab on the customer account page.

I want to get customer ordered product list in this page.

I create a custom block and I wrote all queries and I want to get product collection and customer it inside the .phtml file.

<?php
namespace Vendor\Module\Block;
use Magento\Downloadable\Api\Data\LinkInterface;
use Magento\Downloadable\Api\LinkRepositoryInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
class CustomTabs extends \Magento\Framework\View\Element\Template
{

 
    /**
     * @var array
     */
    private $data;
    /**
     * @var Context
     */
    private $context;
 
    /**
     * @var Session
     */
    protected $customer;

    /**
     * @var CollectionFactory
     */
    private $_orderCollectionFactory;

    protected $_productRepository;

    public function __construct(
        \Magento\Customer\Model\Session $customer,
        LinkRepositoryInterface $linkRepository,
        CollectionFactory $orderCollectionFactory,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
        )
    {

        parent::__construct($context, $data);
        $this->linkRepository = $linkRepository;
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->_productRepository = $productRepository;
        $this->customer = $customer;
    }
    

    /**
     * Get list of links of Downloadable Product.
     *
     * 
     * @return LinkInterface[]
     */

    public function getCustomerData(){
       $data = [];
       $dataitem = [];
        $customer = $this->customer;
        $customerId = $customer->getId(); 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    
            
        $orders = $this->_orderCollectionFactory->create()
            ->addFieldToFilter('customer_id', $customerId)->addAttributeToSelect('entity_id');
         foreach($orders as $order){
            $orderId = $order->getId(); 
            
            $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
            $orderItemsCollection = $order->getAllVisibleItems();
            
            foreach ($orderItemsCollection as $item){ 
               $productId = $item['product_id'];
              $productdata = $this->_productRepository->getById($productId);

                    foreach($productdata as $product){
                        $sku = $product['sku'];
                           $data[] = $product['sku'];
                        $downloadableLink = $this->linkRepository->getList($sku);
                        foreach ($downloadableLink as $link ) {
                           //echo "<pre>"; var_dump($link->getData());
                              $data[] = $link->debug();

                             //$data[$key]["downloadablelinks"]= $link['id'];
                        }

                   } 
                 
                
            }         
         }

    return $data; 
}

}

I get the last foreach loop data but the way I get it's totally wrong. can someone please give the suggestion to how I can get it. the product collection and order collection in my .phtml file.

1 Answer 1

1

I try to help a bit

use Magento\Quote\Model\Quote\Item;
use Magento\Sales\Model\Order;
use Magento\Quote\Model\Quote;

class Test
{
    public function getCustomerData()
    {
        $orders = []; // remove this line with your code

        /** @var Order $order */
        foreach($orders as $order) {
            /** @var Quote $quote */
            $quote = $this->quoteFactory->create()->load($order->getQuoteId());

            /** @var Item $item */
            foreach ($quote->getAllItems() as $item) {
                $downloadableLink = $this->linkRepository->getList($item->getProduct()->getSku());
                foreach ($downloadableLink as $link) {
                    // do whatever you want to do here
                }
            }
        }
    }
}

The points in your code are as follows

You already have a order object here, you are doing stuff twice

$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);

You are not taking the correct approach to reach the product, The correct approach is as follows

OrderCollection >> Order >> Quote >> QuoteItem >> Product >> Product Attributes
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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