Category Archives: PHP

Creating products programmatically in Magento

Let’s face it – importing is hard. Especially if it’s from some other unfamiliar product, or something that doesn’t already have a Dataflow/import plugin for Magento already. Dataflow has its limits. I’ve actually found it easier to import manually (creating models for products) as opposed to writing Dataflow routines. However, one of my biggest bug bears is not just creating products in Magento programmatically, but creating them successfully. We always end up with stock or relationship issues between simple and configurable products.

Continue reading “Creating products programmatically in Magento” »

Get website-level configuration in Magento

I stumbled across this one a couple of times now, and it’s caught me out every time.

With Magento, you have the method Mage::getStoreConfig() to get a store-level config, but nothing obvious to get a website-level configuration (such as a default URL for the website).

So I used this;

Mage::app()->getWebsite($websiteId)->getConfig('web/unsecure/base_url')

It’s the same syntax as what’s used in Mage::getStoreConfig(), but uses getWebsite() instead of getStore(). Hopefully this won’t catch me out anymore!

Bug in Magento 1.6.1.0/1.6.2.0 affecting development sites using base_url

I installed a copy of Magento 1.6.1.0 on a dev site I setup to do some testing with Varnish with (more on that later). However, in the requirement to be able to get to Magento using 2 different URL’s, I stumbled across this quite annoying bug.

a:5:{i:0;s:67:"Illegal scheme supplied, only alphanumeric characters are permitted";i:1;s:729:"#0 /home/dan/workspace/magento1610/app/code/core/Mage/Core/Model/Store.php(712): Zend_Uri::factory('{{base_url}}')
#1 /home/dan/workspace/magento1610/app/code/core/Mage/Core/Controller/Varien/Front.php(313): Mage_Core_Model_Store->isCurrentlySecure()
#2 /home/dan/workspace/magento1610/app/code/core/Mage/Core/Controller/Varien/Front.php(161): Mage_Core_Controller_Varien_Front->_checkBaseUrl(Object(Mage_Core_Controller_Request_Http))
#3 /home/dan/workspace/magento1610/app/code/core/Mage/Core/Model/App.php(349): Mage_Core_Controller_Varien_Front->dispatch()
#4 /home/dan/workspace/magento1610/app/Mage.php(640): Mage_Core_Model_App->run(Array)
#5 /home/dan/workspace/magento1610/index.php(80): Mage::run('', 'store')
#6 {main}";s:3:"url";s:85:"/index.php/admin/system_config/edit/section/web/key/41c8c3d3f4bcb72e3c267ae0b73333d7/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:7:"default";}

Bug reports on Magento’s site here, here, here and here. Not being patient enough for Varien to fix it, I developed a small extension which resolves the problem. It’s available to download below.

Put the tarball in the Magento webroot and extract. After you install this extension, you’ll need to empty the cache manually (if it was enabled). I assume if you’re reading this, then your Magento installation is broken. To do this, simply remove the contents of var/cache in Magento’s webroot.

When you re-visit your Magento’s front or backend, the problem should vanish.

BTS_1610Fix.tar.gz (md5: 8316468f4e179cf2f512edaaf62a12db)

Updated 9th January 2012: It apparent that, attempting to login before applying this update with {{base_url}} set screws up the admin login cookies somehow. When you apply this patch, you’ll probably need to empty out your chosen browser’s cookie jar before attempting to login. You can tell if you need to or not by trying to login, and Magento coming back to you with a login screen, with no failed login message.

Updated 12th January 2012: Magento 1.6.2.0 was released today. I can confirm that this problem still exists and has not yet been fixed (despite what the developers have written on one of the bug reports).

Updated 25th April 2012: I can confirm that this problem has been resolved in the latest 1.7.0.0 version, released 24th April 2012.

Getting Configurable Product from Simple Product ID in Magento 1.5+

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)

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');
    }
 
}