Skip to content

Commit

Permalink
MAGETWO-70606: [Backport] - [GITHUB] Simple product videos display th…
Browse files Browse the repository at this point in the history
…e thumbnail image rather than the embedded video player. #6360 - for 2.1
  • Loading branch information
VladimirZaets committed Aug 29, 2017
1 parent ebb08a0 commit cd6c087
Show file tree
Hide file tree
Showing 14 changed files with 902 additions and 145 deletions.
4 changes: 4 additions & 0 deletions app/code/Magento/Catalog/Block/Product/View/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public function getGalleryImagesJson()
'caption' => $image->getLabel(),
'position' => $image->getPosition(),
'isMain' => $this->isMainImage($image),
'type' => str_replace('external-', '', $image->getMediaType()),
'videoUrl' => $image->getVideoUrl(),
];
}
if (empty($imagesItems)) {
Expand All @@ -127,6 +129,8 @@ public function getGalleryImagesJson()
'caption' => '',
'position' => '0',
'isMain' => true,
'type' => 'image',
'videoUrl' => null,
];
}
return json_encode($imagesItems);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Block\Plugin\Product\Media;

use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Catalog\Model\Product;
use Magento\Framework\Serialize\Serializer\Json;

/**
* Provides a serialized media gallery data for configurable product options.
*/
class Gallery
{
/**
* @var Json
*/
private $json;

/**
* @param Json $json
*/
public function __construct(
Json $json
) {
$this->json = $json;
}

/**
* @param \Magento\Catalog\Block\Product\View\Gallery $subject
* @param string $result
* @return string
*/
public function afterGetOptionsMediaGalleryDataJson(
\Magento\Catalog\Block\Product\View\Gallery $subject,
$result
) {
$result = $this->json->unserialize($result);
$parentProduct = $subject->getProduct();
if ($parentProduct->getTypeId() == Configurable::TYPE_CODE) {
/** @var Configurable $productType */
$productType = $parentProduct->getTypeInstance();
$products = $productType->getUsedProducts($parentProduct);
/** @var Product $product */
foreach ($products as $product) {
$key = $product->getId();
$result[$key] = $this->getProductGallery($product);
}
}
return $this->json->serialize($result);
}

/**
* @param Product $product
* @return array
*/
private function getProductGallery($product)
{
$result = [];
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
$result[] = [
'mediaType' => $image->getMediaType(),
'videoUrl' => $image->getVideoUrl(),
'isBase' => $product->getImage() == $image->getFile(),
];
}
return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

use Magento\ConfigurableProduct\Model\ConfigurableAttributeData;
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Locale\Format;
use Magento\Framework\Pricing\PriceCurrencyInterface;

/**
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
Expand Down Expand Up @@ -57,6 +60,11 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
*/
protected $configurableAttributeData;

/**
* @var Format
*/
private $localeFormat;

/**
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
Expand All @@ -67,6 +75,8 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
* @param PriceCurrencyInterface $priceCurrency
* @param ConfigurableAttributeData $configurableAttributeData
* @param array $data
* @param Format|null $localeFormat
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
Expand All @@ -77,21 +87,36 @@ public function __construct(
CurrentCustomer $currentCustomer,
PriceCurrencyInterface $priceCurrency,
ConfigurableAttributeData $configurableAttributeData,
array $data = []
array $data = [],
Format $localeFormat = null
) {
$this->priceCurrency = $priceCurrency;
$this->helper = $helper;
$this->jsonEncoder = $jsonEncoder;
$this->catalogProduct = $catalogProduct;
$this->currentCustomer = $currentCustomer;
$this->configurableAttributeData = $configurableAttributeData;
$this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(Format::class);

parent::__construct(
$context,
$arrayUtils,
$data
);
}

/**
* Get cache key informative items.
*
* @return array
*/
public function getCacheKeyInfo()
{
$parentData = parent::getCacheKeyInfo();
$parentData[] = $this->priceCurrency->getCurrencySymbol();
return $parentData;
}

/**
* Get allowed attributes
*
Expand Down Expand Up @@ -129,11 +154,14 @@ public function hasOptions()
public function getAllowProducts()
{
if (!$this->hasAllowProducts()) {
$products = [];
$skipSaleableCheck = $this->catalogProduct->getSkipSaleableCheck();

$products = $skipSaleableCheck ?
$this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null) :
$this->getProduct()->getTypeInstance()->getSalableUsedProducts($this->getProduct(), null);
$allProducts = $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null);
foreach ($allProducts as $product) {
if ($product->isSaleable() || $skipSaleableCheck) {
$products[] = $product;
}
}
$this->setAllowProducts($products);
}
return $this->getData('allow_products');
Expand Down Expand Up @@ -178,23 +206,23 @@ public function getJsonConfig()
$config = [
'attributes' => $attributesData['attributes'],
'template' => str_replace('%s', '<%- data.price %>', $store->getCurrentCurrency()->getOutputFormat()),
'currencyFormat' => $store->getCurrentCurrency()->getOutputFormat(),
'optionPrices' => $this->getOptionPrices(),
'priceFormat' => $this->localeFormat->getPriceFormat(),
'prices' => [
'oldPrice' => [
'amount' => $this->_registerJsPrice($regularPrice->getAmount()->getValue()),
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
],
'basePrice' => [
'amount' => $this->_registerJsPrice(
$finalPrice->getAmount()->getBaseAmount()
),
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
],
'finalPrice' => [
'amount' => $this->_registerJsPrice($finalPrice->getAmount()->getValue()),
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
],
],
'productId' => $currentProduct->getId(),
'chooseText' => __('Choose an Option...'),
'images' => isset($options['images']) ? $options['images'] : [],
'images' => $this->getOptionImages(),
'index' => isset($options['index']) ? $options['index'] : [],
];

Expand All @@ -207,45 +235,98 @@ public function getJsonConfig()
return $this->jsonEncoder->encode($config);
}

/**
* Get product images for configurable variations
*
* @return array
*/
protected function getOptionImages()
{
$images = [];
foreach ($this->getAllowProducts() as $product) {

$productImages = $this->helper->getGalleryImages($product) ?: [];
foreach ($productImages as $image) {
$images[$product->getId()][] =
[
'thumb' => $image->getData('small_image_url'),
'img' => $image->getData('medium_image_url'),
'full' => $image->getData('large_image_url'),
'caption' => $image->getLabel(),
'position' => $image->getPosition(),
'isMain' => $image->getFile() == $product->getImage(),
'type' => str_replace('external-', '', $image->getMediaType()),
'videoUrl' => $image->getVideoUrl(),
];
}
}

return $images;
}

/**
* @return array
*/
protected function getOptionPrices()
{
$prices = [];
foreach ($this->getAllowProducts() as $product) {
$tierPrices = [];
$priceInfo = $product->getPriceInfo();
$tierPriceModel = $priceInfo->getPrice('tier_price');
$tierPricesList = $tierPriceModel->getTierPriceList();
foreach ($tierPricesList as $tierPrice) {
$tierPrices[] = [
'qty' => $this->localeFormat->getNumber($tierPrice['price_qty']),
'price' => $this->localeFormat->getNumber($tierPrice['price']->getValue()),
'percentage' => $this->localeFormat->getNumber(
$tierPriceModel->getSavePercent($tierPrice['price'])
),
];
}

$prices[$product->getId()] =
[
'oldPrice' => [
'amount' => $this->_registerJsPrice(
'amount' => $this->localeFormat->getNumber(
$priceInfo->getPrice('regular_price')->getAmount()->getValue()
),
],
'basePrice' => [
'amount' => $this->_registerJsPrice(
'amount' => $this->localeFormat->getNumber(
$priceInfo->getPrice('final_price')->getAmount()->getBaseAmount()
),
],
'finalPrice' => [
'amount' => $this->_registerJsPrice(
'amount' => $this->localeFormat->getNumber(
$priceInfo->getPrice('final_price')->getAmount()->getValue()
),
]
];
],
'tierPrices' => $tierPrices,
];
}
return $prices;
}

/**
* Replace ',' on '.' for js
*
* @deprecated Will be removed in major release
* @param float $price
* @return string
*/
protected function _registerJsPrice($price)
{
return str_replace(',', '.', $price);
}

/**
* Should we generate "As low as" block or not
*
* @return bool
*/
public function showMinimalPrice()
{
return true;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/ConfigurableProduct/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<type name="Magento\Catalog\Model\Product\Attribute\Backend\Price">
<plugin name="configurable" type="Magento\ConfigurableProduct\Model\Plugin\PriceBackend" sortOrder="100" />
</type>
<type name="Magento\ProductVideo\Block\Product\View\Gallery">
<plugin name="product_video_gallery" type="Magento\ConfigurableProduct\Block\Plugin\Product\Media\Gallery" />
</type>
<type name="Magento\ConfigurableProduct\Model\Product\Type\Configurable">
<arguments>
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Collection</argument>
Expand Down
Loading

0 comments on commit cd6c087

Please sign in to comment.