Monday, December 26, 2011

Magento Database Interaction

require_once 'app/Mage.php';
Mage::app('default');

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult=$write->query("SELECT product_id, category_id FROM catalog_category_product ORDER BY product_id");
while ($row = $readresult->fetch() ) {
$write2 = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult2=$write2->query("SELECT sku FROM catalog_product_entity WHERE entity_id = ".$row['product_id']." LIMIT 1");
while ($row2 = $readresult2->fetch() ) {
$sku = $row2['sku'];
}
".$sku."echo $row['product_id']."
";

}

Sunday, December 25, 2011

Create a Global function in Magento

This code will allow you to add a function that can be called from anywhere within Magento. It extends the helper class

1) Create a file named ‘Mycode.xml’ and copy it to app/etc/modules/ – it should look like this:

 version="1.0"?>             true    local         

2) Create the directory
app/code/local/Mycode/Function/etc
and then create a file named ‘config.xml’
In it copy:

 version="1.0"?>                              1.0.0                                                              Mycode_Function_Helper                                 

3) Create the directory
app/code/local/Mycode/Function/Helper
and then create a file named ‘Data.php’
In it copy:

   class Mycode_Function_Helper_Data extends Mage_Core_Helper_Abstract {    public function test(){    return 'works';    }   }

You can now call this function like so

         echo Mage::helper('function')->test();      ?>