Skip to content

Commit

Permalink
MAGETWO-86332: Issues #10559 - Extend swatch using mixins (M2.2) #12929
Browse files Browse the repository at this point in the history
 - Merge Pull Request #12929 from srenon/magento2:2.2-develop
 - Merged commits:
   1. 1350c77
   2. 33261e0
   3. 4fca69d
   4. 97779a6
   5. 494c9c0
   6. 20b9575
   7. 089b468
   8. ca471e4
   9. c84087e
   10. ec51bd2
   11. d4fa8a5
  • Loading branch information
magento-engcom-team committed Mar 6, 2018
2 parents 0f39eae + d4fa8a5 commit 1f5c40f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 47 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/Catalog/Pricing/Render/FinalPriceBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ protected function wrapResult($html)
{
return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
'data-role="priceBox" ' .
'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
'>' . $html . '</div>';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ public function testRenderMsrpEnabled()

//assert price wrapper
$this->assertEquals(
'<div class="price-box price-final_price" data-role="priceBox" data-product-id="">test</div>',
'<div class="price-box price-final_price" data-role="priceBox" data-product-id="" ' .
'data-price-box="product-id-">test</div>',
$result
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
/**
* @var Format
*/
private $localeFormat;
protected $localeFormat;

/**
* @var Session
Expand Down Expand Up @@ -211,9 +211,6 @@ public function getJsonConfig()
$store = $this->getCurrentStore();
$currentProduct = $this->getProduct();

$regularPrice = $currentProduct->getPriceInfo()->getPrice('regular_price');
$finalPrice = $currentProduct->getPriceInfo()->getPrice('final_price');

$options = $this->helper->getOptions($currentProduct, $this->getAllowProducts());
$attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options);

Expand All @@ -223,17 +220,7 @@ public function getJsonConfig()
'currencyFormat' => $store->getCurrentCurrency()->getOutputFormat(),
'optionPrices' => $this->getOptionPrices(),
'priceFormat' => $this->localeFormat->getPriceFormat(),
'prices' => [
'oldPrice' => [
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
],
'basePrice' => [
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
],
'finalPrice' => [
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
],
],
'prices' => $this->getPrices(),
'productId' => $currentProduct->getId(),
'chooseText' => __('Choose an Option...'),
'images' => $this->getOptionImages(),
Expand All @@ -248,6 +235,32 @@ public function getJsonConfig()

return $this->jsonEncoder->encode($config);
}

/**
* Get product prices for configurable variations
*
* @return array
* @since 100.2.0
*/
protected function getPrices()
{
$currentProduct = $this->getProduct();

$regularPrice = $currentProduct->getPriceInfo()->getPrice('regular_price');
$finalPrice = $currentProduct->getPriceInfo()->getPrice('final_price');

return [
'oldPrice' => [
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
],
'basePrice' => [
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
],
'finalPrice' => [
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
],
];
}

/**
* Get product images for configurable variations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public function getJsonConfig()
$this->unsetData('allow_products');
return parent::getJsonConfig();
}

/**
* Composes configuration for js price format
*
* @return string
*/
public function getPriceFormatJson()
{
return $this->jsonEncoder->encode($this->localeFormat->getPriceFormat());
}

/**
* Composes configuration for js price
*
* @return string
*/
public function getPricesJson()
{
return $this->jsonEncoder->encode($this->getPrices());
}

/**
* Do not load images for Configurable product with swatches due to its loaded by request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,38 @@
* See COPYING.txt for license details.
*/
?>
<?php /** @var $block \Magento\Swatches\Block\Product\Renderer\Listing\Configurable */ ?>
<div class="swatch-opt-<?= /* @escapeNotVerified */ $block->getProduct()->getId() ?>"></div>
<script>
require([
'jquery',
'jquery/ui',
'Magento_Swatches/js/swatch-renderer',
'Magento_Swatches/js/catalog-add-to-cart',
'priceBox'
], function ($) {
var jsonConfig = <?= /* @escapeNotVerified */ $block->getJsonConfig() ?>;

$('.swatch-opt-<?= /* @escapeNotVerified */ $block->getProduct()->getId() ?>').SwatchRenderer({
selectorProduct: '.product-item-details',
onlySwatches: true,
enableControlLabel: false,
numberToShow: <?= /* @escapeNotVerified */ $block->getNumberSwatchesPerProduct() ?>,
jsonConfig: jsonConfig,
jsonSwatchConfig: <?= /* @escapeNotVerified */ $block->getJsonSwatchConfig() ?>,
mediaCallback: '<?= /* @escapeNotVerified */ $block->getMediaCallback() ?>'
});
<?php
/** @var $block \Magento\Swatches\Block\Product\Renderer\Listing\Configurable */
$productId = $block->getProduct()->getId();
?>
<div class="swatch-opt-<?= /* @escapeNotVerified */ $productId ?>"
data-role="swatch-option-<?= /* @escapeNotVerified */ $productId ?>"></div>

var dataPriceBoxSelector = '[data-role=priceBox]',
dataProductIdSelector = '[data-product-id=<?= $block->escapeHtml($block->getProduct()->getId()) ?>]',
priceBoxes = $(dataPriceBoxSelector + dataProductIdSelector);
<script type="text/x-magento-init">
{
"[data-role=swatch-option-<?= /* @escapeNotVerified */ $productId ?>]": {
"Magento_Swatches/js/swatch-renderer": {
"selectorProduct": ".product-item-details",
"onlySwatches": true,
"enableControlLabel": false,
"numberToShow": <?= /* @escapeNotVerified */ $block->getNumberSwatchesPerProduct(); ?>,
"jsonConfig": <?= /* @escapeNotVerified */ $block->getJsonConfig(); ?>,
"jsonSwatchConfig": <?= /* @escapeNotVerified */ $block->getJsonSwatchConfig(); ?>,
"mediaCallback": "<?= /* @escapeNotVerified */ $block->getMediaCallback() ?>"
}
}
}
</script>

priceBoxes.priceBox({
'priceConfig': {
priceFormat: jsonConfig.priceFormat,
prices: jsonConfig.prices
<script type="text/x-magento-init">
{
"[data-role=priceBox][data-price-box=product-id-<?= /* @escapeNotVerified */ $productId ?>]": {
"priceBox": {
"priceConfig": {
"priceFormat": <?= /* @escapeNotVerified */ $block->getPriceFormatJson(); ?>,
"prices": <?= /* @escapeNotVerified */ $block->getPricesJson(); ?>
}
}
});
});
}
}
</script>

0 comments on commit 1f5c40f

Please sign in to comment.