Skip to content

Commit

Permalink
MAGETWO-8709: [GITHUB] Child product image should be shown in Wishist…
Browse files Browse the repository at this point in the history
… if options are selected for configurable product #8168

- fixing formatting
  • Loading branch information
cpartica committed Jul 11, 2018
1 parent e1ffa88 commit 838182a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
*/
namespace Magento\Catalog\Model\Product\Configuration\Item;

use Magento\Catalog\Api\Data\ProductInterface;

/**
* Composite implementation for @see ItemResolverInterface
* {@inheritdoc}
*/
class ItemResolverComposite implements ItemResolverInterface
{
Expand All @@ -24,14 +26,14 @@ public function __construct(array $itemResolvers)
/**
* {@inheritdoc}
*/
public function getFinalProduct(
\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
) : \Magento\Catalog\Api\Data\ProductInterface {
public function getFinalProduct(ItemInterface $item) : ProductInterface
{
$product = $item->getProduct();
foreach ($this->itemResolvers as $resolver) {
$resolvedProduct = $resolver->getFinalProduct($item);
if ($resolvedProduct !== $product) {
return $resolvedProduct;
$product = $resolvedProduct;
break;
}
}
return $product;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
*/
namespace Magento\Catalog\Model\Product\Configuration\Item;

use Magento\Catalog\Api\Data\ProductInterface;

/**
* Resolves the product for a configured item
* Resolves the product from a configured item.
*
* @api
*/
interface ItemResolverInterface
{
/**
* Get the final product from a configured item by product type and selection
* Get the final product from a configured item by product type and selection.
*
* @param ItemInterface $item
* @return \Magento\Catalog\Api\Data\ProductInterface
* @return ProductInterface
*/
public function getFinalProduct(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item)
: \Magento\Catalog\Api\Data\ProductInterface;
public function getFinalProduct(ItemInterface $item) : ProductInterface;
}
2 changes: 1 addition & 1 deletion app/code/Magento/Checkout/Model/Cart/ImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
) {
$this->itemRepository = $itemRepository;
$this->itemPool = $itemPool;
$this->customerDataItem = $customerDataItem ?? ObjectManager::getInstance()->get(DefaultItem::class);
$this->customerDataItem = $customerDataItem ?: ObjectManager::getInstance()->get(DefaultItem::class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function getOptionList()

/**
* {@inheritdoc}
* @deprecated
* @deprecated because parent can handle the logic for images of all product types
* @see \Magento\Checkout\Block\Cart\Item\Renderer::getProductForThumbnail
*/
public function getProductForThumbnail()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,69 @@
namespace Magento\ConfigurableProduct\Model\Product\Configuration\Item;

use Magento\Catalog\Model\Config\Source\Product\Thumbnail;
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Catalog\Model\Product;

/**
* Resolves the product for a configured option item
* {@inheritdoc}
*/
class ItemProductResolver implements \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface
class ItemProductResolver implements ItemResolverInterface
{
const CONFIG_THUMBNAIL_SOURCE = 'checkout/cart/configurable_product_image';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

/**
* Identify the product from which thumbnail should be taken.
*
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
* @return \Magento\Catalog\Api\Data\ProductInterface
* {@inheritdoc}
*/
public function getFinalProduct(
\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
) : \Magento\Catalog\Api\Data\ProductInterface {
public function getFinalProduct(ItemInterface $item) : ProductInterface
{
/**
* Show parent product thumbnail if it must be always shown according to the related setting in system config
* or if child thumbnail is not available
* or if child thumbnail is not available.
*/
$parentItem = $item->getProduct();
$config = $this->scopeConfig->getValue(
\Magento\ConfigurableProduct\Block\Cart\Item\Renderer\Configurable::CONFIG_THUMBNAIL_SOURCE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

return $config == Thumbnail::OPTION_USE_PARENT_IMAGE
|| (!$this->getChildProduct($item)->getData('thumbnail')
|| $this->getChildProduct($item)->getData('thumbnail') == 'no_selection')
? $parentItem
: $this->getChildProduct($item);
$childProduct = $this->getChildProduct($item);
$childThumbnail = $childProduct->getData('thumbnail');
$finalProduct =
($config == Thumbnail::OPTION_USE_PARENT_IMAGE) || (!$childThumbnail || $childThumbnail == 'no_selection')
? $parentItem
: $childProduct;
return $finalProduct;
}

/**
* Get item configurable child product
*
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
* @return \Magento\Catalog\Model\Product
* @param ItemInterface $item
* @return Product
*/
private function getChildProduct(
\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
) : \Magento\Catalog\Model\Product {
private function getChildProduct(ItemInterface $item) : Product
{
$option = $item->getOptionByCode('simple_product');
$product = $item->getProduct();
if ($option) {
return $option->getProduct();
$product = $option->getProduct();
}
return $item->getProduct();
return $product;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function getGroupedProduct()

/**
* {@inheritdoc}
* @deprecated
* @deprecated because parent can handle the logic for images of all product types
* @see \Magento\Checkout\Block\Cart\Item\Renderer::getProductForThumbnail
*/
public function getProductForThumbnail()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,40 @@
namespace Magento\GroupedProduct\Model\Product\Configuration\Item;

use Magento\Catalog\Model\Config\Source\Product\Thumbnail;
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;

/**
* Resolves the product for a configured option item
* {@inheritdoc}
*/
class ItemProductResolver implements \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface
class ItemProductResolver implements ItemResolverInterface
{
/**
* Path in config to the setting which defines if parent or child product should be used to generate a thumbnail.
*/
const CONFIG_THUMBNAIL_SOURCE = 'checkout/cart/grouped_product_image';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

/**
* Identify the product from which thumbnail should be taken.
*
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
* @return \Magento\Catalog\Api\Data\ProductInterface
* {@inheritdoc}
*/
public function getFinalProduct(
\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
) : \Magento\Catalog\Api\Data\ProductInterface {
public function getFinalProduct(ItemInterface $item) : ProductInterface
{
/**
* Show grouped product thumbnail if it must be always shown according to the related setting in system config
* or if child product thumbnail is not available
Expand All @@ -48,25 +49,28 @@ public function getFinalProduct(
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$childProduct = $item->getProduct();
return $config == Thumbnail::OPTION_USE_PARENT_IMAGE ||
(!$childProduct->getData('thumbnail') || $childProduct->getData('thumbnail') == 'no_selection')
$childThumbnail = $childProduct->getData('thumbnail');

$finalProduct =
($config == Thumbnail::OPTION_USE_PARENT_IMAGE) || (!$childThumbnail || $childThumbnail == 'no_selection')
? $this->getParentProduct($item)
: $childProduct;
return $finalProduct;
}

/**
* Get grouped product
*
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
* @return \Magento\Catalog\Model\Product
* @param ItemInterface $item
* @return Product
*/
private function getParentProduct(
\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
) : \Magento\Catalog\Model\Product {
private function getParentProduct(ItemInterface $item) : Product
{
$option = $item->getOptionByCode('product_type');
$product = $item->getProduct();
if ($option) {
return $option->getProduct();
$product = $option->getProduct();
}
return $item->getProduct();
return $product;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
UrlBuilder $urlBuilder = null,
ItemResolverInterface $itemResolver = null
) {
$this->itemResolver = $itemResolver ?? ObjectManager::getInstance()->get(ItemResolverInterface::class);
$this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
parent::__construct(
$context,
$httpContext,
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Wishlist/CustomerData/Wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(
$this->imageHelperFactory = $imageHelperFactory;
$this->block = $block;
$this->view = $view;
$this->itemResolver = $itemResolver ?? ObjectManager::getInstance()->get(
$this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class
);
}
Expand Down

0 comments on commit 838182a

Please sign in to comment.