在magento 2中显示自定义客户表单

时间:2018-08-03 05:17:25

标签: magento

任何人都可以告诉我如何在magento 2中创建表单并将数据显示在magento 2面板的管理面板中

谢谢

2 个答案:

答案 0 :(得分:0)

在您的phtml文件中创建表单。

<form class="customer_form" id="customer_form-form" method="post" action="<?php echo $this->getFormAction(); ?>">
<fieldset class="fieldset">
    <div class="field name required">
        <label class="label" for="customer_fname"><span>Firstname</span></label>
        <div class="control">
                    <input required="true" name="customer_fname" id="customer_fname" title="customer_fname" value="" class="input-text" type="text" />
        </div>
    </div>
    <div class="field name required">
        <label class="label" for="customer_lname"><span>Lastname</span></label>
        <div class="control">
                    <input required="true" name="customer_lname" id="customer_lname" title="customer_lname" value="" class="input-text" type="text" />
        </div>
    </div>
    <div class="field name required">
                <label class="label" for="customer_email"><span>Email</span></label>
                <div class="control">
                    <input required="true" name="customer_email" id="customer_email" title="customer_email" value="" type="email" class="input-text" />
                </div>
        </div>

        <div class="field name required">
                <label class="label" for="customer_number"><span>Mobile No.</span></label>
                <div class="control">
                    <input required="true" name="customer_number" id="customer_number" title="customer_number" value="" type="text" class="input-text" />
                </div>
        </div>

        <div class="field name required">
                <label class="label" for="customer_dob"><span>DOB</span></label>
                <div class="control">
                    <input required="true" name="customer_dob" id="customer_dob" title="customer_dob" value="" type="text" class="input-text" />
                </div>
        </div>
    <div class="field name required">
                <label class="label" for="customer_gender"><span>Gender</span></label>
                <div class="control">
                    <input type="radio" name="customer_gender" value="1">Male<br>
                    <input type="radio" name="customer_gender" value="2">Female<br>  
                </div>
    </div>
        <div class="field name required">
                <label class="label" for="customer_country"><span>Country</span></label>
                <div class="control">
                <?php
                    $countryList=$block->getCountries();
                    ?>
                    <div class="field country">
                        <label class="label" for="customer_country" name="customer_country" id="customer_country"><span><?php echo __('Country') ?></span></label>
                        <div class="control">
                            <?php echo $countryList?>
                        </div>
                    </div>

                    </div>
        </div>
        <div class="field name required">
                <label class="label" for="customer_city"><span>City</span></label>
                <div class="control">
                    <input required="true" name="customer_city" id="customer_city" title="customer_city" value="" class="input-text" type="text" />
                </div>
        </div>

        <div class="field name required">
                <label class="label" for="customer_state"><span>State/Province</span></label>
                <div class="control">
                    <input required="true" name="customer_state" id="customer_state" title="customer_state" value="" class="input-text" type="text" />
                </div>
        </div>
        <div class="field name required">
                <label class="label" for="customer_address"><span>Street Address</span></label>
                <div class="control">
                    <input required="true" name="customer_street" id="customer_street" title="customer_street" value="" class="input-text" type="text" />
                </div>
        </div>

        <div class="field name required">
                <label class="label" for="customer_postal"><span>Postal Code</span></label>
                <div class="control">
                    <input required="true" name="customer_postal" id="customer_postal" title="customer_postal" value="" class="input-text" type="text" />
                </div>
        </div>


        <div class="field name required">
                <label class="label" for="customer_company"><span>Company</span></label>
                <div class="control">
                    <input required="true" name="customer_company" id="customer_company" title="customer_company" value="" class="input-text" type="text" />
                </div>
        </div>
        <div class="field name required">
                <label class="label" for="customer_fax"><span>Fax No.</span></label>
                <div class="control">
                    <input required="true" name="customer_fax" id="customer_fax" title="customer_fax" value="" class="input-text" type="text" />
                </div>
        </div>

        <div class="field name required">
                <label class="label" for="customer_designation"><span>Designation</span></label>
                <div class="control">
                    <input required="true" name="customer_designation" id="customer_designation" title="customer_designation" value="" class="input-text" type="text" />
                </div>
        </div>
        <?php echo $block->getChildHtml('form.additional.info'); ?>
