Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Configurable Class Reference
Inheritance diagram for Configurable:
AbstractView AbstractProduct Template AbstractBlock DataObject BlockInterface Configurable Configurable Configurable

Public Member Functions

 __construct (\Magento\Catalog\Block\Product\Context $context, \Magento\Framework\Stdlib\ArrayUtils $arrayUtils, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\ConfigurableProduct\Helper\Data $helper, \Magento\Catalog\Helper\Product $catalogProduct, CurrentCustomer $currentCustomer, PriceCurrencyInterface $priceCurrency, ConfigurableAttributeData $configurableAttributeData, array $data=[], Format $localeFormat=null, Session $customerSession=null, \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices $variationPrices=null)
 
 getCacheKeyInfo ()
 
 getAllowAttributes ()
 
 getAllowProducts ()
 
 getCurrentStore ()
 
 getJsonConfig ()
 
 showMinimalPrice ()
 
- Public Member Functions inherited from AbstractView
 __construct (\Magento\Catalog\Block\Product\Context $context, \Magento\Framework\Stdlib\ArrayUtils $arrayUtils, array $data=[])
 
 getProduct ()
 
 decorateArray ($array, $prefix='decorated_', $forceSetAll=false)
 
- Public Member Functions inherited from AbstractProduct
 __construct (\Magento\Catalog\Block\Product\Context $context, array $data=[])
 
 getAddToCartUrl ($product, $additional=[])
 
 getSubmitUrl ($product, $additional=[])
 
 getAddToWishlistParams ($product)
 
 getAddToCompareUrl ()
 
 getMinimalQty ($product)
 
 getReviewsSummaryHtml (\Magento\Catalog\Model\Product $product, $templateType=false, $displayIfNoReviews=false)
 
 getProduct ()
 
 getProductUrl ($product, $additional=[])
 
 hasProductUrl ($product)
 
 getColumnCount ()
 
 addColumnCountLayoutDepend ($pageLayout, $columnCount)
 
 removeColumnCountLayoutDepend ($pageLayout)
 
 getColumnCountLayoutDepend ($pageLayout)
 
 getPageLayout ()
 
 getCanShowProductPrice ($product)
 
 displayProductStockStatus ()
 
 getRandomString ($length, $chars=null)
 
 getProductPrice (\Magento\Catalog\Model\Product $product)
 
 isRedirectToCartEnabled ()
 
 getProductDetailsHtml (\Magento\Catalog\Model\Product $product)
 
 getDetailsRenderer ($type=null)
 
 getImage ($product, $imageId, $attributes=[])
 
- Public Member Functions inherited from Template
 __construct (Template\Context $context, array $data=[])
 
 setTemplateContext ($templateContext)
 
 getTemplate ()
 
 setTemplate ($template)
 
 getTemplateFile ($template=null)
 
 getArea ()
 
 assign ($key, $value=null)
 
 fetchView ($fileName)
 
 getBaseUrl ()
 
 getObjectData (\Magento\Framework\DataObject $object, $key)
 
 getCacheKeyInfo ()
 
