0

I tried to add custom data to SalesRule through the endpoint V1/salesRules but ended up with getting error

My code is below

1- etc/extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\SalesRule\Api\Data\RuleInterface">
        <attribute code="customer_email" type="string"/>
        <attribute code="domain_name" type="string"/>
        <attribute code="phone_number" type="string"/>
    </extension_attributes>
</config>

2- 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\SalesRule\Api\RuleRepositoryInterface">
        <plugin name="vendor_extend_cart_rule_add_custom_data_to_sales_rule" type="Vendor\ExtendedCartRule\Plugin\AddCustomDataToSalesRule"/>
    </type>
   
</config>

3- Plugin

<?php
declare(strict_types=1);

namespace Vendor\ExtendedCartRule\Plugin;

use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory;
use Magento\SalesRule\Api\RuleRepositoryInterface;

class AddCustomDataToSalesRule
{
    private $techwareSalesRuleCollectionFactory;

    
    public function __construct(
        CollectionFactory $techwareSalesRuleCollectionFactory
    ) {
        $this->techwareSalesRuleCollectionFactory = $techwareSalesRuleCollectionFactory;
    }

    /**
     * @param SalesRuleRepositoryInterface $subject
     * @param $result
     * @return mixed
     */
    public function afterGet(
        RuleRepositoryInterface $subject,
        $result
    ) {
       
        
        $SalesRuleCollection = $this->techwareSalesRuleCollectionFactory->create();
        $techwareSalesRule = $SalesRuleCollection
            ->addFieldToFilter('rule_id', $result->getId())
            ->getFirstItem();
       
        $extensionAttributes = $result->getExtensionAttributes();
        
        $extensionAttributes->setData('customer_email', $techwareSalesRule->getData('customer_email'));
        $extensionAttributes->setData('domain_name', $techwareSalesRule->getData('domain_name'));
        $extensionAttributes->setData('phone_number', $techwareSalesRule->getData('phone_number'));

        $result->setExtensionAttributes($extensionAttributes);

        return $result;
    }

    /**
     * @param SalesRuleRepositoryInterface $subject
     * @param $result
     * @return mixed
     */
    public function afterGetList(
        RuleRepositoryInterface $subject,
        $result
    ) {
       
        foreach ($result->getItems() as $sales_rule) {
            $this->afterGet($subject, $sales_rule);
        }

        return $result;
    }
}

enter image description here

Here is the structure of Salesrule table after altering and adding custom column.

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.