0

I have activated reorder in my magento 2 website. Now I wanted to build the functionality where if the customer click on the reorder button it will add all the products in the cart except for disabled products but instead of doing that, if the order contains any disabled products then it removes the reorder button from the order even though that orders contains some of the items which are enabled.

I want that reorder button to show even if that order contains some disabled items. How can I achieve this functionality?

0

Update

Its better to write your own Module instead of overriding Magento modules. So just add a module Vendor_Module and add a helper class as given below

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Module\Helper;

/**
 * Sales module base helper
 */
class Reorder extends \Magento\Framework\App\Helper\AbstractHelper
{
    const XML_PATH_SALES_REORDER_ALLOW = 'sales/reorder/allow';

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $orderRepository;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
    ) {
        $this->orderRepository = $orderRepository;
        $this->customerSession = $customerSession;
        parent::__construct(
            $context
        );
    }

    /**
     * @return bool
     */
    public function isAllow()
    {
        return $this->isAllowed();
    }

    /**
     * Check if reorder is allowed for given store
     *
     * @param \Magento\Store\Model\Store|int|null $store
     * @return bool
     */
    public function isAllowed($store = null)
    {
        if ($this->scopeConfig->getValue(
            self::XML_PATH_SALES_REORDER_ALLOW,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        )) {
            return true;
        }
        return false;
    }

    /**
    * Check is it possible to reorder and let the reorder button be shown, 
    * though the recent order has items which are not saleable anymore (disabled or out of stock)  
    *
    * @param int $orderId
    * @return bool
    */
    public function canReorderIgnoreSaleable($orderId)
    {
        $order = $this->orderRepository->get($orderId);
        if (!$this->isAllowed($order->getStore())) {
            return false;
        }
        if ($this->customerSession->isLoggedIn()) {
            return $order->canReorderIgnoreSalable();
        } else {
            return true;
        }
    }

}

Use this new method canReorderIgnoreSaleable in the respective templates in your frontend theme. This will let the reorder button be shown, though the recent order has items which are not saleable anymore (disabled or out of stock)

vendor\magento\module-sales\view\frontend\templates\order\history.phtml vendor\magento\module-sales\view\frontend\templates\order\info\buttons.phtml vendor\magento\module-sales\view\frontend\templates\order\recent.phtml

$this->helper(\Vendor\Module\Helper\Reorder::class)->canReorderIgnoreSaleable($_order->getEntityId())

Override the Helper \Magento\Sales\Helper\Reorder and add a new method canReorderIgnoreSaleable

    /**
     * Check is it possible to reorder and let the reorder button be shown, though the recent order has items which are not saleable anymore (disabled or out of stock)  
     *
     * @param int $orderId
     * @return bool
     */
    public function canReorderIgnoreSaleable($orderId)
    {
        $order = $this->orderRepository->get($orderId);
        if (!$this->isAllowed($order->getStore())) {
            return false;
        }
        if ($this->customerSession->isLoggedIn()) {
            return $order->canReorderIgnoreSalable();
        } else {
            return true;
        }
    }

Use this new method in the respective frontend templates. Replace the method canReorder with canReorderIgnoreSaleable. This will let the reorder button be shown, though the recent order has items which are not saleable anymore (disabled or out of stock)

vendor\magento\module-sales\view\frontend\templates\order\history.phtml vendor\magento\module-sales\view\frontend\templates\order\info\buttons.phtml vendor\magento\module-sales\view\frontend\templates\order\recent.phtml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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