简体   繁体   English

当产品视图块在Magento之外呈现时,无法将可配置产品添加到购物车

[英]Cannot add configurable products to cart when Product View blocks rendered outside of Magento

I'm trying to display parts of the "Product View" page outside Magento. 我正在尝试在Magento外部显示“产品视图”页面的部分内容。 I'm able to get everything to show up properly and all the Javascript to load -- however, whenever I click the Add To Cart button, I'm given a message saying "Please specify the product's option(s)". 我能够正确显示所有内容以及要加载的所有Javascript - 但是,每当我单击“添加到购物车”按钮时,我都会收到一条消息“请指定产品的选项”。

As noted in my comments, if I change 如我的评论中所述,如果我改变了

 
 
 
 
  
  
  $addtocartBlock->createBlock()
 
 
  

to

 
 
 
 
  
  
  $addtocartBlock->getBlockSingleton()
 
 
  

the entire top portion is replaced by the Add To Cart block. 整个顶部被Add To Cart块替换。 See edit. 见编辑。

Any thoughts? 有什么想法吗?

I get the feeling that the Add to Cart button isn't working properly because it's not explicitly hooked up to the other blocks, though I might be wrong. 我觉得添加到购物车按钮不能正常工作,因为它没有明确地连接到其他块,但我可能是错的。

Alternatively, what would also be super helpful are some general guidelines in rendering these blocks programmatically -- while I'm fairly adept at PHP, Magento just loses me and I'm often just cutting and pasting random snippets from the Magento forum . 或者,对于以编程方式呈现这些块的一些一般指导原则也是非常有用的 - 虽然我非常擅长PHP,Magento只是失去了我而且我经常只是从Magento论坛剪切和粘贴随机片段

Thank you! 谢谢!


Edit: 编辑:

After a bit more digging, a few more points: 经过多一点挖掘,还有几点:

  1. Moving the renderView() calls below each block (instead of having them clumped together) fixes the "Add to cart replacing the main info block" issue. 在每个块下面移动renderView()调用(而不是将它们聚集在一起)修复了“添加到购物车替换主信息块”问题。
  2. Simple products are able to be added without issue. 简单的产品可以毫无问题地添加。 The only problem I'm having is making Magento recognize the product options submitted for configurable products. 我遇到的唯一问题是让Magento认可为可配置产品提交的产品选项。

MOAR EDITZ!!!!!1111! MOAR EDITZ !!!!! 1111!

Further pursuant to this Question That Just Won't Die, I've discovered that @moldovan-gheorghe-daniel's correct about the "super_attribute" array not being sent with the rest of the POST. 根据这个“不会死的问题”,我发现@mouldovan-gheorghe-daniel关于“super_attribute”数组没有与POST的其余部分一起发送是正确的。 Further, if I use Firebug to cut and paste the configurable product fields as a child of the submitting <form> element, everything works beautifully. 此外,如果我使用Firebug将可配置产品字段剪切并粘贴为提交<form>元素的子元素,那么一切都很有效。 To finally cut to the chase: 最终切入追逐:

tl;dr -- HOW DO I LOAD THE CONFIGURABLE PRODUCT ATTRIBUTES BLOCK AS A CHILD OF THE ADD TO CART BLOCK? tl; dr - 我如何加载可配置的产品属性作为添加到CART BLOCK的孩子?

whew! 噢!

Here's my code: 这是我的代码:

 <?php //Pretty standard loading Magento stuff. $bootstrap = $_SERVER['DOCUMENT_ROOT'] . '/magento/app/Mage.php'; require_once $bootstrap; session_name ( 'frontend' ); Mage::getSingleton ( 'core/session', array ('name' => 'frontend' ) ); $app = Mage::app('default'); $app->getTranslator()->init('frontend'); umask(0); session_name('frontend'); Mage::getSingleton('customer/session'); //I'm not sure I need this. $_product = Mage::getModel('catalog/product'); $_product->load($product_id); Mage::unregister('product'); Mage::register('product', $_product); //The following loads the main Mage_Catalog_Block_Product_View block. $linksBlock = $app->getLayout()->getBlockSingleton("catalog/product_view"); $linksBlock->setProduct($_product)->setTemplate('catalog/product/view.phtml'); //The following loads the configurable product attributes block. $checkoutLinksBlock = $app->getLayout() ->getBlockSingleton("catalog/product_view_type_configurable") ->setTemplate('catalog/product/view/type/options/configurable.phtml'); $checkoutLinksBlock->setParentBlock($linksBlock); /* The following loads the Add To Cart block. If I use getBlockSingleton() instead * of createBlock(), this replaces the entire top block. */ $addtocartBlock = $app->getLayout() ->createBlock("catalog/product_view") ->setTemplate('catalog/product/view/addtocart.phtml'); $addtocartBlock->setParentBlock($linksBlock); $blocks['info'] = $linksBlock->renderView(); $blocks['addtocart'] = $addtocartBlock->renderview(); if ($_product->getTypeId() == 'configurable') $blocks['config'] = $checkoutLinksBlock->renderView(); else $blocks['config'] = ''; Mage::unregister('product'); // ...And output everything here. echo $blocks['info'] . $blocks['config'] . $blocks['addtocart']; 