- Public Member Functions inherited from AbstractBlock
 __construct (\Magento\Framework\View\Element\Context $context, array $data=[])
 
 getJsLayout ()
 
 getRequest ()
 
 getParentBlock ()
 
 setLayout (\Magento\Framework\View\LayoutInterface $layout)
 
 getLayout ()
 
 setNameInLayout ($name)
 
 getChildNames ()
 
 setAttribute ($name, $value=null)
 
 setChild ($alias, $block)
 
 addChild ($alias, $block, $data=[])
 
 unsetChild ($alias)
 
 unsetCallChild ($alias, $callback, $result, $params)
 
 unsetChildren ()
 
 getChildBlock ($alias)
 
 getChildHtml ($alias='', $useCache=true)
 
 getChildChildHtml ($alias, $childChildAlias='', $useCache=true)
 
 getBlockHtml ($name)
 
 insert ($element, $siblingName=0, $after=true, $alias='')
 
 append ($element, $alias='')
 
 getGroupChildNames ($groupName)
 
 getChildData ($alias, $key='')
 
 getUiId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getJsId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getUrl ($route='', $params=[])
 
 getViewFileUrl ($fileId, array $params=[])
 
 formatDate ( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
 
 formatTime ( $time=null, $format=\IntlDateFormatter::SHORT, $showDate=false)
 
 getModuleName ()
 
 escapeHtml ($data, $allowedTags=null)
 
 escapeJs ($string)
 
 escapeHtmlAttr ($string, $escapeSingleQuote=true)
 
 escapeCss ($string)
 
 stripTags ($data, $allowableTags=null, $allowHtmlEntities=false)
 
 escapeUrl ($string)
 
 escapeXssInUrl ($data)
 
 escapeQuote ($data, $addSlashes=false)
 
 escapeJsQuote ($data, $quote='\'')
 
 getNameInLayout ()
 
 getCacheKeyInfo ()
 
 getCacheKey ()
 
 getVar ($name, $module=null)
 
 isScopePrivate ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from BlockInterface
 toHtml ()
 

Protected Member Functions

 _getAdditionalConfig ()
 
 getOptionImages ()
 
 getOptionPrices ()
 
 _registerJsPrice ($price)
 
- Protected Member Functions inherited from AbstractProduct
 _addProductAttributesAndPrices (\Magento\Catalog\Model\ResourceModel\Product\Collection $collection)
 
 getDetailsRendererList ()
 
- Protected Member Functions inherited from Template
 _construct ()
 
 _toHtml ()
 
 getRootDirectory ()
 
 getMediaDirectory ()
 
- Protected Member Functions inherited from AbstractBlock
 _construct ()
 
 _prepareLayout ()
 
 _beforeToHtml ()
 
 _afterToHtml ($html)
 
 _toHtml ()
 
 _getNotFoundUrl ($route='', $params=['_direct'=> 'core/index/notFound'])
 
 getCacheTags ()
 
 getCacheLifetime ()
 
 _loadCache ()
 
 _saveCache ($data)
 
 _getSidPlaceholder ($cacheKey=null)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $catalogProduct = null
 
 $currentCustomer
 
 $_prices = []
 
 $jsonEncoder
 
 $helper
 
 $priceCurrency
 
 $configurableAttributeData
 
- Protected Attributes inherited from AbstractView
 $arrayUtils
 
- Protected Attributes inherited from AbstractProduct
 $_priceBlock = []
 
 $_useLinkForAsLowAs = true
 
 $_defaultColumnCount = 3
 
 $_columnCountLayoutDepend = []
 
 $_coreRegistry
 
 $_taxData
 
 $_catalogConfig
 
 $_mathRandom
 
 $_cartHelper
 
 $_wishlistHelper
 
 $_compareProduct
 
 $_imageHelper
 
 $reviewRenderer
 
 $stockRegistry
 
 $imageBuilder
 
- Protected Attributes inherited from Template
 $_viewVars = []
 
 $_baseUrl
 
 $_jsUrl
 
 $_allowSymlinks
 
 $_filesystem
 
 $_template
 
 $templateEnginePool
 
 $_storeManager
 
 $_appState
 
 $directory
 
 $templateContext
 
 $pageConfig
 
 $resolver
 
 $validator
 
- Protected Attributes inherited from AbstractBlock
 $_design
 
 $_session
 
 $_sidResolver
 
 $_nameInLayout
 
 $_layout
 
 $jsLayout = []
 
 $_request
 
 $_urlBuilder
 
 $_eventManager
 
 $_frontController
 
 $_assetRepo
 
 $_viewConfig
 
 $_cacheState
 
 $_logger
 
 $_escaper
 
 $filterManager
 
 $_localeDate
 
 $inlineTranslation
 
 $_isScopePrivate = false
 
 $_scopeConfig
 
 $_cache
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Public Member Functions inherited from AbstractBlock
static extractModuleName ($className)
 
- Data Fields inherited from Template
const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'
 
- Data Fields inherited from AbstractBlock
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER
 
const CACHE_KEY_PREFIX = 'BLOCK_'
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

@api @SuppressWarnings(PHPMD.CouplingBetweenObjects) @api

Since
100.0.2

Definition at line 23 of file Configurable.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Block\Product\Context  $context,
\Magento\Framework\Stdlib\ArrayUtils  $arrayUtils,
\Magento\Framework\Json\EncoderInterface  $jsonEncoder,
\Magento\ConfigurableProduct\Helper\Data  $helper,
\Magento\Catalog\Helper\Product  $catalogProduct,
CurrentCustomer  $currentCustomer,
PriceCurrencyInterface  $priceCurrency,
ConfigurableAttributeData  $configurableAttributeData,
array  $data = [],
Format  $localeFormat = null,
Session  $customerSession = null,
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices  $variationPrices = null 
)
Parameters
\Magento\Catalog\Block\Product\Context$context
\Magento\Framework\Stdlib\ArrayUtils$arrayUtils
\Magento\Framework\Json\EncoderInterface$jsonEncoder
\Magento\ConfigurableProduct\Helper\Data$helper
\Magento\Catalog\Helper\Product$catalogProduct
CurrentCustomer$currentCustomer
PriceCurrencyInterface$priceCurrency
ConfigurableAttributeData$configurableAttributeData
array$data
Format | null$localeFormat
Session | null$customerSession
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices | null$variationPrices@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 97 of file Configurable.php.

