首页 文章

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

提问于
浏览
3

我正在尝试在Magento外部显示“产品视图”页面的部分内容 . 我能够正确显示所有内容以及要加载的所有Javascript - 但是,每当我单击“添加到购物车”按钮时,我都会收到一条消息“请指定产品的选项” .

如我的评论中所述,如果我改变了
$ addtocartBlock-> createBlock()
to $ addtocartBlock-> getBlockSingleton()
整个顶部被Add To Cart块替换 . 见编辑 .

有什么想法吗?

我感觉 Add to Cart button 并没有明确地连接到其他块,尽管我可能错了 .

或者,对于以编程方式渲染这些块的一些一般指导原则而言,这也是非常有用的 - 而我通常只是剪切和粘贴random snippets from the Magento forum .

谢谢!


编辑:

经过多一点挖掘,还有几点:

  • 在每个块下面移动renderView()调用(而不是让它们聚集在一起)修复了"Add to cart replacing the main info block"问题 .

  • 简单的产品可以毫无问题地添加 . 我遇到的唯一问题是让Magento认可为可配置产品提交的产品选项 .

MOAR EDITZ !!!!! 1111!

根据这个刚刚赢得的问题't Die, I'已经发现,@ moldovan-gheorghe-daniel关于"super_attribute"阵列没有与POST的其余部分一起发送是正确的 . 此外,如果我使用Firebug剪切并粘贴可配置产品字段作为提交 <form> 元素的子元素,那么一切都很好用 . 最终切入追逐:

tl; dr - 我如何加载可配置的产品属性作为添加到CART BLOCK的孩子?

噢!

这是我的代码:

<?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'];

2 回答

  • 0

    所有可配置产品都需要通过特定选项添加到购物车中,对于简单的产品,您只需要数量和ID . 这是在将可配置产品添加到购物车时应该查看请求数组的方式 .

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

    “super_attribute”包含用户选择的选项 . 所以我建议检查这些数据是否在浏览器请求中 . 也许您在没有正确验证js验证的情况下发布数据,并且用户不从可用的可配置选项中选择任何内容,或者根本不呈现可配置选项下拉列表 .

  • 1

    也许你可以找到一种更适合你需求的方法,但这就是我要做的:

    Create a custom controller in Magento

    Alan Storm有一些很棒的Magento教程,请查看控制器的this one . 扩展默认产品控制器 . 这是您将从Drupal安装访问的控制器(可能在iframe中调用?) .

    Create a custom layout

    开始here - 你'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). I' d想一想将物品添加到购物车时会发生什么 .

    有了上述两个,您应该拥有一个具有工作功能的产品页面,并能够自定义页面布局和样式以与当前站点一起使用 . 它本身并不是真正的桥梁 . 如果这就是您所追求的,请查看Magento的API .

    如果您仍想使用现有解决方案,请查看Magento的布局文档 . 在产品视图页面的.phtml中,您将看到生成的HTML - 以及您需要生成的HTML - 以模拟添加到购物车表单 .

    EDIT 不确定为什么没有评论的downvotes(如果我的答案不符合你的喜好,请告诉我为什么和我'll improve it). Magento is a framework, and whilst the accepted solution might work, it is not the ' Magento的方式:未来的开发人员(包括他自己)可能很难维持提供的解决方案 . 了解Magento的工作原理(以及合并它)是值得的 - 毕竟,您的客户正在付钱给您解决问题 .

相关问题