Domanda

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);
È stato utile?

Soluzione

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>

Altri suggerimenti

You can get the order url as given below.

$this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top