Archive for September, 2011
Getting Configurable Product from Simple Product ID in Magento 1.5+
by Dan on Sep.19, 2011, under Magento, PHP
I recently stumbled across a hurdle which stopped some of my code from working. It was code that gets the configurable product associated with a given simple product. Many solutions out there call a “loadParentProductIds()” function within the Mage_Catalog_Model_Product class. However, as of Magento 1.4.2.0, they deprecated this method, simply by setting the data element (and returning) an empty array. So any calls to this function would return/yield no parents. Hm, how to get the parent product now?
Finally, I managed to find a workable solution. Check this out;
$simpleProductId = 465; $parentIds = Mage::getResourceSingleton('catalog/product_type_configurable') ->getParentIdsByChild($simpleProductId); $product = Mage::getModel('catalog/product')->load($parentIds[0]); echo $product->getId(); // ID = 462 (aka, Parent of 465) |
There you have it. Short and sweet.
Adding multiple products to the cart simultaneously in Magento (Part 2)
by Dan on Sep.08, 2011, under Magento, PHP
A while ago I wrote about adding multiple products to the shopping cart simultaneously. It turns out this seems to have stopped working from Magento 1.4 or so. Up until now, I’ve not really had the time to look in detail to figure out why. Finally, this evening, I got some time. So I dug deeper.
It turns out there seems to have been some changes in the way models persist their data, and unsetting and unloading/resetting them didn’t seem to cut it any more. Anyway, below is a fixed version of the previous post. Tested on 1.4.2.0 and 1.6.0.0.
This is a replacement for the file app/code/local/BTS/AddMultipleProducts/controllers/AddController.php;
<?php class BTS_AddMultipleProducts_AddController extends Mage_Core_Controller_Front_Action { public function indexAction() { $products = explode(',', $this->getRequest()->getParam('products')); $cart = Mage::getModel('checkout/cart'); $cart->init(); /* @var $pModel Mage_Catalog_Model_Product */ foreach ($products as $product_id) { if ($product_id == '') { continue; } $pModel = Mage::getModel('catalog/product')->load($product_id); if ($pModel->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) { try { $cart->addProduct($pModel, array('qty' => '1')); } catch (Exception $e) { continue; } } } $cart->save(); if ($this->getRequest()->isXmlHttpRequest()) { exit('1'); } $this->_redirect('checkout/cart'); } } |