예제 #1
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function getItems()
 {
     Profiler::start(__METHOD__);
     $items = [];
     /** @var \Magento\Catalog\Model\Product $product */
     foreach ($this->collection as $product) {
         $item = ['sku' => $product->getSku(), 'name' => $product->getName(), 'url' => $product->getProductUrl(), 'description' => null, 'image' => null, 'price' => null, 'rating' => null];
         if ($this->config->isShowShortDescription()) {
             $item['description'] = html_entity_decode(strip_tags($product->getDataUsingMethod('description')));
         }
         $image = false;
         if ($product->getImage() && $product->getImage() != 'no_selection') {
             $image = $product->getImage();
         } elseif ($product->getSmallImage() && $product->getSmallImage() != 'no_selection') {
             $image = $product->getSmallImage();
         }
         if ($this->config->isShowImage() && $image) {
             $item['image'] = $this->imageHelper->init($product, false)->setImageFile($image)->resize(65 * 2, 80 * 2)->getUrl();
         }
         if ($this->config->isShowPrice()) {
             $item['price'] = $this->catalogHelper->getTaxPrice($product, $product->getFinalPrice());
             $item['price'] = $this->pricingHelper->currency($item['price'], false, false);
         }
         if ($this->config->isShowRating()) {
             $item['rating'] = $this->reviewRenderer->getReviewsSummaryHtml($product, ReviewRendererInterface::SHORT_VIEW);
         }
         $items[] = $item;
     }
     Profiler::stop(__METHOD__);
     return $items;
 }
예제 #2
0
 /**
  * Get price for selection product
  *
  * @param \Magento\Catalog\Model\Product $selection
  * @return int|float
  */
 public function getSelectionPrice($selection)
 {
     $price = 0;
     $store = $this->getProduct()->getStore();
     if ($selection) {
         $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $selection, 1);
         if (is_numeric($price)) {
             $price = $this->pricingHelper->currencyByStore($price, $store, false);
         }
     }
     return is_numeric($price) ? $price : 0;
 }
 /**
  * Basket items.
  *
  * @return mixed
  */
 public function getBasketItems()
 {
     $params = $this->getRequest()->getParams();
     if (!isset($params['quote_id']) || !isset($params['code'])) {
         $this->helper->log('Basket no quote id or code is set');
         return false;
     }
     $quoteId = $params['quote_id'];
     $quoteModel = $this->quoteFactory->create()->loadByIdWithoutStore($quoteId);
     //check for any quote for this email, don't want to render further
     if (!$quoteModel->getId()) {
         $this->helper->log('no quote found for ' . $quoteId);
         return false;
     }
     if (!$quoteModel->getIsActive()) {
         $this->helper->log('Cart is not active : ' . $quoteId);
         return false;
     }
     $this->quote = $quoteModel;
     //Start environment emulation of the specified store
     $storeId = $quoteModel->getStoreId();
     $appEmulation = $this->emulationFactory->create();
     $appEmulation->startEnvironmentEmulation($storeId);
     $quoteItems = $quoteModel->getAllItems();
     $itemsData = [];
     foreach ($quoteItems as $quoteItem) {
         //skip configurable products
         if ($quoteItem->getParentItemId() != null) {
             continue;
         }
         $_product = $quoteItem->getProduct();
         $inStock = $_product->isInStock() ? 'In Stock' : 'Out of stock';
         $total = $this->priceHelper->currency($quoteItem->getPrice());
         $productUrl = $_product->getProductUrl();
         $grandTotal = $this->priceHelper->currency($this->getGrandTotal());
         $itemsData[] = ['grandTotal' => $grandTotal, 'total' => $total, 'inStock' => $inStock, 'productUrl' => $productUrl, 'product' => $_product, 'qty' => $quoteItem->getQty()];
     }
     return $itemsData;
 }
