0

I want to rewrite Reorder controller from \Magento\Sales\Controller\AbstractController\Reorder. In di.xml I put

<preference for = "Magento\Sales\Controller\AbstractController\Reorder" type = "Vendor\Module\Controller\AbstractController\CustomReorder"/>

And in the /Controller/AbstractController/CustomReorder.php I have:

<?php

namespace Vendor\Module\Controller\AbstractController;

class CustomReorder extends \Magento\Sales\Controller\AbstractController\Reorder
{
    /**
     * Action for reorder
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $result = $this->orderLoader->load($this->_request);
        if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
            return $result;
        }
        $order = $this->_coreRegistry->registry('current_order');
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();

        /* @var $cart \Magento\Checkout\Model\Cart */
        $cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
        $items = $order->getItemsCollection();
        error_log( 'MY STUFF: ' . print_r( "TEST", true ) );
        foreach ($items as $item) {
            error_log( 'MY STUFF: ' . print_r( "12121", true ) );
            try {
                $cart->addOrderItem($item);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                if ($this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getUseNotice(true)) {
                    $this->messageManager->addNoticeMessage($e->getMessage());
                } else {
                    $this->messageManager->addErrorMessage($e->getMessage());
                }
                return $resultRedirect->setPath('*/*/history');
            } catch (\Exception $e) {
                $this->messageManager->addExceptionMessage(
                    $e,
                    __('We can\'t add this item to your shopping cart right now.')
                );
                return $resultRedirect->setPath('checkout/cart');
            }
        }

        $cart->save();
        return $resultRedirect->setPath('checkout/cart');
    }
}

What I want is to check every item and if the price is 0(I have a module where I can insert a product with price 0 if in the cart is a certain product) to jump over that item when I press Order again from order history from my account. But nothing happen. I put those error_logs but without any results. Do you know what do I do wrong or what do I have to do? If I put in vendor if($item->getPrice === "0.0000") continue it's working. Thank you!

2 Answers 2

1

The issue is that you cannot create a preference for an Abstract class.

There are 2 different reorder controllers, one for logged in customers and one for guest customers:

\Magento\Sales\Controller\Guest\Reorder
\Magento\Sales\Controller\Order\Reorder

Both of these classes extend the abstract class \Magento\Sales\Controller\AbstractController\Reorder

If you want your changes to apply to both customers and guests then you will need to create 2 separate preferences for both concrete classes:

<preference for="Magento\Sales\Controller\Guest\Reorder" type="Vendor\Module\Controller\Guest\Reorder"/>
<preference for="Magento\Sales\Controller\Order\Reorder" type="Vendor\Module\Controller\Order\Reorder"/>

As an alternative, you could instead create a preference or plugin for the \Magento\Sales\Model\Reorder\Reorder class and modify the execute function as needed. That way the changes will be applied to both customers and guests without having to create multiple preferences.

1

Try this

Create the file app/code/Vendor/Module/Controller/AbstractController/CustomReorder.php and define your custom controller class CustomReorder.

 <?php
namespace Vendor\Module\Controller\AbstractController;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Sales\Controller\AbstractController\Reorder as MagentoReorder;

class CustomReorder extends MagentoReorder implements HttpPostActionInterface
{
    /**
     * Reorder action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        // Custom logic for reorder action
        // ...
    }
}

In your CustomReorder controller, it seems that you are attempting to check if the price of each item in the order is zero and skip adding it to the cart if it is. However, the condition $item->getPrice === "0.0000" will not work as intended because getPrice is a method and should be called as $item->getPrice().

To fix this issue, modify the condition to $item->getPrice() == 0.0000.

foreach ($items as $item) {
    if ($item->getPrice() == 0.0000) {
        continue; // Skip adding item with zero price to the cart
    }
    // Rest of the code
    // ...
}

By using the correct method call $item->getPrice() and comparing it to 0.0000, you can properly check if the price of each item is zero and skip adding it to the cart if necessary.

Make sure to clear the cache after making this change. Additionally, verify that the condition and logic inside the loop are correct for your specific use case.

1
  • I had parentheses, but it was my mistake because I forgot them to put in the question. Like Andrew said you cannot create a preference for an abstract class and I use those controller from "Order" and "Guest".
    – Andrew
    May 29, 2023 at 7:12

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.