浏览文章
文章信息
Magento2 新增优惠规则promotion
12244
这里只是简版的。
主要功能:模块新增筛选属性到购物车规则。
模块在附件中,以下只是基础代码演示,需要此功能下载附件模块即可。
system.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <section id="Aiweline_Rule_admin" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1"> <class>separator-top</class> <label>规则属性</label> <tab>Aiweline</tab> <resource>Aiweline_Rule::config</resource> <group id="general" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="0" translate="label"> <attribute type="expanded">1</attribute> <label>一般配置</label> <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0"> <label>模块开启</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> <comment> <![CDATA[<i style='color:red'>*</i><b style='color:gray'>有必要时可以选择模塊启用状态来控制模塊的运行与否.</b>]]></comment> </field> </group> <group id="cart" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="0" translate="label"> <attribute type="expanded">1</attribute> <label>参数配置</label> <field id="attributes" translate="label" type="textarea" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <label>规则属性</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> <comment> <![CDATA[<i style='color:red'>*</i><b style='color:gray'>添加要加入的属性,每个属性以<b style="color:red">转行</b>分割。 <br>示例:base_grand_total=>Grand Total=>numeric <br>base_subtotal_with_discount=>Subtotal (Excl. Tax)=>numeric. <br>提示:第一个=>号后是标签,第二个=>号后是类型,总类型有:numeric,string,select. <br> 谨慎填写,否则会无效。 ]]> </comment> </field> </group> <group id="information" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="99" translate="label"> <attribute type="expanded">1</attribute> <label>Core Information</label> <field id="creator_companey" translate="label" type="label" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Companey</label> </field> <field id="creator_author" translate="label" type="label" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Author</label> </field> <field id="creator_email" translate="label" type="label" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Email</label> </field> <field id="module_name" translate="label" type="label" sortOrder="4" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Module Name</label> </field> <field id="module_version" translate="label" type="label" sortOrder="5" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Module Version</label> </field> <field id="about_info" translate="label" type="label" sortOrder="99" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Copyright Info</label> <comment> <![CDATA[ <strong>Copyright © 2020 <a href="http:://www.aiweline.com" target="_blank">www.aiweline.com</a> / <a href="mailto:1714255949@qq.com">1714255949@qq.com</a></strong><br /> Discover other must have extensions at <a href="http:://www.aiweline.com" target="_blank">www.aiweline.com</a>. Thanks for choosing Aiweline Extensions.<br/><br/> <hr style="border-top: 1px solid #e3e3e3" /> ]]> </comment> </field> </group> </section> </system> </config>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"> <!--根据grand total来计算包邮--> <type name="Magento\SalesRule\Model\Rule\Condition\Address"> <plugin name="Aiweline_Rule::add_custom_promo" type="Aiweline\Rule\Plugin\Model\Rule\Condition\Address" /> </type> </config>Address.php
<?php /** * @Author 秋枫雁飞 * @Email aiweline@qq.com/1714255949@qq.com * @Desc 文件由Aiweline(秋枫雁飞)编写,若有升级需要 * 建议不要随意修改文件源码。 **/ namespace Aiweline\Rule\Plugin\Model\Rule\Condition; use Aiweline\Rule\Helper\Data; class Address { /** * @var Data */ private Data $data; function __construct( Data $data ) { $this->data = $data; } public function afterLoadAttributeOptions(\Magento\SalesRule\Model\Rule\Condition\Address $subject) { if ($this->data->enable() && $attrs = $this->data->getPromotionAttributes()) { $attributes = $subject->getAttributeOption(); foreach ($attrs as $k => &$v) { $v = $v['label']; } $attributes = array_merge($attributes, $attrs); $subject->setAttributeOption($attributes); } return $subject; } public function afterGetInputType(\Magento\SalesRule\Model\Rule\Condition\Address $subject) { if ($attrs = $this->data->getPromotionAttributes()) { $attribute = $subject->getAttribute(); $attrs_type = [ 'base_subtotal' => 'numeric', 'weight' => 'numeric', 'total_qty' => 'numeric', 'shipping_method' => 'select', 'payment_method' => 'select', 'country_id' => 'select', 'region_id' => 'select', ]; foreach ($attrs as &$attr) { $attr = $attr['input_type']; } $attrs_type = array_merge($attrs,$attrs_type); if (isset($attrs_type[$attribute])) { return $attrs_type[$attribute]; } else { return 'string'; } } else { switch ($subject->getAttribute()) { case 'base_subtotal': case 'weight': case 'total_qty': return 'numeric'; case 'shipping_method': case 'payment_method': case 'country_id': case 'region_id': return 'select'; } return 'string'; } } }Data.php
<?php /** * @Author 秋枫雁飞 * @Email aiweline@qq.com/1714255949@qq.com * @Desc 文件由Aiweline(秋枫雁飞)编写,若有升级需要 * 建议不要随意修改文件源码。 **/ namespace Aiweline\Rule\Helper; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Store\Model\ScopeInterface; class Data extends \Aiweline\Core\Helper\Data { /** * @Desc | 添加购物车条件属性base_subtotal_with_discount * @param int|null $store_id * @return array|false|string[] */ function getPromotionAttributes() { $attributes = []; if ($this->enable()) { try { $need_input_types = ['numeric', 'string', 'select']; $attributes_text = $this->getConfig('cart/attributes', ScopeConfigInterface::SCOPE_TYPE_DEFAULT); $attributes = explode("\r\n", $attributes_text); if ($attributes) { foreach ($attributes as $key => $attribute) { $attr = explode('=>', $attribute); if (count($attr) == 3 && in_array(trim($attr[2]), $need_input_types)) { $attributes[trim($attr[0])] = ['label' => __(trim($attr[1])), 'input_type' => trim($attr[2])]; } unset($attributes[$key]); } } } catch (LocalizedException $e) { // 不做处理 } } return $attributes; } }