Magento 如何在不重定向到愿望列表页面的情况下在愿望列表中添加产品

Magento 如何在不重定向到愿望列表页面的情况下在愿望列表中添加产品,magento,magento2,magento-2.3,Magento,Magento2,Magento 2.3,如果我们点击“添加到愿望列表”按钮,它将显示“愿望列表”页面 我不想显示愿望列表页面。相反,我想站在同一页上。但是这个项目应该 “添加到愿望列表” 请帮助我找到解决方案。您应该自定义Magento添加到愿望列表的过程。 Magento使用dataPost.js发布愿望列表 您可以更改为使用ajax来实现它。您可以覆盖Wishlist模块的添加控制器,并将重定向URL更改为当前URL: 创建一个模块,例如Wishlist/example etc/di.xml <?xml version=&

如果我们点击“添加到愿望列表”按钮,它将显示“愿望列表”页面

我不想显示愿望列表页面。相反,我想站在同一页上。但是这个项目应该

“添加到愿望列表”


请帮助我找到解决方案。

您应该自定义Magento添加到愿望列表的过程。 Magento使用
dataPost.js
发布愿望列表


您可以更改为使用ajax来实现它。

您可以覆盖Wishlist模块的添加控制器,并将重定向URL更改为当前URL:

创建一个模块,例如Wishlist/example

etc/di.xml

 <?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="Magento\Wishlist\Controller\Index\Add" type="Wishlist\Example\Controller\Index\Add">
    </preference>
 </config>

<?php
 
namespace Wishlist\Example\Controller\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
{
    /**
     * Adding new item
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     * @throws NotFoundException
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
     */
    public function execute()
    {
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        if (!$this->formKeyValidator->validate($this->getRequest())) {
            return $resultRedirect->setPath('*/');
        }
 
        $wishlist = $this->wishlistProvider->getWishlist();
        if (!$wishlist) {
            throw new NotFoundException(__('Page not found.'));
        }
 
        $session = $this->_customerSession;
 
        $requestParams = $this->getRequest()->getParams();
 
        if ($session->getBeforeWishlistRequest()) {
            $requestParams = $session->getBeforeWishlistRequest();
            $session->unsBeforeWishlistRequest();
        }
 
        $productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
        if (!$productId) {
            $resultRedirect->setPath('*/');
            return $resultRedirect;
        }
 
        try {
            $product = $this->productRepository->getById($productId);
        } catch (NoSuchEntityException $e) {
            $product = null;
        }
 
        if (!$product || !$product->isVisibleInCatalog()) {
            $this->messageManager->addErrorMessage(__('We can\'t specify a product.'));
            $resultRedirect->setPath('*/');
            return $resultRedirect;
        }
 
        try {
            $buyRequest = new \Magento\Framework\DataObject($requestParams);
 
            $result = $wishlist->addNewItem($product, $buyRequest);
            if (is_string($result)) {
                throw new \Magento\Framework\Exception\LocalizedException(__($result));
            }
            if ($wishlist->isObjectNew()) {
                $wishlist->save();
            }
            $this->_eventManager->dispatch(
                'wishlist_add_product',
                ['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
            );
 
            $referer = $session->getBeforeWishlistUrl();
            if ($referer) {
                $session->setBeforeWishlistUrl(null);
            } else {
                $referer = $this->_redirect->getRefererUrl();
            }
 
            $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
 
            $this->messageManager->addComplexSuccessMessage(
                'addProductSuccessMessage',
                [
                    'product_name' => $product->getName(),
                    'referer' => $referer
                ]
            );
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addErrorMessage(
                __('We can\'t add the item to Wish List right now: %1.', $e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addExceptionMessage(
                $e,
                __('We can\'t add the item to Wish List right now.')
            );
        }
 
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }
}
$resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());