I need to save custom field data in apply_on_weekday
column which is located at salesrule
Db table. I tried to much but it doesn’t save it in db. i share my code.
vendorModuleetcextension_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="MagentoSalesRuleApiDataRuleInterface">
<attribute code="apply_on_weekday" type="string[]" />
</extension_attributes>
</config>
VendorModuleviewadminhtmlui_componentsales_rule_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="rule_information" sortOrder="10">
<field name="apply_on_weekday" formElement="multiselect">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<!-- <item name="dataType" xsi:type="string">int</item> -->
<item name="source" xsi:type="string">sales_rule</item>
<item name="default" xsi:type="string">Mon</item>
</item>
</argument>
<settings>
<label translate="true">Apply for specific weekdays</label>
<dataScope>apply_on_weekday</dataScope>
</settings>
<formElements>
<multiselect>
<settings>
<options class="VendorModuleModelConfigSourceWeekdays"/>
</settings>
</multiselect>
</formElements>
</field>
</fieldset>
</form>
VendorModuleModelConfigSourceWeekdays.php file
<?php
namespace VendorModuleModelConfigSource;
use MagentoFrameworkDataOptionSourceInterface;
class Weekdays implements OptionSourceInterface
{
/**
* Value which equal "Monday" for Weekdays dropdown.
*/
const MONDAY = "Mon";
/**
* Value which equal "Tuesday" for Weekdays dropdown.
*/
const TUESDAY = "Tue";
/**
* Value which equal "Wednesday" for Weekdays dropdown.
*/
const WEDNESDAY = "Wed";
/**
* Value which equal "Thursday" for Weekdays dropdown.
*/
const THURSDAY = "Thu";
/**
* Value which equal "Friday" for Weekdays dropdown.
*/
const FRIDAY = "Fri";
/**
* Value which equal "Saturday" for Weekdays dropdown.
*/
const SATURDAY = "Sat";
/**
* Value which equal "Sunday" for Weekdays dropdown.
*/
const SUNDAY = "Sun";
/**
* {@inheritdoc}
*/
public function toOptionArray()
{
return [
['value' => self::MONDAY, 'label' => __('Mon')],
['value' => self::TUESDAY, 'label' => __('Tue')],
['value' => self::WEDNESDAY, 'label' => __('Wed')],
['value' => self::THURSDAY, 'label' => __('Thu')],
['value' => self::FRIDAY, 'label' => __('Fri')],
['value' => self::SATURDAY, 'label' => __('Sat')],
['value' => self::SUNDAY, 'label' => __('Sun')],
];
}
}
VendorModuleetcdi.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="MagentoSalesRuleApiRuleRepositoryInterface">
<plugin name="vendor_module_rule_repository_plugin" type="VendorModulePluginModelRuleRuleRepositoryPlugin"/>
</type>
</config>
VendorModulePluginModelRuleRuleRuleRepositoryPlugin.php
<?php
namespace VendorModulePluginModelRule;
use MagentoSalesRuleApiDataRuleExtensionFactory;
use MagentoSalesRuleApiDataRuleExtensionInterface;
class RuleRepositoryPlugin
{
/**
* Rule Extension Attributes Factory
*
* @var RuleExtensionFactory
*/
protected $extensionFactory;
/**
* RuleExtensionFactory constructor
*
* @param RuleExtensionFactory $extensionFactory
*/
public function __construct(RuleExtensionFactory $extensionFactory)
{
$this->extensionFactory = $extensionFactory;
}
public function afterSave(
MagentoSalesRuleApiRuleRepositoryInterface $subject,
MagentoSalesRuleApiDataRuleInterface $entity
) {
$rule = $entity;
/** Get Current Extension Attributes from Rule
* @var MagentoSalesRuleApiDataRuleExtensionInterface|null
*/
$extensionAttributes = $rule->getExtensionAttributes();
$extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
$applyOnWeekday = $extensionAttributes->getApplyOnWeekday();
var_dump($applyOnWeekday);
if($applyOnWeekday != null) {
if(is_array($applyOnWeekday)) {
$applyOnWeekday = implode(',', $applyOnWeekday);
}
$rule->setData('apply_on_weekday', $applyOnWeekday);
}
return $rule;
}
}
Now we call API for create salesrule
Endpoint :- rest/default/V1/salesRules
Method :- POST
paylod:-
{
"rule": {
"name": "testing the string",
"store_labels": [],
"description": "",
"website_ids": [
1
],
"customer_group_ids": [
1
],
"from_date": "2022-10-31",
"uses_per_customer": 0,
"is_active": true,
"condition": {
"condition_type": "Magento\SalesRule\Model\Rule\Condition\Combine",
"aggregator_type": "all",
"operator": null,
"value": true
},
"action_condition": {
"condition_type": "Magento\SalesRule\Model\Rule\Condition\Product\Combine",
"aggregator_type": "all",
"operator": null,
"value": true
},
"stop_rules_processing": true,
"is_advanced": true,
"sort_order": 0,
"simple_action": "by_percent",
"discount_amount": 0,
"discount_step": 0,
"apply_to_shipping": false,
"times_used": 0,
"is_rss": true,
"coupon_type": "NO_COUPON",
"use_auto_generation": false,
"uses_per_coupon": 0,
"simple_free_shipping": "0",
"extension_attributes": {
"apply_on_weekday": [
"Fri",
"Sat",
"Sun"
]
}
}
}