An open API service indexing awesome lists of open source software.

https://github.com/walkwizus/magento2-module-virtual-attribute-sales-rule

Virtual Attribute Sales Rule for Magento 2
https://github.com/walkwizus/magento2-module-virtual-attribute-sales-rule

adobe-commerce magento magento2 magento2-extension magento2-extension-free magento2-modu marketing php sales-rules

Last synced: 2 months ago
JSON representation

Virtual Attribute Sales Rule for Magento 2

Awesome Lists containing this project

README

          

# Walkwizus Virtual Attribute Sales Rule for Magento 2

## 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

- 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

- Magento 2.4.4 or later
- PHP 8.1 or later

## Installation

### Using Composer (Recommended)

1. In your Magento 2 root directory, run the following command:
```bash
composer require walkwizus/magento2-module-virtual-attribute-sales-rule
```

2. Enable the module:
```bash
bin/magento module:enable Walkwizus_VirtualAttributeSalesRule
```

3. Run the Magento setup upgrade:
```bash
bin/magento setup:upgrade
```

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

5. Clear the cache:
```bash
bin/magento cache:clean
```

## Usage

### 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

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

| Type | Description |
|-------------|--------------------------------------------------|
| string | A text value |
| numeric | A number (integer or float) |
| date | A date value |
| select | A single selection from a list of options |
| boolean | A true/false value |
| multiselect | Multiple 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

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

```xml






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

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

#### Product Attribute Example

```php
getSpecialPrice();

if ($specialPrice && $specialPrice < $model->getPrice()) {
$from = $model->getSpecialFromDate();
$to = $model->getSpecialToDate();

$now = $this->timezone->date()->format('Y-m-d H:i:s');

if ((!$from || $from <= $now) && (!$to || $to >= $now)) {
return true;
}
}

return false;
}
}
```

#### Cart Attribute Example

```php
timezone->date();
$updatedAt = $this->timezone->date($model->getUpdatedAt());

$differenceInSeconds = $now->getTimestamp() - $updatedAt->getTimestamp();

return (int) ($differenceInSeconds / 60);
}
}
```

#### Cart Item Attribute Example

```php
getWeight();
}
}
```

#### Select/Multiselect Example

```php
'category_a', 'label' => __('Category A')],
['value' => 'category_b', 'label' => __('Category B')],
['value' => 'category_c', 'label' => __('Category C')]
];
}
}
```

### Step 3: Use in Sales Rules

After installing your module and implementing your virtual attributes:

1. Go to **Marketing > Promotions > Cart Price Rules** in the Magento admin
2. Create or edit a rule
3. In the "Conditions" tab, you'll see your virtual attributes available in the respective sections:
- Product attributes in the "Product attribute combination" condition
- Cart attributes in the "Cart attribute" condition
- Cart item attributes in the "Product attribute" condition (with "(Virtual Attribute)" suffix)

## Support

For issues and support, please create an issue on the [GitHub repository](https://github.com/walkwizus/magento2-module-virtual-attribute-sales-rule/issues).