简体   繁体   中英

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. 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:

  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.
  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.

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

All configurable products need to be added to cart with a specific option, for the simple ones you only need quantity and 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. 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.

Maybe you can find an approach better suited to your needs, but here is what I'd do:

Create a custom controller in Magento

Alan Storm has some great Magento tutorials, check out this one for controllers. Extend the default product controller. This is the controller you will visit form a Drupal installation (maybe called in an 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). 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.

If you still want to use your existing solution, take a look at the layout documentation from 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.

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). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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