-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d660117
commit eb67938
Showing
14 changed files
with
408 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
|
||
namespace OuterEdge\ConfigProduct\Block\Product\View\Type; | ||
|
||
use Magento\Swatches\Block\Product\Renderer\Configurable as SwatchConfigurable; | ||
use Magento\Catalog\Block\Product\Context; | ||
use Magento\Framework\Stdlib\ArrayUtils; | ||
use Magento\Framework\Json\EncoderInterface; | ||
use Magento\Framework\Json\DecoderInterface; | ||
use Magento\ConfigurableProduct\Helper\Data; | ||
use Magento\Catalog\Helper\Product as CatalogProduct; | ||
use Magento\Customer\Helper\Session\CurrentCustomer; | ||
use Magento\Framework\Pricing\PriceCurrencyInterface; | ||
use Magento\ConfigurableProduct\Model\ConfigurableAttributeData; | ||
use Magento\Swatches\Helper\Data as SwatchData; | ||
use Magento\Swatches\Helper\Media; | ||
use Magento\Swatches\Model\SwatchAttributesProvider; | ||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\Locale\Format; | ||
use Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory; | ||
|
||
/** | ||
* Class Configurable | ||
* @package OuterEdge\ConfigProduct\Block\Product\View\Type | ||
*/ | ||
class Configurable extends SwatchConfigurable | ||
{ | ||
/** | ||
* @var SwatchAttributesProvider | ||
*/ | ||
private $swatchAttributesProvider; | ||
|
||
/** | ||
* @var Format | ||
*/ | ||
private $localeFormat; | ||
|
||
/** | ||
* @var jsonDecoder interface | ||
*/ | ||
protected $jsonDecoder; | ||
|
||
/** | ||
* @var StockRepository | ||
*/ | ||
protected $_stockRepository; | ||
/** | ||
* @var Context | ||
*/ | ||
private $context; | ||
/** | ||
* @var StockItemInterfaceFactory | ||
*/ | ||
private $stockItemInterfaceFactory; | ||
|
||
/** | ||
* @param Context $context | ||
* @param ArrayUtils $arrayUtils | ||
* @param EncoderInterface $jsonEncoder | ||
* @param DecoderInterface $jsonDecoder | ||
* @param Data $helper | ||
* @param CatalogProduct $catalogProduct | ||
* @param CurrentCustomer $currentCustomer | ||
* @param PriceCurrencyInterface $priceCurrency | ||
* @param ConfigurableAttributeData $configurableAttributeData | ||
* @param SwatchData $swatchHelper | ||
* @param Media $swatchMediaHelper | ||
* @param array $data other data | ||
* @param SwatchAttributesProvider $swatchAttributesProvider | ||
* @param Format|null $localeFormat | ||
* @param StockItemInterfaceFactory $stockItemInterfaceFactory | ||
* @SuppressWarnings(PHPMD.ExcessiveParameterList) | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
ArrayUtils $arrayUtils, | ||
EncoderInterface $jsonEncoder, | ||
DecoderInterface $jsonDecoder, | ||
Data $helper, | ||
CatalogProduct $catalogProduct, | ||
CurrentCustomer $currentCustomer, | ||
PriceCurrencyInterface $priceCurrency, | ||
ConfigurableAttributeData $configurableAttributeData, | ||
SwatchData $swatchHelper, | ||
Media $swatchMediaHelper, | ||
array $data = [], | ||
SwatchAttributesProvider $swatchAttributesProvider = null, | ||
Format $localeFormat = null, | ||
StockItemInterfaceFactory $stockItemInterfaceFactory | ||
){ | ||
$this->swatchHelper = $swatchHelper; | ||
$this->swatchMediaHelper = $swatchMediaHelper; | ||
$this->_stockRepository = $stockItemInterfaceFactory->create(); | ||
$this->swatchAttributesProvider = $swatchAttributesProvider | ||
?: ObjectManager::getInstance()->get(SwatchAttributesProvider::class); | ||
$this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(Format::class); | ||
$this->jsonDecoder =$jsonDecoder; | ||
|
||
parent::__construct( | ||
$context, | ||
$arrayUtils, | ||
$jsonEncoder, | ||
$helper, | ||
$catalogProduct, | ||
$currentCustomer, | ||
$priceCurrency, | ||
$configurableAttributeData, | ||
$swatchHelper, | ||
$swatchMediaHelper, | ||
$data | ||
); | ||
$this->context = $context; | ||
$this->stockItemInterfaceFactory = $stockItemInterfaceFactory; | ||
} | ||
|
||
/** | ||
* Sets product stock status. | ||
* | ||
* @param $productId | ||
* | ||
* @return array | ||
*/ | ||
public function getStockItem($productId) | ||
{ | ||
$stock_data = array(); | ||
$stock_data['out_stock'] = 0 ; | ||
|
||
$stock = $this->_stockRepository->load($productId,'product_id'); | ||
if (!$stock->getIsInStock()) { | ||
$stock_data['out_stock'] = 1; | ||
} | ||
|
||
return $stock_data; | ||
} | ||
|
||
/** | ||
* Get Product Stock | ||
* | ||
* @return array | ||
*/ | ||
public function getProductStock() | ||
{ | ||
$stock = []; | ||
$skipSaleableCheck=true; | ||
$allProducts = $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct(), null); | ||
|
||
foreach ($allProducts as $product) { | ||
if ($product->isSaleable() || $skipSaleableCheck) { | ||
$stock[$product->getId()] = $this->getStockItem($product->getId()); | ||
} | ||
} | ||
return $stock; | ||
} | ||
|
||
/** | ||
* Extend the configuration for js to add stock values. | ||
* | ||
* @return string | ||
*/ | ||
public function getJsonConfig() | ||
{ | ||
$config = $this->jsonDecoder->decode(parent::getJsonConfig()); | ||
$currentProduct = $this->getProduct(); | ||
$options = $this->helper->getOptions($currentProduct, $this->getAllowProducts(),$this->getProductStock()); | ||
//Adding stock details to the config product options. | ||
$config['stock']=isset($options['stock']) ? $options['stock'] : []; | ||
return $this->jsonEncoder->encode($config); | ||
} | ||
|
||
/** | ||
* Get Allowed Products | ||
* Modified to set $skipSaleableCheck to true | ||
* | ||
* @return \Magento\Catalog\Model\Product[] | ||
*/ | ||
public function getAllowProducts() | ||
{ | ||
if (!$this->hasAllowProducts()) { | ||
$products = []; | ||
//$skipSaleableCheck = $this->catalogProduct->getSkipSaleableCheck(); | ||
//Setting $skipSaleableCheck true as it is hardcoded false in the parent. | ||
$skipSaleableCheck=true; | ||
$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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace OuterEdge\ConfigProduct\Helper; | ||
|
||
class Data extends \Magento\ConfigurableProduct\Helper\Data | ||
{ | ||
|
||
/** | ||
* Get Options for Configurable Product Options | ||
* | ||
* Modified to add out of stock status option. | ||
* | ||
* @param \Magento\Catalog\Model\Product $currentProduct | ||
* @param array $allowedProducts | ||
* @return array | ||
*/ | ||
public function getOptions($currentProduct, $allowedProducts, $stockdata = null) | ||
{ | ||
$options = []; | ||
$allowAttributes = $this->getAllowAttributes($currentProduct); | ||
|
||
foreach ($allowedProducts as $product) { | ||
$productId = $product->getId(); | ||
foreach ($allowAttributes as $attribute) { | ||
$productAttribute = $attribute->getProductAttribute(); | ||
$productAttributeId = $productAttribute->getId(); | ||
$attributeValue = $product->getData($productAttribute->getAttributeCode()); | ||
|
||
$options[$productAttributeId][$attributeValue][] = $productId; | ||
$options['index'][$productId][$productAttributeId] = $attributeValue; | ||
} | ||
//Adding stock status in the option list. | ||
$options['stock'][$productId][] = $stockdata[$productId]['out_stock']; | ||
} | ||
return $options; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Plugin/Model/ResourceModel/Attribute/InStockOptionSelectBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace OuterEdge\ConfigProduct\Plugin\Model\ResourceModel\Attribute; | ||
|
||
use Magento\CatalogInventory\Api\StockConfigurationInterface; | ||
use \Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute\InStockOptionSelectBuilder as MageInStockOptionSelectBuilder; | ||
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status; | ||
use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface; | ||
use Magento\Framework\DB\Select; | ||
|
||
class InStockOptionSelectBuilder extends MageInStockOptionSelectBuilder | ||
{ | ||
/** | ||
* CatalogInventory Stock Status Resource Model. | ||
* | ||
* @var Status | ||
*/ | ||
private $stockStatusResource; | ||
/** | ||
* | ||
* @var Configuration | ||
*/ | ||
private $stockConfiguration; | ||
/** | ||
* @param Status $stockStatusResource | ||
*/ | ||
|
||
public function __construct(Status $stockStatusResource, StockConfigurationInterface $stockConfiguration) | ||
{ | ||
$this->stockStatusResource = $stockStatusResource; | ||
$this->stockConfiguration = $stockConfiguration; | ||
} | ||
/** | ||
* Add stock status filter to select. | ||
* | ||
* @param OptionSelectBuilderInterface $subject | ||
* @param Select $select | ||
* @return Select | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterGetSelect(OptionSelectBuilderInterface $subject, Select $select) | ||
{ | ||
//Ignore the stock status in the configuration option, if the show out of stock is set. | ||
if (!$this->stockConfiguration->isShowOutOfStock()) { | ||
$select->joinInner( | ||
['stock' => $this->stockStatusResource->getMainTable()], | ||
'stock.product_id = entity.entity_id', | ||
[] | ||
)->where( | ||
'stock.stock_status = ?', | ||
\Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK | ||
); | ||
} | ||
return $select; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.