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.