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">
&lt;type name=&quot;Magento\SalesRule\Api\RuleRepositoryInterface&quot;&gt;
    &lt;plugin name=&quot;vendor_extend_cart_rule_add_custom_data_to_sales_rule&quot; type=&quot;Vendor\ExtendedCartRule\Plugin\AddCustomDataToSalesRule&quot;/&gt;
&lt;/type&gt;

</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-&gt;techwareSalesRuleCollectionFactory = $techwareSalesRuleCollectionFactory;
}

/**
 * @param SalesRuleRepositoryInterface $subject
 * @param $result
 * @return mixed
 */
public function afterGet(
    RuleRepositoryInterface $subject,
    $result
) {


    $SalesRuleCollection = $this-&gt;techwareSalesRuleCollectionFactory-&gt;create();
    $techwareSalesRule = $SalesRuleCollection
        -&gt;addFieldToFilter('rule_id', $result-&gt;getId())
        -&gt;getFirstItem();

    $extensionAttributes = $result-&gt;getExtensionAttributes();

    $extensionAttributes-&gt;setData('customer_email', $techwareSalesRule-&gt;getData('customer_email'));
    $extensionAttributes-&gt;setData('domain_name', $techwareSalesRule-&gt;getData('domain_name'));
    $extensionAttributes-&gt;setData('phone_number', $techwareSalesRule-&gt;getData('phone_number'));

    $result-&gt;setExtensionAttributes($extensionAttributes);

    return $result;
}

/**
 * @param SalesRuleRepositoryInterface $subject
 * @param $result
 * @return mixed
 */
public function afterGetList(
    RuleRepositoryInterface $subject,
    $result
) {

    foreach ($result-&gt;getItems() as $sales_rule) {
        $this-&gt;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

ishaq
  • 195
  • 1
  • 14
  • In the body of your request, you should create the node for the extension_attributes. Eg.: "extension_attributes": {"phone_number": "+1 999 - 9998"} – Eduardo Oct 19 '22 at 18:13
  • hey do you found any solution? I face same issue to get & save custom extension attribute in sales rule api. Please share your source code for refernce – Harsh Patel Nov 01 '22 at 10:22
  • If anyone faced any issue to save extension attribute in salesrule api then see here https://magento.stackexchange.com/q/361475/93504 – Harsh Patel Nov 08 '22 at 08:58

0 Answers0