110  {
111  $this->priceCurrency = $priceCurrency;
112  $this->helper = $helper;
113  $this->jsonEncoder = $jsonEncoder;
114  $this->catalogProduct = $catalogProduct;
115  $this->currentCustomer = $currentCustomer;
116  $this->configurableAttributeData = $configurableAttributeData;
117  $this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(Format::class);
118  $this->customerSession = $customerSession ?: ObjectManager::getInstance()->get(Session::class);
119  $this->variationPrices = $variationPrices ?: ObjectManager::getInstance()->get(
120  \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices::class
121  );
122 
123  parent::__construct(
124  $context,
125  $arrayUtils,
126  $data
127  );
128  }

Member Function Documentation

◆ _getAdditionalConfig()

_getAdditionalConfig ( )
protected

Returns additional values for js config, con be overridden by descendants

Returns
array

Definition at line 209 of file Configurable.php.

210  {
211  return [];
212  }

◆ _registerJsPrice()

_registerJsPrice (   $price)
protected

Replace ',' on '.' for js

Deprecated:
100.1.10 Will be removed in major release
Parameters
float$price
Returns
string

Definition at line 329 of file Configurable.php.

330  {
331  return str_replace(',', '.', $price);
332  }
$price

◆ getAllowAttributes()

getAllowAttributes ( )

Get allowed attributes

Returns
array

Definition at line 149 of file Configurable.php.

150  {
151  return $this->getProduct()->getTypeInstance()->getConfigurableAttributes($this->getProduct());
152  }

◆ getAllowProducts()

getAllowProducts ( )

Get Allowed Products

Returns
\Magento\Catalog\Model\Product[]

Definition at line 178 of file Configurable.php.

179  {
180  if (!$this->hasAllowProducts()) {
181  $products = [];
182  $skipSaleableCheck = $this->catalogProduct->getSkipSaleableCheck();
183  $allProducts = $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null);
184  foreach ($allProducts as $product) {
185  if ($product->isSaleable() || $skipSaleableCheck) {
186  $products[] = $product;
187  }
188  }
189  $this->setAllowProducts($products);
190  }
191  return $this->getData('allow_products');
192  }
getData($key='', $index=null)
Definition: DataObject.php:119

◆ getCacheKeyInfo()

getCacheKeyInfo ( )

Get cache key informative items.

Returns
array
Since
100.1.10

Definition at line 136 of file Configurable.php.

137  {
138  $parentData = parent::getCacheKeyInfo();
139  $parentData[] = $this->priceCurrency->getCurrency()->getCode();
140  $parentData[] = $this->customerSession->getCustomerGroupId();
141  return $parentData;
142  }

◆ getCurrentStore()

getCurrentStore ( )

Retrieve current store

Returns
\Magento\Store\Model\Store

Definition at line 199 of file Configurable.php.

200  {
201  return $this->_storeManager->getStore();
202  }

◆ getJsonConfig()

getJsonConfig ( )

Composes configuration for js

Returns
string

Definition at line 219 of file Configurable.php.

