How to apply custom conditions for product collection in Magento 2?

In Magento 2, There are some situations where you need to set custom conditions for layered(filter) navigation. In Magento 2, All product collection comes from Catalog Module at Layer.php file from vendor/magento/module-catalog/Model/Layer.php. Continue reading “How to apply custom conditions for product collection in Magento 2?”

How to import tier pricing-advanced price using csv in Magento 2?

In Magento 2 Import tier pricing is such a simple way.
You need to Create CSV file for import advanced pricing in Magento 2 sometimes Tier price is called as Advanced Pricing.

Tier price is used when you want to give discount on bulk purchase of product whether based on customer group, based on Quantity you purchase.

Go To Admin panel in Magento 2,
Click On System from Left Menu,
System -> Import -> Import Settings -> Advanced Pricing
Select Import Behaviour as Add/Update for first time pricing import for a product,

Below is column name for tier price import CSV
sku Product SKU
tier_price_website Website Code (Default ALL Website)
tier_price_customer_group Customer Group
tier_price_qty No. of Quantity to purchase for getting discount on a price
tier_price_value_type tier price value fixed/discount of the actual price

Download  Tier price CSV for demo, Tier Price CSV

If you want to replace old value of tier price with New You have to select Replace field from Import behavior in Import Tier price section from Admin panel otherwise price value will be added again for product.

Check Default Behaviour of Product import for Add, Replace and Delete function for Product import behavior.

If you don’t know about Import simple product refer link,Import Simple Product Using CSV Magento 2

How to create custom tabs in product page magento 2?

Let’s create a product tab in Magento 2 product page using the simple module. We want to display short content for a product using product tabs.

Example, We want to display short description info in product tabs.
Start with the Simple module in Magento 2, create registration.php file to register our module.

Path, app/code/Rbj/ProductTabs/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Rbj_ProductTabs',
    __DIR__
);

File path, app/code/Rbj/ProductTabs/etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Rbj_ProductTabs" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

We have passed dependency of Catalog module in sequence tag of our module.
Now We want to create catalog_product_view.xml file for display our custom tabs of the short description.
Create a layout file,
Path, app/code/Rbj/ProductTabs/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.details">
        	<!-- For short description tab -->
            <block class="Rbj\ProductTabs\Block\ExtraInfo" name="product.short.description" template="Rbj_ProductTabs::short-descripiton.phtml" group="detailed_info">
                <arguments>
                    <argument name="title" translate="true" xsi:type="string">Extra Info</argument>
                </arguments>
            </block>
   		</referenceBlock>
   	</body>
</page>

We have keep our tab name is Extra Info if you want to change tabs title you can replace Extra info with your title in argument tag.  In the above layout file, We must need to pass group=”detailed_info”  in the block class.

Create template file for call short description content in tabs using template, Path, app/code/Rbj/ProductTabs/view/frontend/templates/short-descripiton.phtml

<?php $extraInfo = $block->getTabsContent(); ?>

<div class="short">
    <?php echo $extraInfo; ?>
</div>

We need to create ExtraInfo.php file to check if a product has short description we need to display tabs otherwise we don’t need to display empty tabs for the short description.

Path, app/code/Rbj/ProductTabs/Block/ExtraInfo.php

<?php

namespace Rbj\ProductTabs\Block;

class ExtraInfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
         array $data = []
    ) {
        $this->_coreRegistry = $context->getRegistry();
        parent::__construct($context, $data);
    }

    /**
     * Retrieve current product object
     *
     * @return \Magento\Catalog\Model\Product
     */
    public function getProduct()
    {
        if (!$this->hasData('product')) {
            $this->setData('product', $this->_coreRegistry->registry('product'));
        }
        return $this->getData('product');
    }

    /**
     * @return string
     */
    public function getTabsContent()
    {
        return $this->getProduct()->getShortDescription();
    }

    /**
     * @return return HTML
     */
    protected function _toHtml()
    {
        if(!empty($this->getTabsContent())) {
            return parent::_toHtml();
        }

        return false;
    }
}

Now run command to activate our module,

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

You can see new tabs on the product page in the tabs section below the image section. Your new tab will be displayed if your product has the short description otherwise tabs will not display in product page.
You can add new attribute details using the above way.