Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento2: Add custom catalog rule

09 Sep 2021 » magento2

\app\code\Bhaveshpp\ShippingRestriction\etc\di.xml

<?xml version = "1.0"?>
<config xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogRule\Model\Rule\Condition\Combine">
         <plugin name="afterGetAllRates" type="Bhaveshpp\ShippingRestriction\Plugin\Magento\CatalogRule\Model\Rule\Condition\Combine" sortOrder="1" />
    </type>
</config>

\app\code\Bhaveshpp\ShippingRestriction\Plugin\Magento\CatalogRule\Model\Rule\Condition\Combine.php


<?php
namespace Bhaveshpp\ShippingRestriction\Plugin\Magento\CatalogRule\Model\Rule\Condition;
use Magento\CatalogRule\Model\Rule\Condition\Combine as Subject;
class Combine
{
    public function afterGetNewChildSelectOptions(Subject $subject, $conditions)
    {
        return ($this->getCondition($conditions));
    }

    public function getCondition(Array $condition): array
    {
        return array_merge($condition,[
            [
                "label" => __("Shipping Restriction"),
                "value" => $this->getConditionArray()
            ]
        ]);
    }

    public function getConditionArray(): array
    {
        return [
            [
                "label" => __("is Restricted time range"),
                "value" => \Bhaveshpp\ShippingRestriction\Model\Rule\Condition\CheckDate::class
            ]
        ];
    }
}

\app\code\Bhaveshpp\ShippingRestriction\Model\Rule\Condition\CheckDate.php


<?php
declare(strict_types=1);
namespace Bhaveshpp\ShippingRestriction\Model\Rule\Condition;

use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Phrase;
use Magento\Rule\Model\Condition\AbstractCondition;
use Magento\Rule\Model\Condition\Context;
use Bhaveshpp\ShippingRestriction\Model\Check as CheckModel;
class CheckDate extends AbstractCondition
{

    protected $checkModel;

    public function __construct(
        Context $context,
        CheckModel $checkModel,
        array $data = []
    ) {
        $this->checkModel = $checkModel;
        parent::__construct($context, $data);
    }

    public function collectValidatedAttributes(ProductCollection $collection): void
    {

    }

    public function validate(AbstractModel $model): bool
    {
        //preform validation
        return true|false;
    }

    public function getAttribute(): string
    {
        return 'in_date_time_range';
    }

    public function getAttributeElementHtml(): Phrase
    {
        return __('DateTime Range Apply');
    }

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

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

    public function getOperatorSelectOptions(): array
    {
        return [
            [
                'label' => __('is'),
                'value' => '=='
            ]
        ];
    }

    public function getValueSelectOptions(): array
    {
        return [
            [
                'label' => __('yes'),
                'value' => true
            ],
            [
                'label' => __('no'),
                'value' => false
            ]
        ];
    }

}