220  {
221  $store = $this->getCurrentStore();
222  $currentProduct = $this->getProduct();
223 
224  $options = $this->helper->getOptions($currentProduct, $this->getAllowProducts());
225  $attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options);
226 
227  $config = [
228  'attributes' => $attributesData['attributes'],
229  'template' => str_replace('%s', '<%- data.price %>', $store->getCurrentCurrency()->getOutputFormat()),
230  'currencyFormat' => $store->getCurrentCurrency()->getOutputFormat(),
231  'optionPrices' => $this->getOptionPrices(),
232  'priceFormat' => $this->localeFormat->getPriceFormat(),
233  'prices' => $this->variationPrices->getFormattedPrices($this->getProduct()->getPriceInfo()),
234  'productId' => $currentProduct->getId(),
235  'chooseText' => __('Choose an Option...'),
236  'images' => $this->getOptionImages(),
237  'index' => isset($options['index']) ? $options['index'] : [],
238  ];
239 
240  if ($currentProduct->hasPreconfiguredValues() && !empty($attributesData['defaultValues'])) {
241  $config['defaultValues'] = $attributesData['defaultValues'];
242  }
243 
244  $config = array_merge($config, $this->_getAdditionalConfig());
245 
246  return $this->jsonEncoder->encode($config);
247  }
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$attributesData

◆ getOptionImages()

getOptionImages ( )
protected

Get product images for configurable variations

Returns
array
Since
100.1.10

Definition at line 255 of file Configurable.php.

256  {
257  $images = [];
258  foreach ($this->getAllowProducts() as $product) {
259  $productImages = $this->helper->getGalleryImages($product) ?: [];
260  foreach ($productImages as $image) {
261  $images[$product->getId()][] =
262  [
263  'thumb' => $image->getData('small_image_url'),
264  'img' => $image->getData('medium_image_url'),
265  'full' => $image->getData('large_image_url'),
266  'caption' => $image->getLabel(),
267  'position' => $image->getPosition(),
268  'isMain' => $image->getFile() == $product->getImage(),
269  'type' => str_replace('external-', '', $image->getMediaType()),
270  'videoUrl' => $image->getVideoUrl(),
271  ];
272  }
273  }
274 
275  return $images;
276  }

◆ getOptionPrices()

getOptionPrices ( )
protected
Returns
array

Definition at line 281 of file Configurable.php.

282  {
283  $prices = [];
284  foreach ($this->getAllowProducts() as $product) {
285  $tierPrices = [];
286  $priceInfo = $product->getPriceInfo();
287  $tierPriceModel = $priceInfo->getPrice('tier_price');
288  $tierPricesList = $tierPriceModel->getTierPriceList();
289  foreach ($tierPricesList as $tierPrice) {
290  $tierPrices[] = [
291  'qty' => $this->localeFormat->getNumber($tierPrice['price_qty']),
292  'price' => $this->localeFormat->getNumber($tierPrice['price']->getValue()),
293  'percentage' => $this->localeFormat->getNumber(
294  $tierPriceModel->getSavePercent($tierPrice['price'])
295  ),
296  ];
297  }
298 
299  $prices[$product->getId()] =
300  [
301  'oldPrice' => [
302  'amount' => $this->localeFormat->getNumber(
303  $priceInfo->getPrice('regular_price')->getAmount()->getValue()
304  ),
305  ],
306  'basePrice' => [
307  'amount' => $this->localeFormat->getNumber(
308  $priceInfo->getPrice('final_price')->getAmount()->getBaseAmount()
309  ),
310  ],
311  'finalPrice' => [
312  'amount' => $this->localeFormat->getNumber(
313  $priceInfo->getPrice('final_price')->getAmount()->getValue()
314  ),
315  ],
316  'tierPrices' => $tierPrices,
317  ];
318  }
319  return $prices;
320  }
$tierPriceModel

◆ showMinimalPrice()

showMinimalPrice ( )

Should we generate "As low as" block or not

Returns
bool
Since
100.1.10

Definition at line 340 of file Configurable.php.

341  {
342  return true;
343  }

Field Documentation

◆ $_prices

$_prices = []
protected

Definition at line 45 of file Configurable.php.

◆ $catalogProduct

$catalogProduct = null
protected

Definition at line 30 of file Configurable.php.

◆ $configurableAttributeData

$configurableAttributeData
protected

Definition at line 65 of file Configurable.php.

◆ $currentCustomer

$currentCustomer
protected

Definition at line 38 of file Configurable.php.

◆ $helper

$helper
protected

Definition at line 55 of file Configurable.php.

◆ $jsonEncoder

$jsonEncoder
protected

Definition at line 50 of file Configurable.php.

◆ $priceCurrency

$priceCurrency
protected

Definition at line 60 of file Configurable.php.


The documentation for this class was generated from the following file: