3

In Magento 2 I want to be able to display the shopping cart (e.g. minicart) in the header of the shop only when products are displayed on the page that are salable. The shop I'm building has two types of products:

  • Products that are displayed as informative products (as a catalogue without purchase ability).
  • Products that should be purchased.

The last one will only be displayed in one (or maybe two) categories.

1
  • can you please share more info (with link) Commented Apr 25, 2018 at 11:18

1 Answer 1

1

For this first you need to create one product attribute like Salebale with Yes/No value

then need to add condition on minicart.phtml

app/design/frontend/Yourpackage/Yourtheme/Magento_Checkout/templates/cart/minicart.phtml

Here First you need to get current product as you need to hide minicart from product page so to get current product use below code

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
echo $product->getId();
echo $product->getName();
?>

Or Add bellow code in your block file. for example

app/code/Packagename/Modulename/Block/CustomBlock.php

<?php
namespace Packagename\Modulename\Block;
class CustomBlock extends \Magento\Framework\View\Element\Template
{
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,       
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {       
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCurrentProduct()
    {       
        return $this->_registry->registry('current_product');
    }   
}
?>

Printing current product data in your template (custom.phtml) file

if ($currentProduct = $block->getCurrentProduct()) {
    echo $currentProduct->getName() . '<br />';    
}

So when you get the current product object then get saleable attribute value using current product like below

<?php $saleable = $currentProduct->getSalebale(); ?>

then in above mentioned minicart.phtml add condition to show minicart only on Saleable product page. put all minicart code in condition like

<?php if($saleable): ?>
/******* Minicart content **********/
<?php endif; ?>

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.