Engine23

Magento Ecommerce Strategy, Design & Development

Magento - How to add customer default shipping address to adminhtml grid

I just was asked to update/change the Admin Customer Grid to show the default Shipping information.

So the new grid has  Email, Company Name ( custom attribute ),  Customer Name, Address, City, State, Postcode, Country, Phone, Group, Customer Since and Website.

First thing we need to create the module to handle this.  This is new module is called Dpc.

app/etc/modules/Dpc_Adminhtml.xml

<?xml version="1.0"?>
<config>
<modules>
<Dpc_Adminhtml>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Core />
</depends>
</Dpc_Adminhtml>
</modules>
</config>

Then the app/code/local/Dpc/etc/config.xml

Read more

Magento - EAV get option values

When ever you have an catalog product attribute and are looking for the options, here is the code sample you need to get the values.

$attribute= Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'created_by');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}

Magento - Adding an index to a table during an upgrade script

I think the title of this says it all. This is how you can add an index and a new column to a table during an upgrade script

$installer= $this;

$installer->startSetup();
$tableName = $installer->getTable('dpc_subscription/event');
$indexName = $installer->getIdxName($tableName, array('is_shipped'));
$installer->getConnection()->addColumn($tableName, 'is_shipped', array(
'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Is shipped'
));
$installer->getConnection()->addIndex($tableName, $indexName, array('is_shipped'));
$installer->endSetup();

Magento - Blocks Why some rewrites do not work for example Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option_Checkbox

Most of the time doing a rewrite of a Block is pretty simple.  However you will run into issues when you want to do something as simple as remove the + from a Bundle product price.  

Most of the time, you can just do a rewrite of the block in XML and its done. However when you try to do this with some blocks, they just dont work!  The reason....some blocks are not called using the factory method.  These blocks are the ones that are extended from, so they are just part of the php code.  The way to get at those is by rewriting the blocks we can do a rewrite ( for example Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option_Checkbox ).  Then we can extend our version of Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option.

Here is how I achieve this for Bundle Products and the checkbox options for the children products.

 

Read more

Magento - Find all products in your store with missing images

$products = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('image');
$a = array();
foreach($products as $id => $_product){
$a[(int)$id] = (int)$id;
}
$has_images = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('image')->addAttributeTofilter('image', array('notnull' => ''));

foreach($has_images as $id => $_product)
{
$b[(int)$id] = (int)$id;
}

$product_ids_missing_images = array_diff($a, $b);
foreach($product_ids_missing_images as $_id){
echo '<div>'.$_id.'</div>';
}

Magento - Create a CSV of all categories

In case you need to export to a csv all the categories in your site, here is the code.

The csv just has the ID of the category, the Name of the category and the url key


$categories = Mage::getModel("catalog/category")->getCollection()
 ->addAttributeToSelect('name')
 ->addAttributeToSelect('url_key');

fputcsv($file,array('Category ID', 'Name', 'URL Key'));

// id, name, url-key
foreach($categories as $_category)
{
 fputcsv($file,array($_category->getData('entity_id'), $_category->getData('name'), $_category->getData('url_key')));

}
fclose($file);

Magento - Admin form not saving new columns

If you modify an admin form, lets say you add a column to the database for another field.  If you start to test, but the form is not saving the new fields, and it appears that Magneto is ignoring your new columns, well....it is.

Long story short, there is a function called

protected function _prepareDataForTable(Varien_Object $object, $table)

Found In :

Read more

Adminhtml Grid appears twice

If you create a custom adminhtml grid and you get the resulting data 2x after you use a column header or filter, the problem is with the xml.

The solution is to remove part of the xml.  The red text (output="toHtml") on the nested block is the code you need to remove

***** BAD XML that is showing the block twice *****
<
adminhtml_flat_entry_grid>
<block type="core/text_list" name="root" output="toHtml">
<block type="engine23_blog/adminhtml_flat_entry_grid" name="blog_ajax_grid" output="toHtml"/>
</block>
</adminhtml_flat_entry_grid>

**** Correct XML to remove the extra block when filters/sorting is used ****

Read more

MySQL result set for MIN and MAX that do not equal one another

I needed to get a mysql query where the MIN and MAX were set, but I could exclude any that the same.  I struggled a bit to figure out the proper syntax but my friend Tom B. from Gallup helped me out ( Thank you sir, that is one digital drop for you! ).  For those of you who may need to get this in the future...or maybe future me in a few weeks after I forget the syntax....here it is:

 SELECT SQL_CALC_FOUND_ROWS MIN(`entity_id`) as minResult, MAX(`entity_id`) as maxResult, `oms_id` FROM `customer_entity` WHERE `oms_id` IS NOT NULL GROUP BY `oms_id` HAVING minResult != maxResult