从 Javascript 访问 Magento 模型

发布于 2024-11-04 08:03:06 字数 1022 浏览 1 评论 0原文

我不确定这是否可行,所以如果不可能,我欢迎任何替代方法来实现这一目标。基本上,我想在用户选择复选框时设置 Mage 结账会话变量。目前,我通过重定向到执行该工作的控制器,然后在保存变量后重定向回结账来实现此功能。这需要一段时间,我觉得应该有更好的方法,但我无法让它发挥作用。

以下是 methods.phtml 中的输入复选框声明:

<input id="p_method_<?php echo $_code ?>" value="1" type="checkbox" name="payment[use_internal_credit]" title="<?php echo $this->htmlEscape($_method->getTitle()) ?>" class="checkbox" 
    onclick="checkout.update({'review': 1}); parent.location='<?php if(Mage::getSingleton('checkout/session')->getUseInternalCredit()): echo Mage::getUrl('customercredit/index/removeCreditUse/redirect/firecheckout'); else: echo Mage::getUrl('customercredit/index/updateCreditPost/redirect/firecheckout'); endif; ?>'" 
    <?php if(Mage::getSingleton('checkout/session')->getUseInternalCredit()): ?> checked="checked"<?php endif; ?> />

这可以工作,但用户界面不太友好,需要 3-5 秒才能返回到结帐页面。有更好的方法来实现这一点吗?我需要做的就是切换 useInternalCredit 会话变量,然后刷新结账审核部分。刷新很容易......单击时访问 Mage 类是一个问题。

I'm not sure if this is possible, so if not, I welcome any alternative ways to accomplish this. Basically, I want to set a Mage checkout session variable when the user selects a checkbox. I currently have this working by redirecting to a controller that does the work and then redirecting back to checkout after the variable has been saved. This takes a while and I feel like there should be a better way but I can't get it to work.

Here is the input checkbox declaration inside methods.phtml:

<input id="p_method_<?php echo $_code ?>" value="1" type="checkbox" name="payment[use_internal_credit]" title="<?php echo $this->htmlEscape($_method->getTitle()) ?>" class="checkbox" 
    onclick="checkout.update({'review': 1}); parent.location='<?php if(Mage::getSingleton('checkout/session')->getUseInternalCredit()): echo Mage::getUrl('customercredit/index/removeCreditUse/redirect/firecheckout'); else: echo Mage::getUrl('customercredit/index/updateCreditPost/redirect/firecheckout'); endif; ?>'" 
    <?php if(Mage::getSingleton('checkout/session')->getUseInternalCredit()): ?> checked="checked"<?php endif; ?> />

This works but it's not very user friendly and takes a good 3-5 seconds to return back to the checkout page. Is there a better way to accomplish this? All I need to do is toggle the useInternalCredit session variable and then refresh the checkout review section. The refresh is easy...it's accessing the Mage class on click that is a problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无力看清 2024-11-11 08:03:10

系统 API 是服务器端的,这意味着客户端代码在没有任何额外请求的情况下不可能访问它们。这是一件好事,否则任何客户都可以在模型级别任意更改。

The system APIs are server-side, which means that client-side code cannot possibly reach them without any extra kind of request. This is a good thing, as otherwise any customer would be able to change thing arbitrarily at the model level.

神经暖 2024-11-11 08:03:10

这是我会做的一种方法,如果它能激励你的话。

  1. 从您提供的代码来看,该复选框似乎出现在付款步骤中。因此,我将在模板文件 (template/checkout/onepage/ payment.phtml) 中添加复选框。
  2. 让观察者监听 controller_action_postdispatch。该观察者的方法将检查完整的操作名称是否为“checkout_onepage_savePaymentMethod”(
  3. 如果是肯定的),它将检查带有键 payment[use_internal_credit] (又名,chekbox 的名称)的帖子参数是否已设置并且值为 1(复选框值,表示该复选框已被客户选中),如果是这样,请在会话变量中添加您想要的任何内容: Mage::getSingleton('core/session')->; setUniqueName($data[' payment[use_internal_credit]']);$data 是对 $Controller->getRequest()->getPost(); 的引用;

您可以让观察者监听付款步骤专门抛出的事件,要找到它,请查看 Alan 的 Storm 解决方案,我只是不知道它是哪个/如果是,我已经习惯了这个。

如果不清楚,请告诉我。

Here's one way I'd do it, if it inspires you.

  1. From the code you provide, it looks like the checkbox appears in the payment step. So I'd add the checkbox in the template file (template/checkout/onepage/payment.phtml).
  2. make an observer to listen to controller_action_postdispatch. the method of this observer would check the full action name is 'checkout_onepage_savePaymentMethod'
  3. in case of afirmative, it'd check if the post paremeter with key payment[use_internal_credit] (aka, the chekbox's name) is set and has value of 1 (the checkbox value, meaning the checkbox has been checked by the customer) and if so add whatever you want to in a session variable: Mage::getSingleton('core/session')->setUniqueName($data['payment[use_internal_credit]']); ($data being a reference to $Controller->getRequest()->getPost();)

You could probably make the observer listen to the event throwed specifically by the payment step, to find it, check out Alan's Storm solution, I just don't know which/if it is and I'm use to this one.

Let me know if it's not clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文