向OPC添加步骤而不会覆盖 [英] add step to OPC without overriding

查看:87
本文介绍了向OPC添加步骤而不会覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 tweet 之后/users/465971/ivan-chepurnyi> Ivan Chepurnyi ,并且不得不向客户站点的OnePage Checkout(又名OPC)添加一个步骤,我觉得这是学习新知识的绝佳时机.
我是事件/观察者的忠实拥护者,并尝试尽可能多地使用它们,但是就目前的OPC而言,我发现使用它们并不是很优雅.我的意思是,据我所知,没有有用的事件可以使用.

after reading this tweet from Ivan Chepurnyi and having to add a step to the OnePage Checkout (aka OPC) for a client's site, I thaught it was the perfect occasion to learn something new.
I am a big fan of events/observers and try to use them as much as possible, but precisely for the OPC so far I found that it was not very elegant to use them. I mean, there are no useful events (as far as I know) that can be used.

例如,从我的头顶上,我想到了我必须更改的两件事,并且通过重写非常容易,而对于事件/观察者来说则过于复杂了:

For example, from the top of my head I think of 2 things that I have to change and are very easy with a rewrite while it would be over complicated with event/observer:

  1. 步骤:Mage_Checkout_Block_Onepage::getSteps()定义组成OPC的不同步骤.我真的看不到有什么事件能让我改变这一点
  2. 转到:完成一个步骤后,Mage_Checkout_OnepageController的相应操作将返回用于下一步的html以及goto_sectionupdate_section数据.再说一次,我看不到任何有趣的事件
  1. steps: Mage_Checkout_Block_Onepage::getSteps() defines the different steps that composes the OPC. I really can't see what event would allow me to change that
  2. goto: when one step is completed the Mage_Checkout_OnepageController's corresponding action returns the html for next step along with the goto_section and update_section data. Again, I don't see any interesting event

这只是没有真正研究我必须实现的每个细节,我想还有很多陷阱.
javascript部分也是如此:我习惯于扩展checkout js类:

And this is just without really looking into every detail I'll have to implement, I guess there are more pitfalls.
Same thing for the javascript part: I am used to extend the checkout js class:

var MyModule = Class.create(Checkout, {
    //write some code here
});

但我想知道是否有更好的方法.

but I wonder if there is a better way.

我意识到Ivan的推文是为PSP开发人员设计的,并且我正在为最终客户服务,但是我想学习新的东西

I realize that Ivan's tweet is meant for PSP devs, and that I am working for a final client, but I'd like to learn something new

推荐答案

此推文的主要思想是,大多数开发自定义付款方式的开发人员都不关心与您网站上其他模块或自定义设置之间的冲突.

Main idea of the tweet was about most of the developers who develop custom payment methods don't care about conflicts with other modules or customizations you have on the website.

对于您的第一步,这是Magento核心的问题,它具有_getSteps()方法中的所有结帐步骤都是硬编码的,您不能在没有覆盖的情况下更改它.

As for your first step, it is a problem of Magento core, that it has all checkout steps in _getSteps() method are hardcoded and you cannot change it without override.

即使仅使用controller_action_predispatchcontroller_action_postdispatch动作,第二个也很容易实现.

The second one is quite easy to implement even with just using of controller_action_predispatch and controller_action_postdispatch actions.

preDispatch上,Magento允许您使用FLAG_NO_DISPATCHFLAG_NO_PRE_DISPATCHFLAG_NO_POST_DISPATCH等控制器标志来自由控制控制器动作的流程.

On preDispatch Magento gives you a lot of freedom to control the flow of controller action by using controller flags like FLAG_NO_DISPATCH, FLAG_NO_PRE_DISPATCH and FLAG_NO_POST_DISPATCH.

postDispatch上,您可以通过修改响应对象的主体来修改现有结果.如果返回JSON,则可以轻松地将此数据转换回数组,进行修改并将其设置回JSON.

On postDispatch you can modify existent result by modifying body of the response object. In case of returned JSON, you can easily transform this data back to array, modify it and set back as JSON.

是的,该解决方案的实现可能会更加复杂,但是它可以大大提高您与其他模块的兼容性,因为您可以放心地使用其他模块覆盖同一控制器.

Yes this solution might more complex to implement, but it increases your module compatibility with the others a lot, since you are safe with possible overrides of the same controller by other module.

对于JavaScript,您应该始终认为它是动态语言,并且在语言级别上本身没有类,所有东西都是一个对象,并且您始终可以通过使用分配给自己的实现替换现有的对象原型来扩展现有的功能.变量:

As for JavaScript, you should always consider that it is dynamic language and there is no class itself on language level, everything is an object and you are always can extend existing functionality via replacing existing object prototype with own implementation by using assignment of variables:

window.Checkout = Class.create(Checkout, {
      someMethod: ($super, param1, param2) {
          // Do you custom staff before
          $super(param1, param2);
          // Do you custom staff after
      }
});

在下面的示例Checkout类"原型中,您的实例被替换,该实例自定义其someMethod功能. $super变量包含父方法"的Function对象,该对象将被覆盖,并且可以与另一种方法(甚至同一方法)的同一类进行相同类型的自定义工作,因为它仅修改了包含该方法定义.

In the following example prototype of Checkout "class" is gets replaced by your instance, that customizes its someMethod functionality. $super variable contains Function object of parent "method", that gets overwritten and it will work with the same kind of customization to the same class of another method (or even the same one), because it just modifies only property that contains that method definition.

如果您想了解有关此JS语言功能的更多信息,请访问以下网站: http ://www.crockford.com/javascript/javascript.html

If you'd like to read more about this JS language functions please refer to this website: http://www.crockford.com/javascript/javascript.html

这篇关于向OPC添加步骤而不会覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