Magento 2 添加到愿望清单 return 到产品详细信息页面

Magento 2 Add to wish list return to the product detail page

目前在 Magento 2 中,将产品添加到愿望清单后,它会移动到愿望清单页面。我正在尝试将其移回产品详细信息页面。所以为此我尝试用 di preference

覆盖 Magento\Wishlist\Controller\Index\Add

<preference for="Magento\Wishlist\Controller\Index\Add"
            type="Eguana\CustomWishlist\Controller\Rewrite\Index\Add" />

为此我的控制器是这样的

namespace Eguana\CustomWishlist\Controller\Rewrite\Index;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Action;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Add extends \Magento\Wishlist\Controller\Index\Add
{


    public function __construct(Action\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider, ProductRepositoryInterface $productRepository, Validator $formKeyValidator)
    {
        parent::__construct($context, $customerSession, $wishlistProvider, $productRepository, $formKeyValidator);
    }

    /**
     * Adding new item
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     * @throws NotFoundException
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
     */
    public function execute()
    {
      echo 'abc';
    }
}

我的module.xml文件是这样的

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Eguana_CustomWishlist" setup_version="2.1.3">
        <sequence>
            <module name="Magento_Wishlist" />
        </sequence>
    </module>
</config>

但它仍在调用 Magento Wishlist 模块控制器。你能告诉我我的重写过程有什么问题吗?非常感谢。

在 Magento2 中 Magento\Wishlist\Controller\Index\Add 被另一个核心模块 MultipleWishlist 模块覆盖 Magento\MultipleWishlist\Controller\Index\Add 所以如果你想覆盖 wishlist 添加控制器那么你应该覆盖 MultipleWishlist 添加控制器。

我希望它对您有用并节省您的时间。

谢谢阿巴斯

在你的模块中 di.xml

   <?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">        
    <preference for="Magento\MultipleWishlist\Controller\Index\Add" type="Vendor\Module\Controller\MultipleWishlist\Add" />
    <preference for="Magento\Wishlist\Controller\Index\Add" type="Vendor\Module\Controller\Wishlist\Add" />
</config>

注意:在 MultipleWhislist 的覆盖控制器中

 namespace Vendor\Module\Controller\MultipleWishlist;

 use Magento\Catalog\Api\ProductRepositoryInterface;
 use Magento\Customer\Model\Session;
 use Magento\Framework\App\Action;
 use Magento\Framework\Controller\ResultFactory;
 use Magento\Framework\Data\Form\FormKey\Validator;
 use Magento\MultipleWishlist\Model\WishlistEditor;
 use Magento\Wishlist\Controller\WishlistProviderInterface;

 class Add extends \Vendor\Module\Controller\Wishlist\Add
{

一切正常。