// Gets the current store's details $store = Mage::app()->getStore(); // Gets the current store's id $storeId = Mage::app()->getStore()->getStoreId(); // Gets the current store's code $storeCode = Mage::app()->getStore()->getCode(); // Gets the current website's id $websiteId = Mage::app()->getStore()->getWebsiteId(); // Gets the current store's group id $storeGroupId = Mage::app()->getStore()->getGroupId(); // Gets the current store's name $storeName = Mage::app()->getStore()->getName(); // Gets the current store's sort order $storeSortOrder = Mage::app()->getStore()->getSortOrder(); // Gets the current store's status $storeIsActive = Mage::app()->getStore()->getIsActive(); // Gets the current store's locale $storeLocaleCode = Mage::app()->getStore()->getLocaleCode(); // Gets the current store's home url $storeHomeUrl = Mage::app()->getStore()->getHomeUrl(); //load store by code or id $store = Mage::getModel('core/store')->load($code); //you can use code or id
Tag Archives: magento
Magento – How to get current router module controller action ?
You can easily know “where you are” using these fonctions :
in template / block files
$this->getRequest()->getRouteName(); $this->getRequest()->getModuleName(); $this->getRequest()->getControllerName(); $this->getRequest()->getActionName();
in class files
Mage::app()->getRequest()->getRouteName(); Mage::app()->getRequest()->getModuleName(); Mage::app()->getRequest()->getControllerName(); Mage::app()->getRequest()->getActionName();
Magento SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘000000254’ for key ‘UNQ_SALES_FLAT_ORDER_INCREMENT_ID’
In some cases, customers encounter this error on your website :
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '000000254' for key 'UNQ_SALES_FLAT_ORDER_INCREMENT_ID'
You don’t understand and drive you crazy ?
Don’t worry, I will help you.
First, this error probably happend when customer quit violenty the payment page, without canceling, so your order is not “finished” and your quote is not “released” (is_active = 1 in db). Customer come back, recover his quote and try to proceed it again.
The reserved_increment_id is not updated and when magento try to create the order, mySQL give him the previous error.
How a so obvious bug like this can exist ? and why does it crash only on my website, it seems to work fine on other website ?
Let me guest, your client (clients always have weird needs …) ask you to start order number at 0 instead of 100000000, or ask you to put a letter into it.
So, why does this reserved_increment_id is not updated ?
Let check on the function which update the reserved_increment_id field :
// /app/code/core/Mage/Sales/Model/Resource/Quote.php /** * Check is order increment id use in sales/order table * * @param int $orderIncrementId * @return boolean */ public function isOrderIncrementIdUsed($orderIncrementId) { $adapter = $this->_getReadAdapter(); $bind = array(':increment_id' => (int)$orderIncrementId); $select = $adapter->select(); $select->from($this->getTable('sales/order'), 'entity_id') ->where('increment_id = :increment_id'); $entity_id = $adapter->fetchOne($select, $bind); if ($entity_id > 0) { return true; } return false; }
You see it, the little (int) in this line $bind = array(‘:increment_id’ => (int)$orderIncrementId); ?
This transform your increment_id in your request from “000000254” to “254”, mySQL doesn’t find an order with the increment_id “254” and your quote is not updated.
To fix it, override this function :
In your Sales/etc/config.xml file, add
<global> <model> <sales_resource> <rewrite> <quote>Adin_Sales_Model_Resource_Sales_Quote</quote> </rewrite> </sales_resource> </model> </global>
And create the new class file /app/code/local/Adin/Sales/Model/Resource/Sales/Quote.php
class Adin_Sales_Model_Resource_Sales_Quote extends Mage_Sales_Model_Resource_Quote { /** * Check is order increment id use in sales/order table * * @param int $orderIncrementId * @return boolean */ public function isOrderIncrementIdUsed($orderIncrementId) { $adapter = $this->_getReadAdapter(); $bind = array(':increment_id' => $orderIncrementId); $select = $adapter->select(); $select->from($this->getTable('sales/order'), 'entity_id') ->where('increment_id = :increment_id'); $entity_id = $adapter->fetchOne($select, $bind); if ($entity_id > 0) { return true; } return false; } }
That should do the tricks.
Magento : how to get current user ?
Need to get the current user in magneto ?
Here how to.
Frontend :
if(Mage::getSingleton('customer/session')->isLoggedIn()) { Mage::getSingleton('customer/session')->getCustomer(); } //or Mage::helper('customer')->getCustomerName();
Backend :
$user = Mage::getSingleton('admin/session')->getUser(); //bonus : roles $uer->getRoles();
Magento link in backend redirect to dashboard
You are working on Magento backoffice and make a link to a new page but when you click on your anchor, you are redirected to the dashboard ?
Here how to make a proper link :
echo "<a href='".Mage::helper('adminhtml')->getUrl('*/*/displaydetail', array('id' => $cour['id']))."' >".$cour['name']."</a>";
Something like
Mage::getUrl('*/*/displaydetail/id/'.$cour['id'])
doesn’t work, even if I had the FormKey. But you can use it for ajax request
jQuery.ajax({ url: "<?php echo Mage::getUrl('*/*/classroomfromplaceAjax') ?>", type: "POST", dataType: 'json', data: 'place_id=' + place_id + '&index=' + index + '&form_key=' + window.FORM_KEY, success: function (data) { jQuery('#classroom_' + index).html(data.html); }, });
Magento force store_id session
This is how to force or change magento store_id into the session
// Start store emulation process $appEmulation = Mage::getSingleton('core/app_emulation'); $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
Magento custom sort on grid – sort increment_id by numeric not alpha
You can easily filter column in a magento grid (see my previous post) but customize a sort is a little bit more hand made.
In this example in the order grid, I want to sort by increment_id, but this is a text fields and I want to sort it by numeric.
default sort : 1, 10, 100, 2, 3 …
numeric sort : 1, 2, 3, 10, 100 …
If you look the SQL request, you got this
SELECT `main_table`.* FROM `sales_flat_order_grid` AS `main_table` ORDER BY increment_id DESC LIMIT 20
To tell MySQL to sort this text column by numeric, you can add “+ 0″ to the ORDER BY clause, this will transform the text field into numeric field
SELECT `main_table`.* FROM `sales_flat_order_grid` AS `main_table` ORDER BY increment_id + 0 DESC LIMIT 20
Now, how to tell Magento to add “+ 0″ to this sort ?
Override your grid class and modify the “_setCollectionOrder($column)” function.
class Adin_Sales_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid { protected function _setCollectionOrder($column) { $collection = $this->getCollection(); if ($collection) { $columnIndex = $column->getFilterIndex() ? $column->getFilterIndex() : $column->getIndex(); if($columnIndex == 'increment_id') { $columnIndex = 'increment_id + 0'; } $collection->setOrder($columnIndex, strtoupper($column->getDir())); } return $this; } }
Magento custom filter on grid
Sometimes, you need a special column on your grid from another table or with some calculation.
It works well, column is displayed but filter doesn’t work.
Here a small example how to customize your filter :
On your grid class (Adin_Sponsorship_Block_Adminhtml_Sponsorship_Grid.class.php), override your _prepareColumns() function
protected function _prepareColumns() { $this->addColumn('sponsor_email', array( 'header' => Mage::helper('evian_customer')->__('Sponsor Email'), 'index' => 'sponsor_email', 'align' => 'center', 'filter_condition_callback' => array($this, '_filterSponsortEmail'), ) ); }
The ‘filter_condition_callback’ option indicates to use as filter a specific method, on the current class ($this) the method _filterSponsortEmail().
So next step, create this method.
protected function _filterSponsortEmail($collection, $column) { if (!$value = trim($column->getFilter()->getValue())) { return; } $this->getCollection()->addFieldToFilter('customer.email', array('like' => '%'.$value.'%')); }
Yes, this is this simple.
If you use a datetime column, it’s just a bit more complicated, see below
$this->addColumn('created_at', array( 'header' => Mage::helper('evian_customer')->__('Date of sponsorship'), 'index' => 'created_at', 'align' => 'center', 'type' => 'datetime', 'filter_condition_callback' => array($this, '_filterDate'), ) );
and the filter method
protected function _filterDate($collection, $column) { $filters = $column->getFilter()->getValue(); $from = $filters['from']; $to = $filters['to']; $this->getCollection()->addFieldToFilter('main_table.created_at', array('gteq' => $from->toString('yyyy-MM-dd'))); $this->getCollection()->addFieldToFilter('main_table.created_at', array('lteq' => $to->toString('yyyy-MM-dd'))); }
Magento filter products collection get only visible
You want to retrieve all your products but only the one visible in catalog or filter ?
You may use this
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*'); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
But it’s depreacated and doesn’t work.
You may instead use this :
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*'); $collection->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
Magento multi fields validator for RIB
Magento come with a great form validator : VarienForm. These is a lot of existing validator, for every usage and you can easyly add some. This is how you can do it with a commun example, how to check RIB bank account.
First, our html code
<ul class="inlineblockli"> <li> <label for="code_bank_pb"><?php echo $this->__('bank code') ?></label> <input type="text" class="inputText inputTextB validate-digits required-entry validate-length minimum-length-5 maximum-length-5" id="code_bank_pb" maxlength="5" name="prelevement[code_bank_pb]"> </li> <li> <label for="code_guichet_pb"><?php echo $this->__('branch code') ?></label> <input type="text" class="inputText inputTextB validate-digits required-entry validate-length minimum-length-5 maximum-length-5" id="code_guichet_pb" maxlength="5" name="prelevement[code_guichet_pb]"> </li> <li> <label for="num_compte"><?php echo $this->__('Account number to be debited') ?></label> <input type="text" class="inputText inputTextBig3 validate-digits required-entry validate-length minimum-length-11 maximum-length-11" id="num_compte" maxlength="11" name="prelevement[num_compte]"> </li> <li> <label for="key_rib"><?php echo $this->__('RIB key') ?></label> <input type="text" class="inputText inputTextSm3 validate-digits required-entry validate-compte validate-length minimum-length-2 maximum-length-2" id="key_rib" maxlength="2" name="prelevement[key_rib]"> </li> </ul>
We first do a lenght validation with these 3 class : validate-length minimum-length-11 maximum-length-11
Then we validate the rib with this class : validate-compte
You will tell me this validator doesn’t exeist. That’s right, let’s create it.
<script type="text/javascript"> //<![CDATA[ Validation.addAllThese( [ ['validate-compte', '<?php echo Mage::helper('rating')->__('Please check your RIB account') ?>', function(v) { var code_banque = jQuery('#code_bank_pb').val(); var code_guichet = jQuery('#code_guichet_pb').val(); var numero_compte = jQuery('#num_compte').val(); var cle = jQuery('#key_rib').val(); var CompteTmp = code_banque + code_guichet + numero_compte + cle; while(CompteTmp.length > 9) { var CompteTmp2 = CompteTmp.substr(0,9) % 97; CompteTmp = CompteTmp2 + CompteTmp.substr(9,CompteTmp.length); } var CompteTmp2 = CompteTmp.substr(0,9) % 97; if(CompteTmp2 % 97 == 0) { return true; } return false; }] ] ); //]]> </script>
Some explainations :
jQuery('#code_bank_pb').val();
Magento use prototype, tu use jQuery, use jQuery instead of $
For the multi field validation, I grab the different fields from this
var code_banque = jQuery('#code_bank_pb').val(); var code_guichet = jQuery('#code_guichet_pb').val(); var numero_compte = jQuery('#num_compte').val(); var cle = jQuery('#key_rib').val();
I don’t know if this is the cleaner way to do (not really usable for another code) but it works.
If you always use the same input id, you can reuse this code on another site.