예제 #4
0
 /**
  * Return object value by key
  *
  * @param object $object
  * @param string $key
  * @param string $dataKey
  * @param string $value
  * @param int    $storeId
  * @return float|string
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function checkForConvert($object, $key, $dataKey, $value, $storeId)
 {
     if ($key == 'product' || $key == 'category') {
         if ($key == 'product') {
             $attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $dataKey);
         } else {
             $attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Category::ENTITY, $dataKey);
         }
         if ($storeId) {
             $attribute->setStoreId($storeId);
         }
         if ($attribute->getId() > 0) {
             try {
                 $valueId = $object->getDataUsingMethod($dataKey);
                 $value = $attribute->getFrontend()->getValue($object);
             } catch (\Exception $e) {
                 //possible that some extension is removed, but we have it attribute with source in database
                 $value = '';
             }
             if ($value == 'No' && $valueId == '') {
                 $value = '';
             }
             switch ($dataKey) {
                 case 'price':
                     $value = $this->pricingHelper->currency($value, true, false);
                     break;
                 case 'special_price':
                     $value = $this->pricingHelper->currency($value, true, false);
                     break;
             }
         } else {
             switch ($dataKey) {
                 case 'final_price':
                     $value = $this->pricingHelper->currency($value, true, false);
                     break;
             }
         }
     }
     if (is_array($value)) {
         if (isset($value['label'])) {
             $value = $value['label'];
         } else {
             $value = '';
         }
     }
     return $value;
 }
예제 #5
0
 /**
  * Get bundled selections (slections-products collection)
  *
  * Returns array of options objects.
  * Each option object will contain array of selections objects
  *
  * @param ItemInterface $item
  * @return array
  */
 public function getBundleOptions(ItemInterface $item)
 {
     $options = [];
     $product = $item->getProduct();
     /** @var \Magento\Bundle\Model\Product\Type $typeInstance */
     $typeInstance = $product->getTypeInstance();
     // get bundle options
     $optionsQuoteItemOption = $item->getOptionByCode('bundle_option_ids');
     $bundleOptionsIds = $optionsQuoteItemOption ? unserialize($optionsQuoteItemOption->getValue()) : [];
     if ($bundleOptionsIds) {
         /** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */
         $optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $product);
         // get and add bundle selections collection
         $selectionsQuoteItemOption = $item->getOptionByCode('bundle_selection_ids');
         $bundleSelectionIds = unserialize($selectionsQuoteItemOption->getValue());
         if (!empty($bundleSelectionIds)) {
             $selectionsCollection = $typeInstance->getSelectionsByIds($bundleSelectionIds, $product);
             $bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true);
             foreach ($bundleOptions as $bundleOption) {
                 if ($bundleOption->getSelections()) {
                     $option = ['label' => $bundleOption->getTitle(), 'value' => []];
                     $bundleSelections = $bundleOption->getSelections();
                     foreach ($bundleSelections as $bundleSelection) {
                         $qty = $this->getSelectionQty($product, $bundleSelection->getSelectionId()) * 1;
                         if ($qty) {
                             $option['value'][] = $qty . ' x ' . $this->escaper->escapeHtml($bundleSelection->getName()) . ' ' . $this->pricingHelper->currency($this->getSelectionFinalPrice($item, $bundleSelection));
                         }
                     }
                     if ($option['value']) {
                         $options[] = $option;
                     }
                 }
             }
         }
     }
     return $options;
 }
 public function testGetOptions()
 {
     $optionIds = 'a:1:{i:0;i:1;}';
     $selectionIds = 'a:1:{i:0;s:1:"2";}';
     $selectionId = '2';
     $product = $this->getMock('Magento\\Catalog\\Model\\Product', ['getTypeInstance', '__wakeup', 'getCustomOption', 'getSelectionId', 'getName', 'getPriceModel'], [], '', false);
     $typeInstance = $this->getMock('Magento\\Bundle\\Model\\Product\\Type', ['getOptionsByIds', 'getSelectionsByIds'], [], '', false);
     $priceModel = $this->getMock('Magento\\Bundle\\Model\\Product\\Price', ['getSelectionFinalTotalPrice'], [], '', false);
     $selectionQty = $this->getMock('Magento\\Quote\\Model\\Quote\\Item\\Option', ['getValue', '__wakeup'], [], '', false);
     $bundleOption = $this->getMock('Magento\\Bundle\\Model\\Option', ['getSelections', 'getTitle', '__wakeup'], [], '', false);
     $selectionOption = $this->getMock('\\Magento\\Catalog\\Model\\Product\\Configuration\\Item\\Option\\OptionInterface', ['getValue']);
     $collection = $this->getMock('Magento\\Bundle\\Model\\ResourceModel\\Option\\Collection', ['appendSelections'], [], '', false);
     $itemOption = $this->getMock('\\Magento\\Catalog\\Model\\Product\\Configuration\\Item\\Option\\OptionInterface', ['getValue']);
     $collection2 = $this->getMock('Magento\\Bundle\\Model\\ResourceModel\\Selection\\Collection', [], [], '', false);
     $this->escaper->expects($this->once())->method('escapeHtml')->with('name')->will($this->returnValue('name'));
     $this->pricingHelper->expects($this->once())->method('currency')->with(15)->will($this->returnValue('<span class="price">$15.00</span>'));
     $priceModel->expects($this->once())->method('getSelectionFinalTotalPrice')->will($this->returnValue(15));
     $selectionQty->expects($this->any())->method('getValue')->will($this->returnValue(1));
     $bundleOption->expects($this->any())->method('getSelections')->will($this->returnValue([$product]));
     $bundleOption->expects($this->once())->method('getTitle')->will($this->returnValue('title'));
     $selectionOption->expects($this->once())->method('getValue')->will($this->returnValue($selectionIds));
     $collection->expects($this->once())->method('appendSelections')->with($collection2, true)->will($this->returnValue([$bundleOption]));
     $itemOption->expects($this->once())->method('getValue')->will($this->returnValue($optionIds));
     $typeInstance->expects($this->once())->method('getOptionsByIds')->with(unserialize($optionIds), $product)->will($this->returnValue($collection));
     $typeInstance->expects($this->once())->method('getSelectionsByIds')->with(unserialize($selectionIds), $product)->will($this->returnValue($collection2));
     $product->expects($this->once())->method('getTypeInstance')->will($this->returnValue($typeInstance));
     $product->expects($this->any())->method('getCustomOption')->with('selection_qty_' . $selectionId)->will($this->returnValue($selectionQty));
     $product->expects($this->any())->method('getSelectionId')->will($this->returnValue($selectionId));
     $product->expects($this->once())->method('getName')->will($this->returnValue('name'));
     $product->expects($this->once())->method('getPriceModel')->will($this->returnValue($priceModel));
     $this->item->expects($this->any())->method('getProduct')->will($this->returnValue($product));
     $this->item->expects($this->at(1))->method('getOptionByCode')->with('bundle_option_ids')->will($this->returnValue($itemOption));
     $this->item->expects($this->at(2))->method('getOptionByCode')->with('bundle_selection_ids')->will($this->returnValue($selectionOption));
     $this->productConfiguration->expects($this->once())->method('getCustomOptions')->with($this->item)->will($this->returnValue([0 => ['label' => 'title', 'value' => 'value']]));
     $this->assertEquals([0 => ['label' => 'title', 'value' => [0 => '1 x name <span class="price">$15.00</span>']], 1 => ['label' => 'title', 'value' => 'value']], $this->helper->getOptions($this->item));
 }
