check below Code.
Reference: \Magento\CatalogRule\Controller\Adminhtml\Promo\Catalog\Save
Before saving rule validate data It's helping for saving Correct value
$model = $objectManager->create('Magento\CatalogRule\Model\Rule');
$model->setName('name')
->setDescription('description')
->setIsActive(1)
->setCustomerGroupIds(array(1))
->setWebsiteIds(array(1))
->setFromDate('')
->setToDate('')
->setSimpleAction('by_fixed')
->setDiscountAmount(10)
->setStopRulesProcessing(0);
$conditions = array();
$conditions["1"] = array
(
"type" => "Magento\CatalogRule\Model\Rule\Condition\Combine",
"aggregator" => "all",
"value" => 1,
"new_child" => ""
);
$conditions["1--1"] = array
(
"type" => "Magento\CatalogRule\Model\Rule\Condition\Product",
"attribute" => "sku",
"operator" => "==",
"value" => "24-UB02"
);
$model->setData('conditions',$conditions);
// Validating rule data before Saving
$validateResult = $model->validateData(new \Magento\Framework\DataObject($model->getData()));
if ($validateResult !== true) {
foreach ($validateResult as $errorMessage) {
echo $errorMessage;
}
return;
}
try {
$model->loadPost($model->getData());
$model->save();
$ruleJob = $objectManager->get('Magento\CatalogRule\Model\Rule\Job');
$ruleJob->applyAll();
echo "rule created";
} catch (Exception $e) {
echo $e->getMessage();
}
Try this,
Use checkout_cart_product_add_after
Create a module and add events.xml in following path
app/code/Vendor/Module/etc/events.xml
Add the below code in it
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="setprice" instance="Vendor\ModuleName\Observer\SetPrice" />
</event>
</config>
then add observer in the below path
Vendor\ModuleName\Observer\SetPrice.php
add below code in it
<?php
namespace Vendor\ModuleName\Observer;
class SetPrice implements \Magento\Framework\Event\ObserverInterface
{
protected $_request;
public function __construct(
\Magento\Framework\App\RequestInterface $request,
array $data = []
) {
$this->_request = $request;
}
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$postdata = $this->_request->getPost();
$priceoption = $postdata['input_name']; // say it select input field which you have
$item = $observer->getEvent()->getData('quote_item');
$item = ($item->getParentItem() ? $item->getParentItem() : $item);
$price = $priceoption; //set your price here
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
}}
$postdata['input_name']; this must be you select input name which you have in the form and in addition to above answer move your input fields to add to cart form fields.
To move your custom form inputs to add to cart form in below way.
Add the catalog_product_view.xml then add the below code in it
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info">
<action method="setTemplate">
<argument name="template" xsi:type="string">Vendor_ModuleName::product/view/form.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
then copy the form.phtml from vendor in order to override and move your input fields to there as per your requirements.
vendor/magento/module-catalog/view/frontend/templates/product/view/form.phtml
To
app/code/Vendor/ModuleName/view/frontend/templates/product/view/form.phtml
Hope this helps :)
Best Answer
You can use as example the standard Magento class
Magento\Rule\Model\AbstractModel
or just extend it in your custom class. The algorithm is pretty simple:1) Load conditions data (
$conditionsSerialized = $this->getConditionsSerialized())
) and unserialize it ($conditions = $this->serializer->unserialize($conditionsSerialized);
)2) Get the conditions object (instance of
\Magento\Rule\Model\Condition\Combine
or what ever you use) and load it using recently loaded data as array ($this->_conditions->loadArray($conditions);
)3) Validate your object using this condition (
$this->_conditions->validate($object);
)