0

How to hide custom button in product details page by particular category in magento 2

In magento 2, custom button will be hidden for each product from the selected categories in admin. I want to admin setting is applied to product details page in frontend.

Admin Setting

product

Frontend enter image description here

1 Answer 1

4

==>You can get the current product category using the below code.

NOTE: Please do not use the Object manager

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Block\Product\View')->getProduct();
$categoryId = $product->getCategoryId();
echo $categoryId;

?>

==>And now get the array of the selected category from your backend and write the login as per the below code.

<?php
$selectedCategory = [2,3,5];

if (!in_array($categoryId , $selectedCategory))
    # code...
    #Add To Quote
}
?>

I hope this is helpful to you!!

4
  • it reflects a \view\frontend\templates\product\view\addtocart.phtml:47:boolean false Feb 11, 2020 at 11:31
  • it not show a category id Feb 11, 2020 at 11:31
  • Please check your class method in template. If there class method is available wich I was used in my code than work according. Feb 12, 2020 at 12:30
  • Other way you can assigned the the template with block wich i used in above code. Feb 12, 2020 at 12:33

Your Answer

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

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