سؤال

I have been trying to use plugin to override Magento\Sales\Helper\Reorder.php. I have added a comment inside the code what I would like to change.

/**
     * Check is it possible to reorder
     *
     * @param int $orderId
     * @return bool
     */
    public function canReorder($orderId)
    {
        $order = $this->orderRepository->get($orderId);
        if (!$this->isAllowed($order->getStore())) {
            return false;
        }

        $currentOrder = $this->registry->registry('current_order');
        if ($this->customerSession->isLoggedIn() || isset($currentOrder)) {
            \\WHAT I AM TRYING TO DO - canReorder() change to canReorderIgnoreSalable()
            return $order->canReorderIgnoreSalable();
        } else {
            return false;
        }
    }

How can I do this through plugin?

هل كانت مفيدة؟

المحلول

You need to write this below code in your module di.xml file

<config>
    <type name="Magento\Sales\Helper\Reorder">
        <plugin name="modulename_sales_reorder" type="YourVendorName\ModuleName\Plugin\Helper\ReorderPlugin" />
    </type>
</config>

Then you have to create a file in the path app\code\YourVendorName\ModuleName\Plugin\Helper\ReorderPlugin.php. Need to write there

<?php
namespace YourVendorName\ModuleName\Plugin\Helper;
 
class ReorderPlugin
{

     protected $customerSession;
 
     protected $orderRepository;

     public $registry;
 
     public function __construct(
         \Magento\Customer\Model\Session $customerSession,
         \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
         \Magento\Framework\Registry $registry
     ) {
         $this->orderRepository = $orderRepository;
         $this->customerSession = $customerSession;
         $this->registry = $registry;
     }

    /**
     * Check is it possible to reorder
     *
     * @param int $orderId
     * @return bool
     */
    public function aroundCanReorder(\Magento\Sales\Helper\Reorder $subject, \callable $proceed, $orderId)
    {
        $order = $this->orderRepository->get($orderId);
        if (!$subject->isAllowed($order->getStore())) {
            return false;
        }

        $currentOrder = $this->registry->registry('current_order');
        if ($this->customerSession->isLoggedIn() || isset($currentOrder)) {
            \\WHAT I AM TRYING TO DO - canReorder() change to canReorderIgnoreSalable()
            return $order->canReorderIgnoreSalable();
        } else {
            return false;
        }
    }
}

After that please run below and try

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento cache:flush

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top