Search This Blog

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

Tuesday, May 24, 2011

Remove or rename add new save continue delete button from magento admin

If you don’t want to show the ‘Add New’ button in the Grid. The Add New button is present in top right corner of Grid Page.

Rename ‘Add New’ button

Here are the steps to rename the ‘Add New’ text to anything you required (for example, ‘Add Report’):-

- Go to YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile.php
- Add the following code in the constructor of this file:-

$this->_addButtonLabel = Mage::helper('yourmodulename')->__('Add Report');

Remove ‘Add New’ button

Here are the steps to remove the ‘Add New’ button:-

- Go to YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile.php
- Add the following code in the constructor of this file (it should be just below the call to parent constructor):-

parent::__construct();
$this->_removeButton('add');


In edit.php
parent::__construct();
$this->_removeButton('delete');
$this->_removeButton('save');
$this->_removeButton('back');

in grid.php
parent::__construct();
$this->_removeButton('add');

Monday, May 16, 2011

magento get last added item from session or get cart items from session

How to get all cart items from cart session in magento

$productid=Mage::getSingleton('checkout/session')->getLastAddedProductId(true);

-----------OR----------------------------
$session= Mage::getSingleton('checkout/session');
foreach($session->getQuote()->getAllItems() as $item)
{
$productid = $item->getProductId();
$productsku = $item->getSku();
$productname = $item->getName();
$productqty = $item->getQty();
}

Wednesday, May 11, 2011

Magento query to check for product visible or visibility and enable or is_enable

Magento query to check for product visible or visibility and enable or is_enable

$visibility = array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG);
$prod= Mage::getModel('catalog/product')->getCollection();
->addAttributeToFilter('visibility', $visibility);
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);