系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
很多时候,在线销售产品时,您必须仅在有限的区域启用运输服务。由于较高的运输/出口率,禁止的产品,严格的海关法律或严格的现金处理,所有这些都导致管理员将订单运输限制在有限的区域。幸运的是,使用Magento 2,您可以创建购物车规则,允许您检查动态条件
但是,如果要创建自定义购物车规则以触发特定代码以响应该事件。要执行相同的操作,您必须创建自定义扩展以满足您的业务需求,因为我们从未建议修改核心文件。让我们以设置购物车页面的城市字段的自定义销售规则为例。您还可以为不同的事件创建自定义购物车规则。
首先要做同样的事情,我们需要在 etc 文件夹中创建“events.xml”文件,并在该文件中添加粘贴下面的代码。
app\code\Vendor\Extension\etc\events.xml
<?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="salesrule_rule_condition_combine"> <observer name="customer_rule" instance="Vendor\Extension\Observer\Shippingcityconditionobserver" /> </event> </config>
现在,我们在观察者文件夹中创建“Shippingcityconditionobserver.php”。
app\code\Vendor\Extension\Observer\Shippingcityconditionobserver.php
<?php
namespace Vendor\Extension\Observer;
class Shippingcityconditionobserver implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$additional = $observer->getAdditional();
$conditions = (array) $additional->getConditions();
$conditions = array_merge_recursive($conditions, [
$this->getCustomerFirstOrderCondition()
]);
$additional->setConditions($conditions);
return $this;
}
private function getCustomerFirstOrderCondition()
{
return [
'label'=> __('Shipping city'),
'value'=> \Vendor\Extension\Model\Rule\Condition\Shippingcity::class
];
}
}在以下路径再创建一个文件“di.xml”文件。
app\code\Vendor\Extension\etc\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="Vendor\Extension\Model\Rule\Condition\Shippingcity"> <arguments> <argument name="data" xsi:type="array"> <item name="form_name" xsi:type="string">sales_rule_form</item> </argument> </arguments> </type> </config>
最后,在此位置再创建一个名为“Shippingcity.php”的文件。
app\code\Vendor\Extension\Model\Rule\Condition\Shippingcity.php
<?php
namespace Vendor\Extension\Model\Rule\Condition;
class Shippingcity extends \Magento\Rule\Model\Condition\AbstractCondition
{
protected $_checkoutSession;
public function __construct(
\Magento\Rule\Model\Condition\Context $context,
\Magento\Checkout\Model\Session $checkoutSession,
array $data = []
) {
parent::__construct($context, $data);
$this->_checkoutSession = $checkoutSession;
}
public function loadAttributeOptions()
{
$this->setAttributeOption([
'shipping_city' => __('Shipping city')
]);
return $this;
}
public function getInputType()
{
return 'select'; // input type for admin condition
}
public function getValueElementType()
{
return 'text';
}
public function validate(\Magento\Framework\Model\AbstractModel $model)
{
$city = $this->_checkoutSession->getQuote()->getShippingAddress()->getCity();
$model->setData('shipping_city', $city); // validation value
return parent::validate($model);
}
}只需清除缓存,您将在管理后端找到一个选项。您还可以使用上面的代码来创建不同类型的自定义运输规则。