0

I have created a custom attribute with name product_deisgn_proof. I want to filter this attribute using product collection in custom template file. When I try to fetch collection of product in an array it gives product_design_proof undefined index. Here is the code I am using to filter the attribute in product collection. enter image description here

Please give me a better solution to fetch the product collection with filter attribute.

1
  • Can anyone give me solution of this issue ? Mar 26, 2020 at 5:32

1 Answer 1

0

try below code

Using ObjectManger

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//get current product
$currentProduct = $objectManager->get('Magento\framework\Registry')->registry('current_product');
$currentProductData = $objectManager->get('Magento\Catalog\Model\Product')->load($currentProduct->getId());

echo $currentProductData->getId();
echo $currentProductData->getName();

$customise = $currentProductData->getProductDesignProof();


/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection
            ->addAttributeToSelect('*')
            ->addAttributeToSelect('product_design_proof', $customise)
            ->addAttributeToFilter('visibility', Visibility::VISIBILITY_BOTH)
            ->addAttributeToFilter('status',Status::STATUS_ENABLED)
            ->load();

foreach ($collection as $product){
     echo 'Name  =  '.$product->getName().'<br>';
}  
?>

best way to create the block and pass product data to the template file.

I hope this will help you..!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.