Magento 2: How to Disable Specific Product Attribute in Catalog Price Rule

How to Disable Specific Product Attribute in Catalog Price Rule

Hello Magento Friends,

Magento 2’s Catalog Price Rules provide a powerful way to offer discounts based on product attributes. But sometimes, you might want to exclude certain custom attributes from being used in catalog price rules—for instance, to prevent misuse or maintain pricing consistency.

Learn –

Create Catalog Price Rules from the Admin Dashboard

Create Catalog Price Rules Programmatically

In this blog, we’ll show you how to disable specific product attributes so they don’t appear in the conditions list of Catalog Price Rules in Magento 2.

Hire Magento Developer

Steps to Disable Specific Product Attribute in Catalog Price Rule in Magento 2:

Step 1: First, we need to create a “di.xml“ file inside our extension at the following path

app\code\Vendor\Extension\etc\di.xml

Now add the code as follows

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogRule\Model\Rule\Condition\Product">
        <plugin name="hide_specific_attribute_plugin" type="Vendor\Extension\Plugin\Hideattribute" />
    </type>
</config>

Step 2: After that, we need to create a “Hideattribute.php” file inside our extension at the following path

app\code\Vendor\Extension\Plugin\Hideattribute.php

And add the code as given below

<?php
namespace Vendor\Extension\Plugin;

class Hideattribute
{
    public function afterLoadAttributeOptions(\Magento\CatalogRule\Model\Rule\Condition\Product $subject, $result)
    {
        $attributes = $subject->getAttributeOption();
        $attributeToHide = 'your_attribute_code'; // Replace with your attribute code

        if (isset($attributes[$attributeToHide])) {
            unset($attributes[$attributeToHide]);
        }

        $subject->setAttributeOption($attributes);
        return $result;
    }
}
?>

Conclusion:

Hence, this way you can disable a specific product attribute in a catalog price rule in Magento 2. Feel free to reach out in the comments if you have any questions or need help implementing this in your Magento 2 store!

Happy Coding!

Hire Magento Programmer
Previous Article

Laravel: Livewire Lifecycle Hooks with Examples

Next Article

Laravel HTTP Testing Cheat Sheet

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