Magento – Override the private function getConfiguredUsedProductCollection()

magento2overrides

I'm trying to override a private function in Magento\ConfigurableProduct\Model\Product\Type\Configurable.

I want to remove the line:

->addFilterByRequiredOptions()

private function getConfiguredUsedProductCollection(\Magento\Catalog\Model\Product $product)
{
    $collection = $this->getUsedProductCollection($product);
    $collection
        ->setFlag('has_stock_status_filter', true)
        ->addAttributeToSelect($this->getCatalogConfig()->getProductAttributes())
        ->addFilterByRequiredOptions()
        ->setStoreId($product->getStoreId());
    $requiredAttributes = ['name', 'price', 'weight', 'image', 'thumbnail', 'status', 'media_gallery'];
    foreach ($requiredAttributes as $attributeCode) {
        $collection->addAttributeToSelect($attributeCode);
    }
    foreach ($this->getUsedProductAttributes($product) as $usedProductAttribute) {
        $collection->addAttributeToSelect($usedProductAttribute->getAttributeCode());
    }
    $collection->addMediaGalleryData();
    $collection->addTierPriceData();
    return $collection;
}

Any idea how to achieve this?

Best Answer

Ah, i see 2.0-develop branch. I do not want to talk about sense of overriding PRIVATE method in such an old instance of magento but in general, to override any class you must add in di.xml new preference eg.

<preference for="Magento\ConfigurableProduct\Model\Product\Type\Configurable" type="Vendor\Module\YourClass" />

Then extend parent class and override requested method. Of course, since you are trying to override something that is private in very core class of product type which does not even exists in current version of magento, you will encounter problems with another private methods in this class not even mentioning eventual upgrading to newest version.

In general, what you try to do makes no sense at all at least in context of given details.