0

I've seen a few similar questions here when i searched but they all pertain to magento 1.

This seems identical do what i want to do..

Redirect "Continue Shopping" Button to the Current Product Category it was added from?

I see their using the Mage class which is now the object manager but is obv. frowned upon to use in magento 2. Any help would be greatly appreciated.

1 Answer 1

0

Try below code in vendor/magento/module-checkout/view/frontend/templates/cart/form.phtml

<?php if ($block->getContinueShoppingUrl()) :?>
            <?php 
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');
                $items = $checkoutSession->getQuote()->getAllItems();
                $max = 0;
                $lastItem = null;
                foreach ($items as $item){
                    if ($item->getId() > $max) {
                        $max = $item->getId();
                        $lastItem = $item;
                    }
                }
                if ($lastItem) {
                    $lastCategory = $lastItem->getCategoryCollection()
                                                ->addAttributeToSelect('*')
                                                ->addOrder('level');
                }
                if ($lastCategory) {
                    $categoryUrl = $lastCategory->getFirstItem()->getUrl();
                }
                if ($categoryUrl) {
            ?>
                <a class="action continue"
                href="<?= $block->escapeUrl($categoryUrl) ?>"
                title="<?= $block->escapeHtmlAttr(__('Continue Shopping')) ?>">
                    <span><?= $block->escapeHtml(__('Continue Shopping')) ?></span>
                </a>
            <?php
                } else {
            ?>
                <a class="action continue"
                href="<?= $block->escapeUrl($block->getContinueShoppingUrl()) ?>"
                title="<?= $block->escapeHtmlAttr(__('Continue Shopping')) ?>">
                    <span><?= $block->escapeHtml(__('Continue Shopping')) ?></span>
                </a>
            <?php
                }
            ?>
        <?php endif; ?>

NOTE: Replace object manager with collections as it is not recommended in Magento

3
  • Replace the first line with $collection instead of $objectManager?
    – Head
    Sep 22, 2022 at 14:19
  • Shouldn't i override this instead of edit vendor? @Arun Sharma
    – Head
    Sep 22, 2022 at 14:20
  • @Supplement yes you have to override it in your theme or custom module. Editing vendor file is not recommended. Instead of using object manager directly you have to use construct to get Session, category collection. You can create a block and call that in your phtml instead of using object manager. Sep 23, 2022 at 4:30

Your Answer

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

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