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
declare(strict_types=1);
namespace Your\Module\Model\VirtualAttribute;
use Walkwizus\VirtualAttributeSalesRule\Api\Data\VirtualAttributeInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Rule\Model\Condition\AbstractCondition;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Phrase;
class ProductIsDiscounted implements VirtualAttributeInterface
{
/**
* @param TimezoneInterface $timezone
*/
public function __construct(
private readonly TimezoneInterface $timezone
) { }
/**
* @return Phrase|string
*/
public function getLabel(): Phrase|string
{
return __('Is Discounted Product');
}
/**
* @return string
*/
public function getType(): string
{
return 'boolean';
}
/**
* @param AbstractCondition $subject
* @param AbstractModel $model
* @return mixed
*/
public function getValue(AbstractCondition $subject, AbstractModel $model): mixed
{
/** @var \Magento\Catalog\Model\Product $model */
$specialPrice = $model->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;
}
}
declare(strict_types=1);
namespace Your\Module\Model\VirtualAttribute;
use Walkwizus\VirtualAttributeSalesRule\Api\Data\VirtualAttributeInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\Phrase;
use Magento\Rule\Model\Condition\AbstractCondition;
use Magento\Framework\Model\AbstractModel;
class CartUpdatedAt implements VirtualAttributeInterface
{
/**
* @param TimezoneInterface $timezone
*/
public function __construct(
private readonly TimezoneInterface $timezone
) { }
/**
* @return Phrase|string
*/
public function getLabel(): Phrase|string
{
return __('Minutes Since Last Update');
}
/**
* @return string
*/
public function getType(): string
{
return 'numeric';
}
/**
* @param AbstractCondition $subject
* @param AbstractModel $model
* @return mixed
*/
public function getValue(AbstractCondition $subject, AbstractModel $model): mixed
{
/** @var \Magento\Quote\Model\Quote $model */
$now = $this->timezone->date();
$updatedAt = $this->timezone->date($model->getUpdatedAt());
$differenceInSeconds = $now->getTimestamp() - $updatedAt->getTimestamp();
return (int) ($differenceInSeconds / 60);
}
}
declare(strict_types=1);
namespace Your\Module\Model\VirtualAttribute;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Phrase;
use Magento\Rule\Model\Condition\AbstractCondition;
use Walkwizus\VirtualAttributeSalesRule\Api\Data\VirtualAttributeInterface;
class CartItemWeight implements VirtualAttributeInterface
{
/**
* @return Phrase|string
*/
public function getLabel(): Phrase|string
{
return __('Item Weight');
}
/**
* @return string
*/
public function getType(): string
{
return 'numeric';
}
/**
* @param AbstractCondition $subject
* @param AbstractModel $model
* @return mixed
*/
public function getValue(AbstractCondition $subject, AbstractModel $model): mixed
{
/** @var \Magento\Quote\Model\Quote\Item $model */
return $model->getWeight();
}
}
declare(strict_types=1);
namespace Your\Module\Model\VirtualAttribute;
use Walkwizus\VirtualAttributeSalesRule\Api\Data\VirtualAttributeInterface;
use Magento\Framework\Phrase;
use Magento\Rule\Model\Condition\AbstractCondition;
use Magento\Framework\Model\AbstractModel;
class ProductCategory implements VirtualAttributeInterface
{
/**
* @return Phrase|string
*/
public function getLabel(): Phrase|string
{
return __('Special Category');
}
/**
* @return string
*/
public function getType(): string
{
return 'select';
}
/**
* @param AbstractCondition $subject
* @param AbstractModel $model
* @return mixed
*/
public function getValue(AbstractCondition $subject, AbstractModel $model): mixed
{
// Logic to determine which category value to return
return 'category_a';
}
/**
* @return array
*/
public function getOptionSource(): array
{
return [
['value' => 'category_a', 'label' => __('Category A')],
['value' => 'category_b', 'label' => __('Category B')],
['value' => 'category_c', 'label' => __('Category C')]
];
}
}