I'm trying to display parts of the "Product View" page outside 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)".
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. 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.
Thank you!
Edit:
After a bit more digging, a few more points:
- Moving the renderView() calls below each block (instead of having them clumped together) fixes the "Add to cart replacing the main info block" issue.
- 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.
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. Further, if I use Firebug to cut and paste the configurable product fields as a child of the submitting <form>
element, everything works beautifully. 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?
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'];