当前位置: 首页>>代码示例>>PHP>>正文


PHP Product::addParamsToBuyRequest方法代码示例

本文整理汇总了PHP中Magento\Catalog\Helper\Product::addParamsToBuyRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::addParamsToBuyRequest方法的具体用法?PHP Product::addParamsToBuyRequest怎么用?PHP Product::addParamsToBuyRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Magento\Catalog\Helper\Product的用法示例。


在下文中一共展示了Product::addParamsToBuyRequest方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: execute

 /**
  * Add wishlist item to shopping cart and remove from wishlist
  *
  * If Product has required options - item removed from wishlist and redirect
  * to product view page with message about needed defined required options
  *
  * @return ResponseInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $itemId = (int) $this->getRequest()->getParam('item');
     /* @var $item \Magento\Wishlist\Model\Item */
     $item = $this->itemFactory->create()->load($itemId);
     if (!$item->getId()) {
         return $this->_redirect('*/*');
     }
     $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
     if (!$wishlist) {
         return $this->_redirect('*/*');
     }
     // Set qty
     $qty = $this->getRequest()->getParam('qty');
     if (is_array($qty)) {
         if (isset($qty[$itemId])) {
             $qty = $qty[$itemId];
         } else {
             $qty = 1;
         }
     }
     $qty = $this->quantityProcessor->process($qty);
     if ($qty) {
         $item->setQty($qty);
     }
     $redirectUrl = $this->_url->getUrl('*/*');
     $configureUrl = $this->_url->getUrl('*/*/configure/', ['id' => $item->getId(), 'product_id' => $item->getProductId()]);
     try {
         /** @var \Magento\Wishlist\Model\Resource\Item\Option\Collection $options */
         $options = $this->optionFactory->create()->getCollection()->addItemFilter([$itemId]);
         $item->setOptions($options->getOptionsByItem($itemId));
         $buyRequest = $this->productHelper->addParamsToBuyRequest($this->getRequest()->getParams(), ['current_config' => $item->getBuyRequest()]);
         $item->mergeBuyRequest($buyRequest);
         $item->addToCart($this->cart, true);
         $this->cart->save()->getQuote()->collectTotals();
         $wishlist->save();
         if (!$this->cart->getQuote()->getHasError()) {
             $message = __('You added %1 to your shopping cart.', $this->escaper->escapeHtml($item->getProduct()->getName()));
             $this->messageManager->addSuccess($message);
         }
         if ($this->cart->getShouldRedirectToCart()) {
             $redirectUrl = $this->cart->getCartUrl();
         } else {
             $refererUrl = $this->_redirect->getRefererUrl();
             if ($refererUrl && $refererUrl != $configureUrl) {
                 $redirectUrl = $refererUrl;
             }
         }
     } catch (ProductException $e) {
         $this->messageManager->addError(__('This product(s) is out of stock.'));
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->messageManager->addNotice($e->getMessage());
         $redirectUrl = $configureUrl;
     } catch (\Exception $e) {
         $this->messageManager->addException($e, __('Cannot add item to shopping cart'));
     }
     $this->helper->calculate();
     return $this->getResponse()->setRedirect($redirectUrl);
 }
开发者ID:shabbirvividads,项目名称:magento2,代码行数:69,代码来源:Cart.php

