PHP : Convert CSV data into associative array

Converting CSV data into associative arrays, is very useful, as we could reference the fields directly with fields names, rather than remembering numerical indexes.

Following assumptions have been made in the preparation of the below mentioned script:

  1. First row of the CSV sheet contains, the name of the columns.
  2. The names of columns are assumed in the format as defined in database columns, for e.g. first_name, last_name, etc. rather than “First Name”, “Last Name”, etc.
  3. Comma(,) has been considered the default delimiter in the file
  4. Double-quotes(“…”) has been considered the default enclosure symbol for complex data
  5. Any data returned from the CSV row, should have same number of elements as header elements.

 

function readCSV($csvFile) {
$aryData = array();
$header = NULL;
$handle = fopen($csvFile, "r");
if($handle) {
while (!feof($handle) ) {
$aryCsvData = fgetcsv($handle);
if(!is_array($aryCsvData)) {
continue;
}
if(is_null($header)) {
$header = $aryCsvData;
} elseif (is_array($header) && count($header) == count($aryCsvData)) {
$aryData[] = array_combine($header, $aryCsvData);
}
}
fclose($handle);
}
return $aryData;
}

$csvFile, refers to full path to the CSV file to be read.

Magento : Add Custom Module to Global Search in Admin Panel

Nice post by the author : Matt Stephenson, on how to make your custom module part of global search in Magento’s Admin Panel

http://blog.mattstephens.co.uk/post/27326981315/adding-custom-module-to-magentos-admin-global-search

Magento – Closest Promo Rule

Task : Display a promotional message, when user visits the cart, to promote more purchases on the site, by providing cart discounts.

Solution:

Step-1: Create Module, I named it as ‘Promotions’

Step-2: Add Observer to ‘sales_quote_collect_totals_after’, as follows,

<sales_quote_collect_totals_after>
<observers>
<promotions_message>
<type>singleton</type>
<class>Maniacs_Promotions_Model_Observer_Promo_Message</class>
<method>addNotice</method>
</promotions_message>
</observers>
</sales_quote_collect_totals_after>

Step-3: Create observer class & method

class Maniacs_Promotions_Model_Observer_Promo_Message {
public function addNotice(Varien_Event_Observer $observer) {
$customer = Mage::getSingleton('customer/session')->getCustomer();
$group_id=Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
if(Mage::helper('customer')->isLoggedIn()) {
$group_id=$customer->getGroupId();
}
$quote = Mage::getSingleton('checkout/session')->getQuote();
$applied_rules = $quote->getAppliedRuleIds();
$attr_match = sprintf('%%%s%%', substr(serialize(array('attribute' => 'base_subtotal')), 5, -1));
$type_match = sprintf('%%%s%%', substr(serialize(array('type' => 'salesrule/rule_condition_address')), 5, -1));
$opr_grt_match = sprintf('%%%s%%', substr(serialize(array('operator' => '>')), 5, -1));
$opr_grteq_match = sprintf('%%%s%%', substr(serialize(array('operator' => '>=')), 5, -1));
$collection = Mage::getResourceModel('salesrule/rule_collection')
->addWebsiteGroupDateFilter(Mage::app()->getWebsite()->getId(), $group_id)
->addFieldToFilter('main_table.coupon_type', Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON)
->addFieldToFilter('main_table.simple_action', 'custom_promo_cart') //refers to custom action we have added for promotions, can we be ignored
->addFieldToFilter('main_table.conditions_serialized', array(
'like' => $attr_match,
))
->addFieldToFilter('main_table.conditions_serialized', array(
'like' => $type_match,
))
->addFieldToFilter('main_table.conditions_serialized', array(array(
'like' => $opr_grt_match
),array(
'like' => $opr_grteq_match
)))
;
//exclude rules those are already applicable
if($applied_rules) {
$collection->addFieldToFilter('main_table.rule_id',array(
'nin' => explode(",", $applied_rules),
));
}
$rules = $collection->setOrder('main_table.sort_order', Varien_Data_Collection::SORT_ORDER_ASC)->load();
//find sub-total
$totals = $quote->getTotals();
$subtotal=$totals["subtotal"]->getValue();
$offset =0;
foreach ($rules as $rule) {
$conditions = $rule->getConditions();
$conditions = $rule->getConditions()->asArray();
foreach( $conditions['conditions'] as $_conditions ) {
$value = $this->_getConditionVal($_conditions);
if($subtotal < $value && ($offset == 0 || $offset > $value)) {
$offset = $value;
}
}
}
if($offset && $subtotal) {
Mage::getSingleton('checkout/session')->addNotice('Make purchase for another '.Mage::helper('core')->currency($offset-$subtotal,true,false).' and get discounts');
}
return $this;
}
protected function _getConditionVal($condition) {
if(array_key_exists('conditions', $condition)) {
return $this->_getConditionVal($condition['conditions']);
} elseif($condition['type'] == "salesrule/rule_condition_address"
&& $condition['attribute'] == "base_subtotal"
&& in_array($condition['operator'],array(">", ">="))) {
return $condition['value'];
}
return 0;
}
}

Let us know your feedback, if there is scope for improvements.

Magento : Enable Widget/Block Codes in Catalog Product

In a recent task, I created a widget, which was about to insert a email opt-in form in the description area.

After creating the widget, I realized the widget worked fine, when placed in WYSIWYG editor in CMS module, but didn’t work if placed in the description field for Product.

After a bit of digging into the template parsing core classes, it was realized  that Magento didn’t support rendering block/widget directives in Catalog section.

So we decided to extend the parsing class, to enable the required feature.

Please find below steps on extending the core class ‘Mage_Catalog_Model_Template_Filter’ and add the required methods to enable the rendering for widget and block directives

1. Create a new module and place it under ‘local’ folder. I named it ‘Maniacs_Directive’

2. Add rewrite in the config file, for the new created module, in order to extend the core file

<catalog>
<rewrite>
<template_filter>Maniacs_Directive_Model_Catalog_Template_Filter</template_filter>
</rewrite>
</catalog>

3. Inside the new class ‘Maniacs_Directive_Model_Catalog_Template_Filter’, add the two methods named as follows: ‘blockDirective’ & ‘widgetDirective’

4. Contents for method ‘blockDirective’, are as follows:

public function blockDirective($construction) {
$model=Mage::getModel('core/email_template_filter');
return $model->blockDirective($construction);
}

5. Contents for method ‘widgetDirective’, are as follows:

public function widgetDirective($construction) {
$construction[2] .= sprintf(' store_id ="%s"', $this->getStoreId());
$model=Mage::getModel('widget/template_filter');
return $model->widgetDirective($construction);
}

This should enable, the rendering, and all the admin has to do is to add directive codes into the WYSIWYG editor.