0

its very Urgent

I have an attribute called 'show_product' (in the backend of Magento) with two outcomes 'yes' or 'no'. At the moment I'm just echoing the product name on the frontend. However, ideally I'd like to show all product name the attribute value. For example, if 'yes' then show all product, if 'no' then No product.

Can anyone please help me.

enter image description here

I tried some this

     $objectManager = \Magento\Framework\App\objectManager::getInstance(); 

            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($singleproductdata['entity_id']);
            $attributes = $product->getAttributes('show_product');
           foreach ($attributes as $attribute) { 

                    echo $attribute->getAttributeCode();   
                    echo $attribute->getFrontend()->getValue($product);   

            }
8
  • You means if Show product is set to yes you want to display on frontend if no then you dont have to display product name , am i correct? Mar 18 '20 at 13:08
  • @HiteshAgrawal yes u are correct
    – DK Thakur
    Mar 18 '20 at 13:09
  • okay so I believe you should create a plugin after product collection and add addAttributeToFilter condition there. Mar 19 '20 at 6:09
  • @HiteshAgrawal you can do it because i tried but it didn't happen
    – DK Thakur
    Mar 19 '20 at 6:14
  • $objectManager = \Magento\Framework\App\objectManager::getInstance(); $product = $objectManager->create('Magento\Catalog\Model\Product')->load($singleproductdata['entity_id']); $attributes = $product->getAttributes('show_product'); foreach ($attributes as $attribute) { echo $attribute->getAttributeCode(); echo $attribute->getFrontend()->getValue($product); }
    – DK Thakur
    Mar 19 '20 at 6:17
0

First get Your Product Collection and then

$this->getProduct()->getAttributeText('show_product');
3
  • I am new in magento 2, please suggest how to create this functionality
    – DK Thakur
    Mar 19 '20 at 12:12
  • you can load product collection by product id and then apply getAttribteText method on it Mar 19 '20 at 12:33
  • $objectManager = \Magento\Framework\App\objectManager::getInstance(); $product = $objectManager->create('Magento\Catalog\Model\Product')->load($singleproductdata['entity_id']); $this->getProduct()->getAttributeText('show_product'); foreach ($attributes as $attribute) { echo $attribute->getAttributeCode(); echo $attribute->getFrontend()->getValue($product); } Right ?\
    – DK Thakur
    Mar 19 '20 at 12:57
0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($singleproductdata['entity_id']);
print_r($product->getCustomAttribute('show_product')->getValue());

please check your product id has attribute value set or not

click here source link

5
  • Thanks for reply let me check and update
    – DK Thakur
    Mar 19 '20 at 9:04
  • Let me inform it's works or not ?
    – Msquare
    Mar 19 '20 at 10:24
  • Sorry @Msquare this is not work
    – DK Thakur
    Mar 19 '20 at 10:33
  • please share your error
    – Msquare
    Mar 19 '20 at 11:07
  • check add link it's helps you.
    – Msquare
    Mar 19 '20 at 13:07
0

For filtering, product collection create after plugin for Magento\Catalog\Block\Product\ListProduct for the function getLoadedProductCollection()

/**
 * Retrieve loaded category collection
 *
 * @return AbstractCollection
 */
public function getLoadedProductCollection()
{
    return $this->_getProductCollection();
}
-1

If you really do want to show the product in forntend you can use Visibility set to "Not visible individually"

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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