Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在组件前端控制器中创建购物车后,如何在前端刷新购物车?_Php_Magento_Magento2 - Fatal编程技术网

Php 在组件前端控制器中创建购物车后,如何在前端刷新购物车?

Php 在组件前端控制器中创建购物车后,如何在前端刷新购物车?,php,magento,magento2,Php,Magento,Magento2,我想在我的组件中创建购物车恢复功能,但我一直在前端重新加载购物车 我正在使用Luma主题和示例产品开发标准Magento CE 2.4.2 我有一个前端恢复控制器: 命名空间Mycomponent\Myname\Controller\Events; 使用\Psr\Log\LoggerInterface; 使用\Magento\Framework\App\Action\Action; 使用\Magento\Framework\App\Action\Context; 使用\Magento\Fram

我想在我的组件中创建购物车恢复功能,但我一直在前端重新加载购物车

我正在使用Luma主题和示例产品开发标准Magento CE 2.4.2

  • 我有一个前端恢复控制器:
  • 
    命名空间Mycomponent\Myname\Controller\Events;
    使用\Psr\Log\LoggerInterface;
    使用\Magento\Framework\App\Action\Action;
    使用\Magento\Framework\App\Action\Context;
    使用\Magento\Framework\View\Result\PageFactory;
    使用\Magento\Catalog\Model\Product;
    使用\Magento\Catalog\Api\ProductRepositoryInterface;
    使用\Magento\Quote\Api\CartRepositoryInterface;
    使用\Magento\Store\Model\Store;
    使用\Magento\Checkout\Model\Session;
    使用\Magento\Catalog\Model\ProductFactory;
    使用\Magento\Quote\Api\CartManagementInterface;
    使用\Magento\Customer\Model\Session作为CustomerSession;
    使用\Magento\Framework\Exception\NoSuchEntityException;
    使用\Mycomponent\Myname\Model\CartRecovery作为CartRecoveryModel;
    使用\Mycomponent\Myname\Helper\CookieManager;
    类恢复扩展了操作
    {
    常数
    页面\购物车\ URL='/checkout/';
    /**
    *@var PageFactory
    */
    受保护的$resultPageFactory;
    /**
    *@var CartRepositoryInterface
    */
    受保护的$cartRepositoryInterface;
    /**
    *@var存储
    */
    受保护的商店;
    /**
    *@var会话
    */
    受保护的会议;
    /**
    *@var产品工厂
    */
    受保护的工厂;
    /**
    *@var-CartManagementInterface
    */
    受保护的$quoteManagement;
    /**
    *@var CustomerSession
    */
    受保护的$customerSession;
    /**
    *@var cartymodel
    */
    受保护的$cartymodel;
    /**
    *@var ProductRepositoryInterface
    */
    受保护的$productRepository;
    /**
    *@var LoggerInterface
    */
    受保护的数据记录器;
    /**
    *@var-CookieManager
    */
    受保护的$cookieManagerHelper;
    受保护的$checkoutSession;
    /**
    *购物车构造函数。
    *@param Context$Context
    *@param PageFactory$resultPageFactory
    *@param ProductRepositoryInterface$productRepository
    *@param CartRepositoryInterface$CartRepositoryInterface
    *@param Store$Store
    *@param Session$Session
    *@param ProductFactory$ProductFactory
    *@param CartManagementInterface$quoteManagement
    *@param CustomerSession$CustomerSession
    */
    公共函数构造(
    上下文$Context,
    PageFactory$resultPageFactory,
    ProductRepositoryInterface$productRepository,
    CartRepositoryInterface$CartRepositoryInterface,
    商店$Store,
    第次会议$第次会议,
    ProductFactory$ProductFactory,
    CartManagementInterface$quoteManagement,
    CustomerSession$CustomerSession,
    CartRecoveryModel$CartRecoveryModel,
    LoggerInterface$logger,
    CookieManager$cookieManagerHelper,
    \Magento\Checkout\Model\Session$checkoutSession
    ) {
    父项::_构造($context);
    $this->resultPageFactory=$resultPageFactory;
    $this->cartRepositoryInterface=$cartRepositoryInterface;
    $this->store=$store;
    $this->session=$session;
    $this->productFactory=$productFactory;
    $this->quoteManagement=$quoteManagement;
    $this->customerSession=$customerSession;
    $this->cartRecoveryModel=$cartRecoveryModel;
    $this->logger=$logger;
    $this->productRepository=$productRepository;
    $this->cookieManagerHelper=$cookieManagerHelper;
    $this->checkoutSession=$checkoutSession;
    }
    /**
    *@return-ResponseInterface |\Magento\Framework\Controller\ResultInterface | void
    *@throws\Magento\Framework\Exception\LocalizedException
    */
    公共函数execute()
    {
    试一试{
    $query=$this->getRequest()->getParam('key');
    $cartData=$this->cartRecoveryModel->recoverCartDataFromUrl($query);
    if(空($cartData['sku'])){
    返回false;
    }
    如果($this->customerSession->isLoggedIn()){
    $customer=$this->customerSession->getCustomer();
    $quoteId=$this->quoteManagement->createEmptyCartForCustomer($customer->getId());
    $quote=$this->cartRepositoryInterface->get($quoteId);
    //foreach将被替换
    foreach($cartData['sku']作为$key=>$sku){
    $product=$this->productRepository->get($sku);
    对于($i=0;$i<$cartData['qty'][$key];$i++){
    $quote->addProduct($product);
    $quote->collectTotals()->save();
    }
    }
    }否则{
    $quote=$this->session->getQuote();
    $quote->setStore($this->store);
    $quote->setCurrency();
    //foreach将被替换
    foreach($cartData['sku']作为$key=>$sku){
    $product=$this->productRepository->get($sku);
    对于($i=0;$i<$cartData['qty'][$key];$i++){
    $quote->addProduct($product);
    $quote->collectTotals()->save();
    }
    }
    }
    $this->cartRepositoryInterface->save($quote);
    $this->checkoutSession->replaceQuote($quote)->unsLastRealOrderId();
    如果(!empty($this->getRequest()->getParam('client')){
    $this->cookieManagerHelper->setCookie(
    CookieManager::客户端\u COOKIE,
    $this->getRequest()->getParam('客户端')
    );
    }
    $this->getResponse()->setRedirect(self::PAGE\u CART\u URL);
    }捕获(\异常$e){
    $this->logger->critical($e->getLogMessage());
    }
    }
    }
    
  • 下一个是Mycomponent/Myname/etc/frontend/sections.xml
  • 
    
  • 下一个是Mycomponent/Myname/etc/frontend/routes.xml
  • 
    
    
    namespace Mycomponent\Myname\Controller\Events;
    
    use \Psr\Log\LoggerInterface;
    
    use \Magento\Framework\App\Action\Action;
    use \Magento\Framework\App\Action\Context;
    use \Magento\Framework\View\Result\PageFactory;
    use \Magento\Catalog\Model\Product;
    
    use \Magento\Catalog\Api\ProductRepositoryInterface;
    
    use \Magento\Quote\Api\CartRepositoryInterface;
    use \Magento\Store\Model\Store;
    use \Magento\Checkout\Model\Session;
    use \Magento\Catalog\Model\ProductFactory;
    use \Magento\Quote\Api\CartManagementInterface;
    use \Magento\Customer\Model\Session as CustomerSession;
    use \Magento\Framework\Exception\NoSuchEntityException;
    
    use \Mycomponent\Myname\Model\CartRecovery as CartRecoveryModel;
    use \Mycomponent\Myname\Helper\CookieManager;
    
    class CartRecovery extends Action
    {
    
      const
          PAGE_CART_URL = '/checkout/';
    
      /**
       * @var PageFactory
       */
      protected $resultPageFactory;
    
      /**
       * @var CartRepositoryInterface
       */
      protected $cartRepositoryInterface;
    
      /**
       * @var Store
       */
      protected $store;
    
      /**
       * @var Session
       */
      protected $session;
    
      /**
       * @var ProductFactory
       */
      protected $productFactory;
    
      /**
       * @var CartManagementInterface
       */
      protected $quoteManagement;
    
      /**
       * @var CustomerSession
       */
      protected $customerSession;
    
      /**
       * @var CartRecoveryModel
       */
      protected $cartRecoveryModel;
    
      /**
       * @var ProductRepositoryInterface
       */
      protected $productRepository;
    
      /**
       * @var LoggerInterface
       */
      protected $logger;
    
      /**
       * @var CookieManager
       */
      protected $cookieManagerHelper;
    
      protected $checkoutSession;
    
      /**
       * Cart constructor.
       * @param Context $context
       * @param PageFactory $resultPageFactory
       * @param ProductRepositoryInterface $productRepository
       * @param CartRepositoryInterface $cartRepositoryInterface
       * @param Store $store
       * @param Session $session
       * @param ProductFactory $productFactory
       * @param CartManagementInterface $quoteManagement
       * @param CustomerSession $customerSession
       */
      public function __construct(
          Context $context,
          PageFactory $resultPageFactory,
          ProductRepositoryInterface $productRepository,
          CartRepositoryInterface $cartRepositoryInterface,
          Store $store,
          Session $session,
          ProductFactory $productFactory,
          CartManagementInterface $quoteManagement,
          CustomerSession $customerSession,
          CartRecoveryModel $cartRecoveryModel,
          LoggerInterface $logger,
          CookieManager $cookieManagerHelper,
          \Magento\Checkout\Model\Session $checkoutSession
      ) {
          parent::__construct($context);
          $this->resultPageFactory       = $resultPageFactory;
          $this->cartRepositoryInterface = $cartRepositoryInterface;
          $this->store                   = $store;
          $this->session                 = $session;
          $this->productFactory          = $productFactory;
          $this->quoteManagement         = $quoteManagement;
          $this->customerSession         = $customerSession;
          $this->cartRecoveryModel       = $cartRecoveryModel;
          $this->logger                  = $logger;
          $this->productRepository       = $productRepository;
          $this->cookieManagerHelper     = $cookieManagerHelper;
          $this->checkoutSession         = $checkoutSession;
      }
    
      /**
       * @return ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
       * @throws \Magento\Framework\Exception\LocalizedException
       */
      public function execute()
      {
          try {
              $query = $this->getRequest()->getParam('key');
    
              $cartData = $this->cartRecoveryModel->recoverCartDataFromUrl($query);
    
              if (empty($cartData['sku'])) {
                  return false;
              }
    
              if ($this->customerSession->isLoggedIn()) {
                  $customer = $this->customerSession->getCustomer();
                  $quoteId = $this->quoteManagement->createEmptyCartForCustomer($customer->getId());
                  $quote = $this->cartRepositoryInterface->get($quoteId);
                  //foreach will be replaced
                  foreach ($cartData['sku'] as $key => $sku) {
                      $product = $this->productRepository->get($sku);
    
                      for ($i = 0; $i < $cartData['qty'][$key]; $i++) {
                          $quote->addProduct($product);
                          $quote->collectTotals()->save();
                      }
                  }
    
              } else {
                  $quote = $this->session->getQuote();
                  $quote->setStore($this->store);
                  $quote->setCurrency();
                  //foreach will be replaced
                  foreach ($cartData['sku'] as $key => $sku) {
                      $product = $this->productRepository->get($sku);
    
                      for ($i = 0; $i < $cartData['qty'][$key]; $i++) {
                          $quote->addProduct($product);
                          $quote->collectTotals()->save();
                      }
                  }
              }
    
              $this->cartRepositoryInterface->save($quote);
              $this->checkoutSession->replaceQuote($quote)->unsLastRealOrderId();
    
              if (!empty($this->getRequest()->getParam('client'))) {
                  $this->cookieManagerHelper->setCookie(
                      CookieManager::CLIENT_COOKIE,
                      $this->getRequest()->getParam('client')
                  );
              }
    
              $this->getResponse()->setRedirect(self::PAGE_CART_URL);
          } catch (\Exception $e) {
              $this->logger->critical($e->getLogMessage());
          }
      }
    }
    
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
        <action name="myname">
            <section name="cart"/>
        </action>
    </config>
    
    <?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
        <router id="standard">
            <route frontName="myname" id="myname">
                <module name="Mycomponent_Myname"/>
            </route>
        </router>
    </config>
    
    //for the first time:
    <action name="myname/events/cartrecovery">
    
    //for the second time
    <action name="myname">