コード例 #1
0
ファイル: Index.php プロジェクト: rejoiner/magento2-plugin
 public function execute()
 {
     $params = $this->getRequest()->getParams();
     /** @var \Magento\Checkout\Model\Cart $cart */
     $cart = $this->cartFactory->create();
     $successMessage = '';
     $websiteId = $this->storeManager->getStore()->getWebsiteId();
     foreach ($params as $key => $product) {
         if ($product && is_array($product)) {
             $productModel = $this->productFactory->create();
             // loadByAttribute() return false if the product was not found. There is no need to check the ID,
             // but lets stay on the safe side for the future Magento releases
             /** @var \Magento\Catalog\Model\Product $productBySKU */
             $productBySKU = $productModel->loadByAttribute('sku', $product['sku']);
             if (!$productBySKU || !($productId = $productBySKU->getId())) {
                 continue;
             }
             $stockItem = $this->stockItemApiFactory->create();
             /** @var \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItemResource */
             $stockItemResource = $this->stockItemApiResourceFactory->create();
             $stockItemResource->loadByProductId($stockItem, $productId, $websiteId);
             $qty = $stockItem->getQty();
             try {
                 if (!$cart->getQuote()->hasProductId($productId) && is_numeric($product['qty']) && $qty > $product['qty']) {
                     $cart->addProduct($productBySKU, (int) $product['qty']);
                     $successMessage .= __('%1 was added to your shopping cart.' . '</br>', $this->escaper->escapeHtml($productBySKU->getName()));
                 }
                 unset($params[$key]);
             } catch (\Exception $e) {
                 $this->rejoinerHelper->log($e->getMessage());
             }
         }
     }
     if (isset($params['coupon_code'])) {
         $cart->getQuote()->setCouponCode($params['coupon_code'])->collectTotals();
     }
     try {
         $cart->getQuote()->save();
         $cart->save();
     } catch (\Exception $e) {
         $this->rejoinerHelper->log($e->getMessage());
     }
     $this->checkoutSession->setCartWasUpdated(true);
     if ($successMessage) {
         $this->messageManager->addSuccess($successMessage);
     }
     $url = $this->_url->getUrl('checkout/cart/', ['updateCart' => true]);
     $this->getResponse()->setRedirect($url);
 }
コード例 #2
0
ファイル: Base.php プロジェクト: rejoiner/magento2-plugin
 /**
  * @return array
  */
 public function getCartItems()
 {
     if (!isset(self::$quoteItemsData)) {
         self::$quoteItemsData = [];
         $displayPriceWithTax = $this->rejoinerHelper->getTrackPriceWithTax();
         if ($quote = $this->getQuote()) {
             $categories = [];
             /** @var \Magento\Quote\Model\Quote $quote */
             /** @var \Magento\Quote\Model\Quote\Item $item */
             foreach ($quote->getAllItems() as $item) {
                 $categories = array_merge($categories, $item->getProduct()->getCategoryIds());
             }
             /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection */
             $categoryCollection = $this->categoryCollectionFactory->create();
             $categoriesArray = $categoryCollection->addAttributeToSelect('name')->addFieldToFilter('entity_id', ['in' => $categories])->load()->getItems();
             $imageWidth = $this->rejoinerHelper->getImageWidth();
             $imageHeight = $this->rejoinerHelper->getImageHeight();
             foreach ($quote->getAllVisibleItems() as $item) {
                 $product = $item->getProduct();
                 $productCategories = $this->rejoinerHelper->getProductCategories($product, $categoriesArray);
                 $imageUrl = $this->imageHelper->init($product, 'category_page_grid')->resize($imageWidth, $imageHeight)->getUrl();
                 if ($displayPriceWithTax) {
                     $productPrice = $item->getPriceInclTax();
                     $rowTotal = $item->getRowTotalInclTax();
                 } else {
                     $productPrice = $item->getPrice();
                     $rowTotal = $item->getRowTotal();
                 }
                 $newItem = ['name' => $item->getName(), 'image_url' => $imageUrl, 'price' => (string) $this->rejoinerHelper->convertPriceToCents($productPrice), 'product_id' => (string) $item->getSku(), 'item_qty' => (string) $item->getQty(), 'qty_price' => (string) $this->rejoinerHelper->convertPriceToCents($rowTotal), 'product_url' => (string) $product->getProductUrl(), 'category' => $productCategories];
                 self::$quoteItemsData[] = $newItem;
             }
         }
     }
     return self::$quoteItemsData;
 }