All configurable products need to be added to cart with a specific option, for the simple ones you only need quantity and id. 所有可配置产品都需要通过特定选项添加到购物车中,对于简单的产品,您只需要数量和ID。 This is how should look array of request when a configurable product is added to cart. 这是在将可配置产品添加到购物车时应该查看请求数组的方式。

Array(
    [uenc] => aHR0cdsfsdfdsfdssssssssssssss
    [product] => 4816
    [qty] => 2
    [related_product] =>
    [super_attribute] => Array(
            [352] => 1093
        )
) 

"super_attribute" contains what option user selected. “super_attribute”包含用户选择的选项。 So i suggest to check if that data are in browser request. 所以我建议检查这些数据是否在浏览器请求中。 Maybe you post data without js validation made properly, and user not select anything from configurable options available, or maybe configurable options dropdown is not rendered at all. 也许您在没有正确验证js验证的情况下发布数据,并且用户不从可用的可配置选项中选择任何内容,或者根本不呈现可配置选项下拉列表。

Maybe you can find an approach better suited to your needs, but here is what I'd do: 也许你可以找到一种更适合你需求的方法,但这就是我要做的:

Create a custom controller in Magento 在Magento中创建自定义控制器

Alan Storm has some great Magento tutorials, check out this one for controllers. 艾伦风暴有一些伟大的Magento的教程,看看这其中的控制器。 Extend the default product controller. 扩展默认产品控制器。 This is the controller you will visit form a Drupal installation (maybe called in an iframe?). 这是您将从Drupal安装访问的控制器(可能在iframe中调用?)。

Create a custom layout 创建自定义布局

Start here - you'll be able to get a feel for how layouts work. 这里开始 - 您将能够了解布局的工作原理。 Take a look at how the product page is rendered (check the layout XML files, as well as the .phtml templates). 看一下产品页面的呈现方式(检查布局XML文件以及.phtml模板)。 I'd have a think about what happens when an item is added to a cart too. 我想一想将物品添加到购物车时会发生什么。

With the two above, you should have a product page with working functionality, and the ability to customise the page layout and style to work with your current site. 有了上述两个,您应该拥有一个具有工作功能的产品页面,并能够自定义页面布局和样式以与当前站点一起使用。 It's not a true bridge per se. 它本身并不是真正的桥梁。 If that is what you are after, take a look at Magento's API. 如果这就是您所追求的,请查看Magento的API。

If you still want to use your existing solution, take a look at the layout documentation from Magento. 如果您仍想使用现有解决方案,请查看Magento的布局文档。 In the .phtml for the product view page, you will see what HTML is generated - and what HTML you need to generate - to mimic the add to cart form. 在产品视图页面的.phtml中,您将看到生成的HTML - 以及您需要生成的HTML - 以模拟添加到购物车表单。

EDIT Not sure why the downvotes without comments (if my answer is not to your liking, let me know why and I'll improve it). 编辑不知道为什么没有评论的downvotes(如果我的答案不符合你的喜好,让我知道为什么,我会改进它)。 Magento is a framework, and whilst the accepted solution might work, it is not the 'Magento' way: future developers (including onself) will likely have a difficult time maintaining the provided solution. Magento是一个框架,虽然接受的解决方案可能有效,但它不是“Magento”方式:未来的开发人员(包括自己)可能很难维护提供的解决方案。 The extra effort in learning how Magento works (and incorporating it) is worth the effort - after all, your client is paying you to fix their problem. 学习Magento如何工作(以及合并它)的额外努力是值得的 - 毕竟,您的客户正在付钱给您解决他们的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM