Url of last order of current user
سؤال
I need to implement a link which redirects to the url of the last order of the current user
How do I do that ?
I have a code that load the last order, but I need to get the URL
$orders = $this->_orderCollectionFactory->create();
$orders->addFieldToFilter('customer_id', $customerId);
$orders->addOrder('customer_id');
$orderId = $orders->getLastItem()->getData('entity_id');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
المحلول
Try the below code:
$orders = $this->_orderCollectionFactory->create();
$orders->addFieldToFilter('customer_id', $customerId);
$orders->addOrder('customer_id');
$orderId = $orders->getLastItem()->getData('entity_id');
$url = $block->getUrl('sales/order/view', ['order_id' => $orderId]);
And use this in phtml like below:
<a href="<?php echo $url; ?>">View Last Order</a>
نصائح أخرى
You can get the order url as given below.
$this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
لا تنتمي إلى magento.stackexchange