PHPackages                             walkwizus/magento2-module-virtual-attribute-sales-rule - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. walkwizus/magento2-module-virtual-attribute-sales-rule

ActiveMagento2-module

walkwizus/magento2-module-virtual-attribute-sales-rule
======================================================

Virtual Attribute Sales Rule for Magento 2

2.0.0(1y ago)840↓33.3%1MITPHPPHP ^8.1

Since Oct 6Pushed 1y ago1 watchersCompare

[ Source](https://github.com/walkwizus/magento2-module-virtual-attribute-sales-rule)[ Packagist](https://packagist.org/packages/walkwizus/magento2-module-virtual-attribute-sales-rule)[ Docs](https://github.com/walkwizus/magento2-module-virtual-attribute-sales-rule)[ RSS](/packages/walkwizus-magento2-module-virtual-attribute-sales-rule/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Walkwizus Virtual Attribute Sales Rule for Magento 2
====================================================

[](#walkwizus-virtual-attribute-sales-rule-for-magento-2)

Overview
--------

[](#overview)

The Virtual Attribute Sales Rule module for Magento 2 allows you to add virtual attributes to cart price rules conditions. This extension enhances the flexibility of your promotional rules by introducing dynamic attributes that can be used in three key areas:

- Cart Attributes
- Cart Item Attributes
- Product Attributes

Create more sophisticated and targeted promotional rules without core code modifications.

Features
--------

[](#features)

- Add virtual attributes to Cart Attribute conditions section
- Add virtual attributes to Cart Item Attribute conditions section
- Add virtual attributes to Product Attribute conditions section
- Support for multiple attribute types: string, numeric, date, select, boolean, and multiselect

Requirements
------------

[](#requirements)

- Magento 2.4.4 or later
- PHP 8.1 or later

Installation
------------

[](#installation)

### Using Composer (Recommended)

[](#using-composer-recommended)

1. In your Magento 2 root directory, run the following command:

    ```
    composer require walkwizus/magento2-module-virtual-attribute-sales-rule
    ```
2. Enable the module:

    ```
    bin/magento module:enable Walkwizus_VirtualAttributeSalesRule
    ```
3. Run the Magento setup upgrade:

    ```
    bin/magento setup:upgrade
    ```
4. Compile the code (in production mode):

    ```
    bin/magento setup:di:compile
    ```
5. Clear the cache:

    ```
    bin/magento cache:clean
    ```

Usage
-----

[](#usage)

### Step 1: Create Your Attribute Classes

[](#step-1-create-your-attribute-classes)

Create new classes that implement `Walkwizus\VirtualAttributeSalesRule\Api\Data\VirtualAttributeInterface`. Your classes must implement these methods:

- `getLabel()`: Returns the attribute's display name
- `getType()`: Returns the attribute's data type
- `getValue()`: Returns the attribute's value for a given model
- `getOptionSource()`: Returns available options (only for 'select' and 'multiselect' types)

#### Supported Attribute Types

[](#supported-attribute-types)

The following types can be returned by the `getType()` method:

TypeDescriptionstringA text valuenumericA number (integer or float)dateA date valueselectA single selection from a list of optionsbooleanA true/false valuemultiselectMultiple selections from a list of options**Note:** For 'select' and 'multiselect' types, you must implement the `getOptionSource()` method to provide the available options.

### Step 2: Add your attributes to di.xml

[](#step-2-add-your-attributes-to-dixml)

Create a `di.xml` file in your module's `etc` directory with the following structure:

```

                    Your\Module\Model\VirtualAttribute\YourProductAttribute

                    Your\Module\Model\VirtualAttribute\YourCartAttribute

                    Your\Module\Model\VirtualAttribute\YourCartItemAttribute

```

Replace `Your\Module\Model\VirtualAttribute\YourAttribute` with your actual attribute class paths, and `attribute_code` with the code you want to use for each attribute.

**Important**: Make sure each `attribute_code` is unique and does not conflict with any existing Magento attribute codes, as this could cause unexpected behavior or errors in your rules.

### Implementation Examples

[](#implementation-examples)

Here are examples of virtual attributes implementation for different section types:

#### Product Attribute Example

[](#product-attribute-example)

```
