pendalff/magento2-checkout-custom-form

在 Magento 2 结算的第一步添加自定义表单。

安装: 13

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 1

分支: 63

类型:magento2-module

2.0.5 2020-05-20 08:18 UTC

README

概览

将自定义表单字段添加到 Magento 2 结算的第一步。表单将在第一步(送货步骤)中显示,位于送货方式之上。表单适用于登录客户和访客。订单提交后,所有数据都设置在 sales_order 表中。页面刷新后,数据仍保留在表单中,直到购物车处于活动状态。

通过独立的 API 请求将表单数据设置在 quota 表中

  • /V1/carts/mine/set-order-custom-fields(登录客户)
  • /V1/guest-carts/:cartId/set-order-custom-field(访客)

兼容性

  • 标签 1.2.* => Magento 2.3
  • 标签 1.1.* => Magento 2.1.x - 2.2.x(不再支持)

要求

  • PHP 7.0 或更高版本

安装细节

  1. 运行 composer require sbodak/magento2-checkout-custom-form
  2. 运行 php bin/magento module:enable Bodak_CheckoutCustomForm
  3. 运行 php bin/magento setup:upgrade

示例自定义表单字段

  • 买家
  • 买家电子邮件地址
  • 采购订单号
  • 商品标记
  • 评论

修改表单字段

  • 您需要修改 Api/Data/CustomFieldsInterfaces.php 中的服务合同数据接口
  • 您需要修改 Setup/InstallData.php 中的表结构
  • 您需要向观察者 Observer/AddCustomFieldsToOrder.php 添加新字段,以保存数据到 quota 和 sales 表
  • 您需要向 view/frontend/layout/checkout_index_index.xml 添加新项
  • 您需要修改 Model/Data/CustomFields.php 中的方法
  • 您需要修改 Model/CustomFieldsRepository.php 中的方法
<item name="custom-checkout-form-fieldset" xsi:type="array">
    <item name="component" xsi:type="string">uiComponent</item>
    <item name="displayArea" xsi:type="string">custom-checkout-form-fields</item>
    <item name="children" xsi:type="array">
        [... place here new definition of your field]
    </item>
</item>

查看官方文档:https://devdocs.magento.com/guides/v2.3/howdoi/checkout/checkout_form.html

  • 您需要修改模板视图 view/frontend/templates/order/view/custom_fields.phtml(客户账户)和 view/adminhtml/templates/order/view/custom_fields.phtml(管理面板)。
  • 结算表单视图自动使用 Ui Components 生成

必填项

如果您想使字段必填,请参考此示例

<item name="checkout_purchase_order_no" xsi:type="array">
    <item name="component" xsi:type="string">Magento_Ui/js/form/element/abstract</item>
    <item name="config" xsi:type="array">
        <item name="customScope" xsi:type="string">customCheckoutForm</item>
        <item name="template" xsi:type="string">ui/form/field</item>
        <item name="elementTmpl" xsi:type="string">ui/form/element/input</item>
    </item>
    <item name="validation" xsi:type="array">
        <item name="required-entry" xsi:type="boolean">true</item>
    </item>
    <item name="provider" xsi:type="string">checkoutProvider</item>
    <item name="dataScope" xsi:type="string">customCheckoutForm.checkout_purchase_order_no</item>
    <item name="label" xsi:type="string">Purchase order no.</item>
    <item name="sortOrder" xsi:type="string">3</item>
</item>

快速字段覆盖

您可以通过修改 i18n/en_US.csv 翻译来更改字段名称。

结算视图 - 自定义表单 - 访客

Checkout frontend custom form - Guest

结算视图 - 自定义表单 - 登录

Checkout frontend custom form - Logged in

自定义账户 - 订单历史视图

Customer account - Order history view

管理面板 - 订单编辑

Admin panel - order edit

卸载

要删除此模块,请运行 php bin/magento module:uninstall Bodak_CheckoutCustomForm。这将删除所有数据并在 sales_orderquote 表中删除列。

许可证

MIT 许可证