</fieldset>
<div class="actions-toolbar">
        <div class="primary">
        <!-- <input  name="submit" id="submit" title="submit" value="" class="input-text" type="button" /> -->
                <button type="submit" title="Submit" class="action submit primary" id="submit_value">
                    <span>Submit</span>
                </button>
        </div>
    </div>

然后在块文件夹中创建其块

<?php

namespace "";

class Customer extends \Magento\Framework\View\Element\Template{


public function __construct(
     \Magento\Backend\Block\Template\Context $context,
     \Magento\Directory\Block\Data $directoryBlock
    array $data = [])
{
    parent::__construct($context, $data);
    $this->_isScopePrivate = true;
    $this->directoryBlock = $directoryBlock;
    $this->CustomerGroup = $CustomerGroup;

}

public function getFormAction()
{

   return //'your controller path where your data will be directed';
}

 }

然后创建您的控制器

<?php
namespace "";

//use Magento\Framework\Controller\ResultFactory;

class Customerdata extends \Magento\Framework\App\Action\Action{

public function __construct(
    \Magento\Framework\App\Action\Context $context,
     \Magento\Framework\Registry $registry,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Customer\Model\AddressFactory $addresses,
    \Magento\Customer\Model\CustomerFactory $customerFactory,
    \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory


)
{
    parent::__construct($context);
    $this->_registry = $registry;
    $this->storeManager = $storeManager;
    $this->addresses = $addresses;
    $this->customerFactory = $customerFactory;    
    $this->resultLayoutFactory = $resultLayoutFactory;    
}

public function execute()
{
    $resultRedirect = $this->resultRedirectFactory->create();
    $mediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); 
    $this->_registry->register('isSecureArea', true);
    $websiteId = $this->storeManager->getWebsite()->getWebsiteId();

    $store = $this->storeManager->getStore();
    $storeId = $store->getStoreId();

    $customer = $this->customerFactory->create();
    $customer->setWebSiteId($websiteId);
    $post = $this->getRequest()->getPost();

    $firstname   = $post['customer_fname'];
    $lastname    = $post['customer_lname'];
    $email       = $post['customer_email'];
    $number      = $post['customer_number'];
    $dob         = $post['customer_dob'];
    $gender      = $post['customer_gender'];
    $country     = $post['country_id'];
    $city        = $post['customer_city'];
    $state       = $post['customer_state'];
    $street      = $post['customer_street'];
    $company     = $post['customer_company'];
    $designation = $post['customer_designation'];
    $postal      = $post['customer_postal'];
    $fax         = $post['customer_fax'];


   try{

    $customer->setEmail($email);
    $customer->setFirstname($firstname);
    $customer->setLastname($lastname);
    $customer->setDob($dob);
    $customer->setGender($gender);       
    $customer->setPassword("admin@123");
    $customer->save();
    echo 'Succesfully Saved'.$customer->getId();

    $address = $this->addresses->create();
    $address->setCustomerId($customer->getId());
    $address->setFirstname($firstname);
    $address->setLastname($lastname);

    $address->setCountryId($country);
    $address->setPostcode($postal);
    $customer->setState($state);
    $address->setCity($city);
    $address->setTelephone($number);
    $address->setFax($fax);
    $address->setCompany($company);
    $address->setStreet($street);
    $address->setIsDefaultBilling('1');
    $address->setIsDefaultShipping('1');
    $address->setSaveInAddressBook('1');
    $address->save();
    $this->messageManager->addSuccess(__('Account created successfully.'));
    return $resultRedirect->setPath('/magento1/index.php/mymodule/index/customerdata');

        }

    catch (\Exception $e){
        echo 'Not Saved';
        $this->messageManager->addExceptionMessage($e, __('Customer with the given email address already exists'));
         return $resultRedirect->setPath('/magento1');

     }
 ////  create a seperate controller for rendering your phtmland add this code in it 
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();


}

}
At last create the layout file and call the block in it and you are good to go.

答案 1 :(得分:0)

您可以使用Custom Form Builder处理此问题。这将有助于创建具有不同字段类型的Web表单,以有效地收集客户信息并将其用于您的营销需求。由于具有拖放式表单构造器,因此可以使用各种类型和元素快速生成任何表单。这将有助于在一个页面上显示多个Web表单,并在提交表单后将商店访问者重定向到任何特定页面。