PHP code example of walkwizus / magento2-module-virtual-attribute-sales-rule

1. Go to this page and download the library: Download walkwizus/magento2-module-virtual-attribute-sales-rule library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

walkwizus / magento2-module-virtual-attribute-sales-rule example snippets




namespace Your\Module\Model\VirtualAttribute;

use Walkwizus\VirtualAttributeSalesRule\Api\Data\VirtualAttributeInterface;
use Magento\Rule\Model\Condition\AbstractCondition;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Phrase;

class ProductCustomAttribute implements VirtualAttributeInterface
{
    public function getLabel(): Phrase|string
    {
        return __('Custom Product Attribute');
    }

    public function getType(): string
    {
        return 'select';
    }

    public function getValue(AbstractCondition $subject, AbstractModel $model): mixed
    {
        $product = $model->getProduct();
        
        // Your custom logic to determine the attribute value
        return 'option1';
    }

    public function getOptionSource(): array
    {
        return [
            ['value' => 'option1', 'label' => __('Option 1')],
            ['value' => 'option2', 'label' => __('Option 2')],
            ['value' => 'option3', 'label' => __('Option 3')],
        ];
    }
}



namespace Your\Module\Model\VirtualAttribute;

use Walkwizus\VirtualAttributeSalesRule\Api\Data\VirtualAttributeInterface;
use Magento\Rule\Model\Condition\AbstractCondition;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Phrase;

class AddressCustomAttribute implements VirtualAttributeInterface
{
    public function getLabel(): Phrase|string
    {
        return __('Custom Address Attribute');
    }

    public function getType(): string
    {
        return 'boolean';
    }

    public function getValue(AbstractCondition $subject, AbstractModel $model): mixed
    {
        $address = $model;
        if (!$address instanceof \Magento\Quote\Model\Quote\Address) {
            $address = $model->getQuote()->isVirtual()
                ? $model->getQuote()->getBillingAddress()
                : $model->getQuote()->getShippingAddress();
        }
        
        // Your custom logic to determine the attribute value
        return $address->getCountryId() === 'US' ? 1 : 0;
    }

    // Note: getOptionSource() is not implemented here because it's not 
xml
   <type name="Walkwizus\VirtualAttributeSalesRule\Model\ProductVirtualAttributeProvider">
        <arguments>
            <argument name="attributes" xsi:type="array">
                <item name="custom_product_attribute" xsi:type="object">Vendor\Module\Model\VirtualAttribute\CustomProductAttribute</item>
            </argument>
        </arguments>
   </type>
   
xml
   <type name="Walkwizus\VirtualAttributeSalesRule\Model\AddressVirtualAttributeProvider">
        <arguments>
            <argument name="attributes" xsi:type="array">
                <item name="custom_address_attribute" xsi:type="object">Vendor\Module\Model\VirtualAttribute\CustomAddressAttribute</item>
            </argument>
        </arguments>
   </type>