I'm trying to override Magento\Bundle\Model\Product\Type
for that i have add code indi.xml
<preference for="Magento\Bundle\Model\Product\Type" type="Tecksky\Catalog\Model\Product\Type" />
And have added file at app\code\Tecksky\Catalog\Model\Product\Type.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
namespace Tecksky\Catalog\Model\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Bundle\Model\ResourceModel\Selection\Collection\FilterApplier as SelectionCollectionFilterApplier;
/**
* Bundle Type Model
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @api
* @since 100.0.2
*/
class Type extends \Magento\Bundle\Model\Product\Type
{
/**
* Catalog data
*
* @var \Magento\Catalog\Helper\Data
*/
protected $_catalogData = null;
/**
* Catalog product
*
* @var \Magento\Catalog\Helper\Product
*/
protected $_catalogProduct = null;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Bundle\Model\OptionFactory
*/
protected $_bundleOption;
/**
* @var \Magento\Bundle\Model\ResourceModel\Selection
*/
protected $_bundleSelection;
/**
* @var \Magento\Catalog\Model\Config
*/
protected $_config;
/**
* @var \Magento\Bundle\Model\ResourceModel\Selection\CollectionFactory
*/
protected $_bundleCollection;
/**
* @var \Magento\Bundle\Model\ResourceModel\BundleFactory
*/
protected $_bundleFactory;
/**
* @var \Magento\Bundle\Model\SelectionFactory $bundleModelSelection
*/
protected $_bundleModelSelection;
/**
* @var PriceCurrencyInterface
*/
protected $priceCurrency;
/**
* @var \Magento\CatalogInventory\Api\StockRegistryInterface
*/
protected $_stockRegistry;
/**
* @var \Magento\CatalogInventory\Api\StockStateInterface
*/
protected $_stockState;
/**
* @var MetadataPool
*/
private $metadataPool;
/**
* @var SelectionCollectionFilterApplier
*/
private $selectionCollectionFilterApplier;
/**
* @param \Magento\Catalog\Model\Product\Option $catalogProductOption
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Catalog\Model\Product\Type $catalogProductType
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\Registry $coreRegistry
* @param \Psr\Log\LoggerInterface $logger
* @param ProductRepositoryInterface $productRepository
* @param \Magento\Catalog\Helper\Product $catalogProduct
* @param \Magento\Catalog\Helper\Data $catalogData
* @param \Magento\Bundle\Model\SelectionFactory $bundleModelSelection
* @param \Magento\Bundle\Model\ResourceModel\BundleFactory $bundleFactory
* @param \Magento\Bundle\Model\ResourceModel\Selection\CollectionFactory $bundleCollection
* @param \Magento\Catalog\Model\Config $config
* @param \Magento\Bundle\Model\ResourceModel\Selection $bundleSelection
* @param \Magento\Bundle\Model\OptionFactory $bundleOption
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param PriceCurrencyInterface $priceCurrency
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
* @param \Magento\CatalogInventory\Api\StockStateInterface $stockState
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
* @param MetadataPool|null $metadataPool
* @param SelectionCollectionFilterApplier|null $selectionCollectionFilterApplier
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Catalog\Model\Product\Option $catalogProductOption,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Catalog\Model\Product\Type $catalogProductType,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Registry $coreRegistry,
\Psr\Log\LoggerInterface $logger,
ProductRepositoryInterface $productRepository,
\Magento\Catalog\Helper\Product $catalogProduct,
\Magento\Catalog\Helper\Data $catalogData,
\Magento\Bundle\Model\SelectionFactory $bundleModelSelection,
\Magento\Bundle\Model\ResourceModel\BundleFactory $bundleFactory,
\Magento\Bundle\Model\ResourceModel\Selection\CollectionFactory $bundleCollection,
\Magento\Catalog\Model\Config $config,
\Magento\Bundle\Model\ResourceModel\Selection $bundleSelection,
\Magento\Bundle\Model\OptionFactory $bundleOption,
\Magento\Store\Model\StoreManagerInterface $storeManager,
PriceCurrencyInterface $priceCurrency,
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
\Magento\CatalogInventory\Api\StockStateInterface $stockState,
Json $serializer = null,
MetadataPool $metadataPool = null,
SelectionCollectionFilterApplier $selectionCollectionFilterApplier = null
) {
$this->_catalogProduct = $catalogProduct;
$this->_catalogData = $catalogData;
$this->_storeManager = $storeManager;
$this->_bundleOption = $bundleOption;
$this->_bundleSelection = $bundleSelection;
$this->_config = $config;
$this->_bundleCollection = $bundleCollection;
$this->_bundleFactory = $bundleFactory;
$this->_bundleModelSelection = $bundleModelSelection;
$this->priceCurrency = $priceCurrency;
$this->_stockRegistry = $stockRegistry;
$this->_stockState = $stockState;
$this->metadataPool = $metadataPool
?: ObjectManager::getInstance()->get(MetadataPool::class);
$this->selectionCollectionFilterApplier = $selectionCollectionFilterApplier
?: ObjectManager::getInstance()->get(SelectionCollectionFilterApplier::class);
parent::__construct(
$catalogProductOption,
$eavConfig,
$catalogProductType,
$eventManager,
$fileStorageDb,
$filesystem,
$coreRegistry,
$logger,
$productRepository,
$serializer
);
}
/**
* Retrieve bundle selections collection based on used options
*
* @param array $optionIds
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Bundle\Model\ResourceModel\Selection\Collection
*/
public function getSelectionsCollection($optionIds, $product)
{
$storeId = $product->getStoreId();
$metadata = $this->metadataPool->getMetadata(
\Magento\Catalog\Api\Data\ProductInterface::class
);
$selectionsCollection = $this->_bundleCollection->create()
->addAttributeToSelect($this->_config->getProductAttributes())
->addAttributeToSelect('tax_class_id') //used for calculation item taxes in Bundle with Dynamic Price
->addAttributeToSelect('weight')
->setFlag('product_children', true)
->setPositionOrder()
->addStoreFilter($this->getStoreFilter($product))
->setStoreId($storeId)
->addFilterByRequiredOptions()
->setOptionIdsFilter($optionIds);
$this->selectionCollectionFilterApplier->apply(
$selectionsCollection,
'parent_product_id',
$product->getData($metadata->getLinkField())
);
if (!$this->_catalogData->isPriceGlobal() && $storeId) {
$websiteId = $this->_storeManager->getStore($storeId)
->getWebsiteId();
$selectionsCollection->joinPrices($websiteId);
}
return $selectionsCollection;
}
}
I have just added ->addAttributeToSelect('weight') to this function.
How to override this function?
2 Answers 2
drop your __construct method because you are not adding anything new to it compared to the original class. Also you may need to copy all of the private methods from the original class into yours.
But to solve your problem.... you don't need to add weight to the select. Just edit the attribute in the backend and set the flag "Used in product listing" to "Yes". It should be picked up automatically.
A reindexing may be needed.
-
thanks it's solved. but I have copied all private method but still not override class? how is it possible?Ketan Borada– Ketan Borada2018年03月28日 11:03:20 +00:00Commented Mar 28, 2018 at 11:03
If you want to add weight attribute to collection, then you just
enable this attribute "Used in Product Listing" from
admin> Stores> Attributes> Product .Select weight attribute and then goto Storefront Properties.
Explore related questions
See similar questions with these tags.