示例2: updateItem

 /**
  * Update wishlist Item and set data from request
  *
  * The $params sets how current item configuration must be taken into account and additional options.
  * It's passed to \Magento\Catalog\Helper\Product->addParamsToBuyRequest() to compose resulting buyRequest.
  *
  * Basically it can hold
  * - 'current_config', \Magento\Framework\Object or array - current buyRequest that configures product in this item,
  *   used to restore currently attached files
  * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file options (file inputs),
  * so they won't intersect with other submitted options
  *
  * For more options see \Magento\Catalog\Helper\Product->addParamsToBuyRequest()
  *
  * @param int|Item $itemId
  * @param \Magento\Framework\Object $buyRequest
  * @param null|array|\Magento\Framework\Object $params
  * @return $this
  * @throws Exception
  *
  * @see \Magento\Catalog\Helper\Product::addParamsToBuyRequest()
  */
 public function updateItem($itemId, $buyRequest, $params = null)
 {
     $item = null;
     if ($itemId instanceof Item) {
         $item = $itemId;
     } else {
         $item = $this->getItem((int) $itemId);
     }
     if (!$item) {
         throw new Exception(__('We can\'t specify a wish list item.'));
     }
     $product = $item->getProduct();
     $productId = $product->getId();
     if ($productId) {
         if (!$params) {
             $params = new \Magento\Framework\Object();
         } else {
             if (is_array($params)) {
                 $params = new \Magento\Framework\Object($params);
             }
         }
         $params->setCurrentConfig($item->getBuyRequest());
         $buyRequest = $this->_catalogProduct->addParamsToBuyRequest($buyRequest, $params);
         $product->setWishlistStoreId($item->getStoreId());
         $items = $this->getItemCollection();
         $isForceSetQuantity = true;
         foreach ($items as $_item) {
             /* @var $_item Item */
             if ($_item->getProductId() == $product->getId() && $_item->representProduct($product) && $_item->getId() != $item->getId()) {
                 // We do not add new wishlist item, but updating the existing one
                 $isForceSetQuantity = false;
             }
         }
         $resultItem = $this->addNewItem($product, $buyRequest, $isForceSetQuantity);
         /**
          * Error message
          */
         if (is_string($resultItem)) {
             throw new Exception(__($resultItem));
         }
         if ($resultItem->getId() != $itemId) {
             if ($resultItem->getDescription() != $item->getDescription()) {
                 $resultItem->setDescription($item->getDescription())->save();
             }
             $item->isDeleted(true);
             $this->setDataChanges(true);
         } else {
             $resultItem->setQty($buyRequest->getQty() * 1);
             $resultItem->setOrigData('qty', 0);
         }
     } else {
         throw new Exception(__('The product does not exist.'));
     }
     return $this;
 }
开发者ID:pavelnovitsky,项目名称:magento2,代码行数:77,代码来源:Wishlist.php

示例3: updateItem

 /**
  * Updates quote item with new configuration
  *
  * $params sets how current item configuration must be taken into account and additional options.
  * It's passed to \Magento\Catalog\Helper\Product->addParamsToBuyRequest() to compose resulting buyRequest.
  *
  * Basically it can hold
  * - 'current_config', \Magento\Framework\Object or array - current buyRequest that configures product in this item,
  *   used to restore currently attached files
  * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file options (file inputs),
  *   so they won't intersect with other submitted options
  *
  * For more options see \Magento\Catalog\Helper\Product->addParamsToBuyRequest()
  *
  * @param int $itemId
  * @param \Magento\Framework\Object $buyRequest
  * @param null|array|\Magento\Framework\Object $params
  * @return \Magento\Quote\Model\Quote\Item
  * @throws \Magento\Framework\Exception\LocalizedException
  *
  * @see \Magento\Catalog\Helper\Product::addParamsToBuyRequest()
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function updateItem($itemId, $buyRequest, $params = null)
 {
     $item = $this->getItemById($itemId);
     if (!$item) {
         throw new \Magento\Framework\Exception\LocalizedException(__('This is the wrong quote item id to update configuration.'));
     }
     $productId = $item->getProduct()->getId();
     //We need to create new clear product instance with same $productId
     //to set new option values from $buyRequest
     $product = clone $this->productRepository->getById($productId, false, $this->getStore()->getId());
     if (!$params) {
         $params = new \Magento\Framework\Object();
     } elseif (is_array($params)) {
         $params = new \Magento\Framework\Object($params);
     }
     $params->setCurrentConfig($item->getBuyRequest());
     $buyRequest = $this->_catalogProduct->addParamsToBuyRequest($buyRequest, $params);
     $buyRequest->setResetCount(true);
     $resultItem = $this->addProduct($product, $buyRequest);
     if (is_string($resultItem)) {
         throw new \Magento\Framework\Exception\LocalizedException(__($resultItem));
     }
     if ($resultItem->getParentItem()) {
         $resultItem = $resultItem->getParentItem();
     }
     if ($resultItem->getId() != $itemId) {
         /**
          * Product configuration didn't stick to original quote item
          * It either has same configuration as some other quote item's product or completely new configuration
          */
         $this->removeItem($itemId);
         $items = $this->getAllItems();
         foreach ($items as $item) {
             if ($item->getProductId() == $productId && $item->getId() != $resultItem->getId()) {
                 if ($resultItem->compare($item)) {
                     // Product configuration is same as in other quote item
                     $resultItem->setQty($resultItem->getQty() + $item->getQty());
                     $this->removeItem($item->getId());
                     break;
                 }
             }
         }
     } else {
         $resultItem->setQty($buyRequest->getQty());
     }
     return $resultItem;
 }
开发者ID:niranjanssiet,项目名称:magento2,代码行数:70,代码来源:Quote.php


注:本文中的Magento\Catalog\Helper\Product::addParamsToBuyRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。