Thursday, January 21, 2016
One single field validation or Validate Form Only Certain Fields in Magneto2
Tuesday, July 14, 2015
Early Magento Session Instantiation or PHPSESSID in Magento
if (!empty($sessionName)) {
$this->setSessionName($sessionName);
}else{
return $this;
}
else{ return $this; }
got some clue from http://alanstorm.com/magento_sessions_early but as per this block they are doing same change in event level i did it in core abstract file itself
Monday, April 13, 2015
Concatenate two fields in admin grid with filter in magento
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
/* adding customer name section */
$customerFirstNameAttr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('firstname');
$customerLastNameAttr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('lastname');
$collection->getSelect()
->joinLeft(
array('cusFirstnameTb' => $customerFirstNameAttr->getBackend()->getTable()),
'main_table.customer_id = cusFirstnameTb.entity_id AND cusFirstnameTb.attribute_id = '.$customerFirstNameAttr->getId(). ' AND cusFirstnameTb.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(),
array('cusFirstnameTb.value')
);
$collection->getSelect()
->joinLeft(
array('cusLastnameTb' => $customerLastNameAttr->getBackend()->getTable()),
'main_table.customer_id = cusLastnameTb.entity_id AND cusLastnameTb.attribute_id = '.$customerLastNameAttr->getId(). ' AND cusLastnameTb.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(),
array('customer_name' => "CONCAT(cusFirstnameTb.value, ' ', cusLastnameTb.value)")
);
/* end adding customer name section */
$this->setCollection($collection);
return parent::_prepareCollection();
}
Now take a deep look at the following portion from the above code section. You’ll understand that this following section is the newly added custom code:
/* adding customer name section */
$customerFirstNameAttr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('firstname');
$customerLastNameAttr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('lastname');
$collection->getSelect()
->joinLeft(
array('cusFirstnameTb' => $customerFirstNameAttr->getBackend()->getTable()),
'main_table.customer_id = cusFirstnameTb.entity_id AND cusFirstnameTb.attribute_id = '.$customerFirstNameAttr->getId(). ' AND cusFirstnameTb.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(),
array('cusFirstnameTb.value')
);
$collection->getSelect()
->joinLeft(
array('cusLastnameTb' => $customerLastNameAttr->getBackend()->getTable()),
'main_table.customer_id = cusLastnameTb.entity_id AND cusLastnameTb.attribute_id = '.$customerLastNameAttr->getId(). ' AND cusLastnameTb.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(),
array('customer_name' => "CONCAT(cusFirstnameTb.value, ' ', cusLastnameTb.value)")
);
/* end adding customer name section */
in the admin sales order grid with the name “customer_name” by inserting the following code snippet in the _prepareColumns() method of the Mage_Adminhtml_Block_Sales_Order_Grid class :
$this->addColumn('customer_name', array(
'header' => Mage::helper('adminhtml')->__('Customer Name'),
'index' => 'customer_name',
'filter_condition_callback' => array($this, 'customerNameFilter'),
'width' => '120px',
));
public function customerNameFilter($collection, $column){
$filterValue = $column->getFilter()->getValue();
if(!is_null($filterValue)){
$filterValue = trim($filterValue);
$filterValue = preg_replace('/[\s]+/', ' ', $filterValue);
$whereArr = array();
$whereArr[] = $collection->getConnection()->quoteInto("cusFirstnameTb.value = ?", $filterValue);
$whereArr[] = $collection->getConnection()->quoteInto("cusLastnameTb.value = ?", $filterValue);
$whereArr[] = $collection->getConnection()->quoteInto("CONCAT(cusFirstnameTb.value, ' ', cusLastnameTb.value) = ?", $filterValue);
$where = implode(' OR ', $whereArr);
$collection->getSelect()->where($where);
}
}
Thursday, February 5, 2015
Magento Auto Login and Auto Authorize for Rest API
Sunday, November 10, 2013
Remove the products from category products and update or map new product to category in magento programmatically
<?php
set_time_limit(0);
define('MAGENTO', '..');
require_once MAGENTO . '/app/Mage.php';
Mage::app();
//Load product model collecttion filtered by sale attribute
$proCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(array('entity_id'))
->addAttributeToFilter('sale', '1');
$writeConnection = Mage::getSingleton('core/resource')->getConnection('core_write');
// Delete Existing Mapped product from Sale Category
$catId=722;
$delQuery = 'Delete from catalog_category_product where category_id ='.$catId;
$writeConnection->query($delQuery);
$category=Mage::getModel('catalog/category')->load($catId);
$products = array();
$category_products='';
foreach ($proCollection as $product){
if(!$category_products){
$category_products=$product->getId().'=1&';
}else{
$category_products .=$product->getId().'=1&';
}
}
//$data['category_products']='4867=1&4868;=1&4876;=1';
parse_str($category_products, $products);
$category->setPostedProducts($products)->save();
$process = Mage::getModel('index/indexer')->getProcessByCode('catalog_category_product');
$process->reindexAll();
echo 'successfully mapped the data<br>';
exit;
?>
Download file setSale.php
http://www.magentocommerce.com/boards/viewthread/704216/
Get Todays start and end date with time in Magento
$todayStartOfDayDate = Mage::app()->getLocale()->date()->setTime('00:00:00')->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayEndOfDayDate = Mage::app()->getLocale()->date()->setTime('23:59:59')->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
or eg:- newarrival of product collection;
$proCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(array('entity_id'))
->addAttributeToFilter('news_from_date', array('or'=> array(
0 => array('date' => true, 'to' => $todayEndOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayStartOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter(
array(
array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
)
);
http://www.magentocommerce.com/boards/viewthread/704170/
Friday, November 8, 2013
Cache a particular block or html in Magento
public function __construct()
{
$this->addData(array(
'cache_lifetime' => 1800,
'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG),
'cache_key' => $this->getCacheKey()
));
}
public function getCacheKey()
{
return $this->getRequest()->getRequestUri().$this->getCacheCurrencyCode();
}
//retreive current currency code
public function getCacheCurrencyCode()
{
return Mage::app()->getStore()->getCurrentCurrencyCode();
}
2. cache a particular html block (only html code will cache it will not cache any objects )
you can do below logic even by creating new block file and can call it in product view page if you dont want to create new block for this small things you can cache the information
<?php
$cache = Mage::getSingleton('core/cache');
$cacheTag = array(
Mage_Catalog_Model_Product::CACHE_TAG,
);
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$storeId = Mage::app()->getStore()->getId();
$plantationkey=str_replace(' ', '_', $plantation);
$cacheKey = 'Origin_' .$plantationkey . $storeId .$currentCurrencyCode;
$orginhtml = $cache->load($cacheKey);
if(!empty($orginhtml)){
echo $orginhtml;
}else{
$theProductBlock = new Mage_Catalog_Block_Product;
$Bestsellerproducts = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addFieldToFilter(array(array('attribute'=>'bestseller','in'=>1)))
->addAttributeToFilter('visibility', $this->visibility)
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->addCategoryFilter($category); ?>
<?php if(count($Bestsellerproducts)):?>
<?php
$orginhtml ='<ul>';
foreach($Bestsellerproducts as $bestseller){
$orginhtml .='<li>
<h2>'.$bestseller->getName().'</h2>
<a href="'.$bestseller->getProductUrl().'" class="product-img"><img src="'.$this->helper('catalog/image')->init($bestseller, 'small_image')->resize(229).'" width="229" height="229" alt="'.$this->stripTags($this->getImageLabel($bestseller, 'small_image'), null, true).'" title="'.$bestseller->getName().'" /></a>
<h2 class="product-name"><a href="'.$bestseller->getProductUrl().'">'.$bestseller->getName().'</a>
</li>';
}
endif; ?>
<?php echo $orginhtml; ?>
<?php $cache->save($orginhtml, $cacheKey, $cacheTag, 3600); ?>
<?php } ?>
http://www.magentocommerce.com/boards/viewthread/700444/
Friday, May 31, 2013
Product Grid in Admin Edit Form or Page in Magento
Today i created one module which will save products that is in admin edit page you can add product grid and select the product and save it it will show back when you come to edit
some thing like in admin product edit page you have cross sells or related product tabs where user can search products ,sort filter the products and select it and save it
which can be extend to custom module custom product grid in edit page where we can filter,sort or search the product and select and save it
For more information you can contact me!! :-)Wednesday, May 29, 2013
Split Order into 2 order in magento
if you need to split the order before it placed
that is if condition is true order will be Split ed into 2 order and place 2 order i created a module which will split the order into 2 and placed it based on the condition
here i took condition based on attribute set of product which can be modified easily
you can contact me if you need !!!
Wednesday, May 22, 2013
Product Tag Import In Magento
if you need to import tags for product you can use the attached file and see read_me.txt which will explain clearly
in this code you have to use advance profile to import the tags for product
for file go to this link where file is attached
http://www.magentocommerce.com/boards/viewthread/394162/
Category Product Position Import Magento
Magento has no option ti import Position of product in category so implemented the code where admin can import the Product Position of Category which will be in Advance profile import
For more Information you contact me
Dynamic load City after selecting region of address form in Magento
In Magneto Region will be load after Country selection in address form if region name is saved in DB but there is no option to load City after selected Region so to over come from that issue i did modified in code
once you select region City will be loaded as like region
for more information contact us
Thursday, May 16, 2013
Changing or Set Shipping Price on fly from event or observer in Magento
Set Shipping Price on fly from event or observer in Magento
http://www.magentocommerce.com/boards/viewthread/316685/
Calling Form with validation outside magento
http://www.magentocommerce.com/boards/viewthread/336023/
Creating xml file of all products fields in magento programmatically
www.magentocommerce.com/boards/viewthread/267477/>
Issue:- Custom option load or set price to 0 in SCP Module in Magento
to over come from this issue
please check below post
http://www.magentocommerce.com/boards/viewthread/297872/
Time Out error in Magento report
If you set server time and memory limit to max and the also facing same issue use below code in controller
for example if you want to export any order
then in viewedAction funtion use this below code same way in export action also
ini_set('memory_limit', '-1');
set_time_limit (0);
this happen in AWS server for load-balancing server
http://www.magentocommerce.com/boards/viewthread/378702/
Adding new model for existing or core module in magento
if you add new model for existing module you have to follow below code
that if you extend one core module ex catalog,checkout etc.. and you need to add new model then follow below steps
extending catalog module to local folder
1. add resource in config.xml that is
<global>
<models>
<sugarcode_catalog>
<class>Sugarcode_Catalog_Model</class>
<resourceModel>sugarcode_catalog_resource</resourceModel>
</sugarcode_catalog>
<sugarcode_catalog_resource>
<class>Sugarcode_Catalog_Model_Resource</class>
<entities>
<newmodel>
<table>tablename_new</table>
</newmodel>
</entities>
</sugarcode_catalog_resource>
</models>
</global>
i extend catalog module and added newmodel as new model
2. create a table tablename_new manually in Db
3. create a model files that is local\Catalog\Model\Newmodel.php local\Catalog\Model\Resource\Newmodel.php local\Catalog\Model\Resource\Newmodel\Collection.php
please check attached files in this post
http://www.magentocommerce.com/boards/viewthread/387252/
now you can call
$obj=Mage::getModel(’sugarcode_catalog/newmodel’)->getCollection(); print_r($obj);
One single field validation or Validate Form Only Certain Fields in Magneto
<script>
jQuery(document).ready(function() {
jQuery('.input-text-taxvat').blur(
function() {
Validation.validate(this);
}
);
});
</script>
Note:- jQuery(’.email).blur where .email is class of email filed
Wednesday, February 27, 2013
Product or Catgeorty List Page With Layered Navigation on Home Page or Any CMS Page Or Custom Module In Magneto
if You have to call Product List page or category page with Left Layered Navigation on CMS Page or Custom Module
for Attached text File steps
1. Override Mage_Catalog_Block_Category_View to your local
class Sugarcode_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
{
public function getCurrentCategory()
{
if (!$this->hasData('current_category')) {
$category = Mage::registry('current_category');
if (!$category)
{
//get the root category
$id = $this->getCategoryId();
$category = Mage::getModel('catalog/category')->load($id);
}
$this->setData('current_category', $category);
}
return $this->getData('current_category');
}
}
2. Override Sugarcode_Catalog_Block_Layer_View to your local
class Sugarcode_Catalog_Block_Layer_View extends Mage_Catalog_Block_Layer_View
{
protected $categoryId;
protected $fromHomePage = false;
protected function _construct()
{
parent::_construct();
$this->_initBlocks();
}
public function setCategoryId($id)
{
$category = Mage::getModel('catalog/category')->load($id);
if ($category->getId()) {
Mage::getSingleton('catalog/layer')->setCurrentCategory($category);
$this->setData('current_category', $category);
$this->categoryId=$id;
$filterableAttributes = Mage::getSingleton('catalog/layer')->getFilterableAttributes();
$stateBlock = $this->getLayout()->createBlock($this->_stateBlockName)
->setLayer(Mage::getSingleton('catalog/layer'));
$categoryBlock = $this->getLayout()->createBlock($this->_categoryBlockName)
->setLayer(Mage::getSingleton('catalog/layer'))
->init();
$this->setChild('layer_state', $stateBlock);
$this->setChild('category_filter', $categoryBlock);
$filterableAttributes = $this->_getFilterableAttributes();
foreach ($filterableAttributes as $attribute) {
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = $this->_priceFilterBlockName;
} elseif ($attribute->getBackendType() == 'decimal') {
$filterBlockName = $this->_decimalFilterBlockName;
} else {
$filterBlockName = $this->_attributeFilterBlockName;
}
$this->setChild($attribute->getAttributeCode() . '_filter',
$this->getLayout()->createBlock($filterBlockName)
->setLayer(Mage::getSingleton('catalog/layer'))
->setAttributeModel($attribute)
->init());
}
Mage::getSingleton('catalog/layer')->apply();
}
}
/**
* Prepare child blocks
*
* @return Mage_Catalog_Block_Layer_View
*/
public function _prepareLayout()
{
//parent::_prepareLayout();
if (!Mage::registry('current_category'))
{
$this->fromHomePage = true;
$category = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->load($this->categoryId);
if (!Mage::helper('catalog/category')->canShow($category))
{
return false;
}
Mage::getSingleton('catalog/session')->setLastVisitedCategoryId($category->getId());
Mage::register('current_category', $category);
Mage::getSingleton('catalog/layer')->setCurrentCategory($category);
}
$stateBlock = $this->getLayout()->createBlock($this->_stateBlockName)
->setLayer($this->getLayer());
$categoryBlock = $this->getLayout()->createBlock($this->_categoryBlockName)
->setLayer($this->getLayer())
->init();
$this->setChild('layer_state', $stateBlock);
$this->setChild('category_filter', $categoryBlock);
$filterableAttributes = $this->_getFilterableAttributes();
foreach ($filterableAttributes as $attribute) {
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = $this->_priceFilterBlockName;
} elseif ($attribute->getBackendType() == 'decimal') {
$filterBlockName = $this->_decimalFilterBlockName;
} else {
$filterBlockName = $this->_attributeFilterBlockName;
}
$this->setChild($attribute->getAttributeCode() . '_filter',
$this->getLayout()->createBlock($filterBlockName)
->setLayer($this->getLayer())
->setAttributeModel($attribute)
->init());
}
$this->getLayer()->apply();
return parent::_prepareLayout();
}
}
3. In CMS design->custom XMl make
Custom Layout:- 2 columns with left bar
XML add below code
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav33" after="-" template="catalog/layer/view.phtml">
<action method="setCategoryId"><category_id>3</category_id></action>
</block>
</reference>
<reference name="content">
<block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml" >
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
<!-- <action method="addReviewSummaryTemplate"><type>default</type><template>review/helper/su.phtml</template></action> -->
<action method="setCategoryId"><category_id>3</category_id></action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
<action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
<action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
<action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
<action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</block>
</reference>
Note :- For More got to here http://www.magentocommerce.com/boards/viewthread/308574/