【问题标题】:How to add a custom field in checkout form in magento 2 with database entry如何使用数据库条目在 magento 2 的结帐表单中添加自定义字段
【发布时间】:2019-12-11 11:27:32
【问题描述】:

我想在结帐表格 magento 2.enter image description here 中的电子邮件后添加一个新的字段工作编号

【问题讨论】:

    标签: magento magento2


    【解决方案1】:

    您可以在自定义模块中创建新的块和模板并将其放入结帐页面。这是一个示例,您可以输入checkout_index_index.xml。您将在新模块中创建此 checkout_index_index.xml

    <?xml version="1.0"?>
      <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceContainer name="checkout.root">
             <block name="job.custom" template="CustomModule::job/job_field.phtml" class="Custom\Module\Block\Job" before="-"></block>
            </referenceContainer>
        </body>
      </page>
    

    根据您要放置字段的位置,您可以相应地使用 before 或 after 标记。

    【讨论】:

      【解决方案2】:

      app/code/Vendor/Module/Setup/UpgradeSchema.php

      <?php
      
      namespace Vendor\Module\Setup;
      
      use Magento\Framework\Setup\UpgradeSchemaInterface;
      use Magento\Framework\Setup\SchemaSetupInterface;
      use Magento\Framework\Setup\ModuleContextInterface;
      
      class UpgradeSchema implements UpgradeSchemaInterface
      {
          /**
           * Upgrades DB schema for a module
           *
           * @param SchemaSetupInterface $setup
           * @param ModuleContextInterface $context
           * @return void
           */
          public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
          {
              $setup->startSetup();
      
              $quoteAddressTable = 'quote';
              $orderTable = 'sales_order';
      
              //Quote address table
              $setup->getConnection()
                  ->addColumn(
                      $setup->getTable($quoteAddressTable),
                      'custom_test',
                      [
                          'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                          'length' => 255,
                          'comment' =>'Custom Test'
                      ]
                  );
              //Order address table
              $setup->getConnection()
                  ->addColumn(
                      $setup->getTable($orderTable),
                      'custom_test',
                      [
                          'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                          'length' => 255,
                          'comment' =>'Custom Test'
      
                      ]
                  );
      
              $setup->endSetup();
          }
      }
      

      将我们的自定义字段添加到结帐页面app/code/Vendor/Module/view/frontend/layout/checkout_index_index.xml

      <?xml version="1.0"?>
      <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
          <body>
              <referenceBlock name="checkout.root">
                  <arguments>
                      <argument name="jsLayout" xsi:type="array">
                          <item name="components" xsi:type="array">
                              <item name="checkout" xsi:type="array">
                                  <item name="children" xsi:type="array">
                                      <item name="steps" xsi:type="array">
                                          <item name="children" xsi:type="array">
                                              <item name="shipping-step" xsi:type="array">
                                                  <item name="children" xsi:type="array">
                                                      <item name="shippingAddress" xsi:type="array">
                                                          <item name="children" xsi:type="array">
                                                              <item name="shipping-address-fieldset" xsi:type="array">
                                                                  <!--Our custom test fieldset here-->
                                                                  <item name="children" xsi:type="array">
                                                                      <item name="customer_test" xsi:type="array">
                                                                          <item name="sortOrder" xsi:type="string">300</item>
                                                                      </item>
                                                                  </item>
                                                              </item>
                                                          </item>
                                                      </item>
                                                  </item>
                                              </item>
                                          </item>
                                      </item>
                                  </item>
                              </item>
                          </item>
                      </argument>
                  </arguments>
              </referenceBlock>
          </body>
      </page>
      

      创建fieldset.xml

      app/code/Vendor/Module/etc/fieldset.xml

      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
          <scope id="global">
              <fieldset id="sales_convert_quote">
                  <field name="custom_test">
                      <aspect name="to_order" />
                  </field>
              </fieldset>
          </scope>
      </config>
      

      别忘了创建registration.php和etc/module.xml

      app/code/Vendor/Module/registration.php

      <?php
      \Magento\Framework\Component\ComponentRegistrar::register(
          \Magento\Framework\Component\ComponentRegistrar::MODULE,
          'Vendor_Module',
          __DIR__
      );
      

      app/code/Vendor/Module/etc/module.xml

      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
          <module name="Vendor_Module" setup_version="1.0.0"/>
      </config>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-29
        • 2012-04-28
        • 1970-01-01
        • 2020-09-09
        • 2018-08-07
        • 2013-02-19
        • 1970-01-01
        相关资源
        最近更新 更多