有谁知道是否可以像最初的Magento 1结帐一样将客户注册步骤移至结帐处?寻找可以为我完成此任务的扩展程序或有关如何实现此目标的建议。
有谁知道是否可以像最初的Magento 1结帐一样将客户注册步骤移至结帐处?寻找可以为我完成此任务的扩展程序或有关如何实现此目标的建议。
Answers:
我将通过在结帐中创建一个步骤来检测您登录的天气,然后相应地显示表单。
将注册表单加载到Ajax上的此步骤中,并修改注册后事件,以检测是否进行了注册,然后将其重定向回检出而不是仪表板。
我可以添加一个工作步骤来显示未登录注册的工作步骤,并将注册成功重定向回结帐功能,从而禁用上一步操作以获得流畅的用户体验。
如果用户拥有一个帐户但在此阶段尚未登录,则提供一个登录选项将在此处进一步改善。
如果您想检查所有的答案,那么这是完整的代码:
在控制器内创建注册表:
由于需要执行一些结帐步骤php
来获取表单url和密钥等,因此需要一个控制器,因此我们可以通过ajax将该寄存器表单加载到该步骤中。
<?php
namespace Harrigo\RegisterCheckout\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Register extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
/**
* Constructor
*
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
$this->resultPageFactory = $resultPageFactory;
$this->_resultFactory = $context->getResultFactory();
parent::__construct($context);
}
/**
* Execute view action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
//if (isset($_POST["cart"])) {
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
return $resultLayout;
//}
//$this->_redirect('checkout/');
}
}
控制器/索引/Register.php
下面在控制器内渲染了注册块,因此我们可以通过ajax调用检出。
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<container name="root" label="Root">
<block class="Magento\Framework\View\Element\Js\Components" name="customer_account_create_head_components" template="Magento_Customer::js/components.phtml"/>
<block class="Magento\Customer\Block\Form\Register" name="customer_form_register" template="Harrigo_RegisterCheckout::register.phtml">
<container name="form.additional.info" as="form_additional_info"/>
<container name="customer.form.register.fields.before" as="form_fields_before" label="Form Fields Before" htmlTag="div" htmlClass="customer-form-before"/>
</block>
<block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
<arguments>
<argument name="triggers" xsi:type="array">
<item name="registerSubmitButton" xsi:type="string">.action.submit</item>
</argument>
</arguments>
</block>
<block class="Magento\Framework\View\Element\Template" name="form_additional_info_customer" template="Magento_Customer::additionalinfocustomer.phtml"/>
</container>
</layout>
/view/frontend/layout/harrigoregister_index_register.xml
确保在模块的etc / frontend文件夹中添加routes.xml。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="harrigoregister" frontName="harrigoregister">
<module name="Harrigo_RegisterCheckout" />
</route>
</router>
</config>
创建一个结帐步骤:
http://devdocs.magento.com/guides/v2.0/howdoi/checkout/checkout_new_step.html
由于这是第一步,因此请确保像我在模块中所做的一样添加mixins,因为devdocs在此处不正确,从而导致显示每个步骤:
<!--The 'step_code' value from the .js file should be used-->
<li id="registerstep" data-bind="fadeVisible: isVisible">
<div class="step-title" data-bind="i18n: 'Register'" data-role="title"></div>
<div id="checkout-step-title"
class="step-content"
data-role="content">
<form data-bind="submit: navigateToNextStep" novalidate="novalidate">
<div class="actions-toolbar" id="shipping-method-buttons-container">
<div class="primary">
<button data-role="opc-continue" type="submit" class="button action continue primary">
<span><!-- ko i18n: 'Continue as guest'--><!-- /ko --></span>
</button>
</div>
</div>
</form>
</div>
<div id="registerblock"></div>
</li>
如果在ajax调用之前未加载块,则失败了,但是最终使用了类似的方法从控制器获取我们的注册表单并将其吐到步骤上:
//waits for elements to load in checkout
function waitForElement(elementPath, callBack){
window.setTimeout(function(){
if($(elementPath).length){
callBack(elementPath, $(elementPath));
}else{
waitForElement(elementPath, callBack);
}
},500)
}
//get crosssell products / newsletter
$.ajax({
url: "/harrigoregister/index/register",
type: "post",
data: {
cart: "yes"
},
success: function(response) {
waitForElement("#registerblock",function(){
$("#registerblock").html(response);
});
},
error: function(xhr) {
}
});
仅在遵循devdocs并确保执行mixin步骤之后,这里还有很多其他步骤,但由于devdocs示例不起作用,因此在下面用于mixin:
define(
[
'ko',
'Magento_Customer/js/model/customer'
], function (ko, customer) {
'use strict';
var mixin = {
initialize: function () {
if(!customer.isLoggedIn()) {
this.isVisible = ko.observable(false);
this.visible = ko.observable(false); // set visible to be initially false to have your step show first
}
this._super();
return this;
}
};
return function (target) {
return target.extend(mixin);
};
}
);
主要区别在于付款和运输步骤分别使用isVisible和Visible,而devdocs仅在示例内使用visible,因此需要添加isVisible进行修复。还必须对是否禁用该步骤进行调整。
修改注册重定向:
<?php
namespace Harrigo\RegisterCheckout\Plugin;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Registry;
use Magento\Framework\UrlInterface;
class Redirect
{
protected $coreRegistry;
protected $url;
protected $resultFactory;
public function __construct(Registry $registry, UrlInterface $url, ResultFactory $resultFactory)
{
$this->coreRegistry = $registry;
$this->url = $url;
$this->resultFactory = $resultFactory;
}
public function aroundGetRedirect ($subject, \Closure $proceed)
{
//need to check out if registration was from checkouit
/** @var \Magento\Framework\Controller\Result\Redirect $result */
if ($_POST['checkout'] = 'true') {
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl($this->url->getUrl('checkout'));
return $result;
}
return $proceed();
}
}
粗略的示例,但还必须重写register.phtml以添加checkout post变量以确定两种形式之间的差异。在这里,我没有显示很多步骤,但是请查看我为完整示例创建的模块。