Search This Blog

Friday, July 8, 2011

Magento sql queries or queries or query in magento

getConnection('core_read');
//make connection
$qry = "select name FROM user_data WHERE id=1 LIMIT 1 "; //query
$res = $read->fetchRow($qry); //fetch row
$name = $res['name']; //outputs name

//other form
$qry = "select name FROM user_data WHERE namer LIKE '%a%' ";//query
$res = $read->fetchAll($qry); //get array

//Insert query
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "INSERT INTO user_data values (?,?)"; //insert query
$write->query($sql, array('name','pass')); //write to database

?>

Tuesday, July 5, 2011

Magento get root category id or magento getRootCategory or function to get root category id or magento getRootCategory()

Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId()

magento get parent category of or from current category

You can get current category from the following code:
$currentCategory = Mage::registry('current_category');

You can get the parent category from the following code:

/**
* You want the parent category of a sub-category
* Let the sub-category be $subCategory
*/

$parentCategory = Mage::getModel('catalog/category')->load($subCategory->getParentId());

Wednesday, June 1, 2011

Magento - How to run a SQL query against the database

$db = Mage::getResourceSingleton('core/resource')->getConnection('core_write');
$result = $db->query('SELECT 'entity_id' FROM 'catalog_product_entity');

if(!$result) {
return FALSE;
}

$rows = $result->fetch(PDO::FETCH_ASSOC);

if(!$rows) {
return FALSE;
}

print_r($rows);

Thursday, May 26, 2011

Magento admin redirect from a form

Class used  Mage_Core_Controller_Varien_Action class.

 Redirect to url
_redirectUrl($url)

Check url to be used as internal
_isUrlInternal($url)

Redirect to error page
_redirectError($defaultUrl)


 Redirect to path
_redirect($path, $arguments=array())



 Redirect to success page
_redirectSuccess($defaultUrl)

Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request param)
_getRefererUrl()

 Set referer url for redirect in response
_redirectReferer($defaultUrl=null)


You can use the redirect functions in your controller class.: $url = "http://abc.com";

$this->_redirectUrl($url);


Redirect with path and arguments:-

Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl($path, $arguments));




Mage::app()->getFrontController()->getResponse()->setRedirect($url);

Wednesday, May 25, 2011

magento custom csv import module

The basic case you may wonder how to do, is import of data for your custom module. Let's do this by example. Imagine you need to display on you e-shop list of stores, you have created custom module, table in database and datamodel part, all you need now is to populate this table with data you have within csv file.

Read the file
First you have to read the file. As you already know (if you read Magento Dataflow - Default Adapters [Part 2]) you can use dataflow/convert_adapter_io adapter for this.


file
var/import





Parse the file content

Now that you have read the file content, you should parse it using dataflow/convert_parser_csv.





true

1



Process rows of data

Now the custom part of this process. Within your custom module you have to create custom adapter that will create row in database for each processed row of parsed file. Within your module root directory create file ./Model/Convert/Adapter/Store.php of this content:

class Baobaz_Offer_Model_Convert_Adapter_Offer
extends Mage_Dataflow_Model_Convert_Adapter_Abstract
{
protected $_storeModel;

public function load() {
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}

public function save() {
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}

public function getStoreModel()
{
if (is_null($this->_storeModel)) {
$storeModel = Mage::getModel('baobaz_store/store');
$this->_storeModel = Mage::objects()->save($storeModel);
}
return Mage::objects()->load($this->_storeModel);
}

public function saveRow(array $importData)
{
$store = $this->getStoreModel();

if (empty($importData['code'])) {
$message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'code');
Mage::throwException($message);
}
else
{
$store->load($importData['code'],'code');
}

$store->setCode($importData['code']);
$store->setName($importData['name']);

$store->save();

return true;

}
}
Now when you have this file created you can modify a little bit the declaration of parser adding adapter and method variables:





true

1

baobaz_store/convert_adapter_store
saveRow


Having this done you should have your xml definition of custom dataflow profile looking like that:


file
var/import






true

1

baobaz_store/convert_adapter_store
saveRow


Source http://blog.baobaz.com/en/blog/customizing-magento-dataflow-import-of-custom-data
http://www.ayasoftware.com/content/how-import-configurable-products-csv-file-magento-system

magento get simple products id from configurable products

magento get simple products id from configurable products
wherer $_product is the configurable product
$ids = $_product->getTypeInstance()->getUsedProductIds();
OR
$loader = Mage::getResourceModel( 'catalog/product_type_configurable' )->load($_product);