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