예제 #7
0
 /**
  * Returns price converted to current currency rate
  *
  * @param float $price
  * @return float
  */
 public function getCurrencyPrice($price)
 {
     $store = $this->getProduct()->getStore();
     return $this->pricingHelper->currencyByStore($price, $store, false);
 }
예제 #8
0
 /**
  * Get price configuration
  *
  * @param \Magento\Catalog\Model\Product\Option\Value|\Magento\Catalog\Model\Product\Option $option
  * @return array
  */
 protected function _getPriceConfiguration($option)
 {
     $optionPrice = $this->pricingHelper->currency($option->getPrice(true), false, false);
     $data = ['prices' => ['oldPrice' => ['amount' => $this->pricingHelper->currency($option->getRegularPrice(), false, false), 'adjustments' => []], 'basePrice' => ['amount' => $this->_catalogData->getTaxPrice($option->getProduct(), $optionPrice, false, null, null, null, null, null, false)], 'finalPrice' => ['amount' => $this->_catalogData->getTaxPrice($option->getProduct(), $optionPrice, true, null, null, null, null, null, false)]], 'type' => $option->getPriceType(), 'name' => $option->getTitle()];
     return $data;
 }
예제 #9
0
 public function testCurrency()
 {
     $price = 10.0;
     $priceHtml = '<span class="price">$10.00</span>';
     $this->assertEquals($priceHtml, $this->helper->currency($price));
 }