Created
December 9, 2019 09:04
-
-
Save zaheerbadi/8ec312d248a0895909c47d4193554a81 to your computer and use it in GitHub Desktop.
Magento1 Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################### Change store programatically ########### | |
Mage::app()->getLocale()->emulate($shipment->getStoreId()); | |
Mage::app()->setCurrentStore($shipment->getStoreId()); | |
####################### Parse the magento directives in custom module ###################### | |
$helper = Mage::helper('cms'); | |
$processor = $helper->getBlockTemplateProcessor(); | |
$html = $processor->filter($contentToParse); | |
echo $html; | |
################## parse json in prototype ########### | |
var json = transport.responseJSON; | |
############################################ | |
################## Response in controller action and display it with ajax ############ | |
$this->getResponse()->setBody('Some Response'); for controller justo text. | |
/*** if you pass array in setbody it will always give json */ | |
$response = array('success', 'Name Saved'); | |
$this->getResponse()->setBody($response); | |
onSuccess: function(transport) { | |
var json = transport.responseText.evalJSON(); | |
alert(json.success); | |
}, | |
/*** when you just setresponse only text ***/ | |
$response = 'your data saved' | |
$this->getResponse()->setBody($response); | |
onSuccess: function(transport) { | |
var response = transport.responseText; | |
alert("Success!" + response); | |
} | |
########################## Resizing via Varien_Image ######################### | |
$image = new Varien_Image('/full/fs/path/to/image.jpg'); | |
// you cannot use method chaining with Varien_Image | |
$image->constrainOnly(false); | |
$image->keepFrame(true); | |
// avoid black borders by setting background colour | |
$image->backgroundColor(array(255,255,255)); | |
$image->keepAspectRatio(true); | |
$image->resize(216, 139); | |
$image->save('/full/fs/path/to/save/to.jpg'); | |
############################################################################### | |
####################### Import DB Using CMd ################################# | |
1)open cmd | |
2)go to your mysql folder i.e. D:\wamp\bin\mysql\mysql5.6.12\bin | |
3)Run This command :: mysql -h <hostip> -u <usernameofdb> -p <password of the db> | |
4)Run :: source <your sqlfile path> | |
############################################################################ | |
################################## nth level category treeeee ##################### | |
<?php $rootCatId = Mage::app()->getStore()->getRootCategoryId(); | |
$catlistHtml = $this->getTreeCategories($rootCatId, false); | |
echo $catlistHtml; ?> | |
public function getTreeCategories($parentId, $isChild){ | |
$allCats = Mage::getModel('catalog/category')->getCollection() | |
->addAttributeToSelect('*') | |
->addAttributeToFilter('is_active','1') | |
->addAttributeToFilter('include_in_menu','1') | |
->addAttributeToFilter('parent_id',array('eq' => $parentId)) | |
->addAttributeToSort('position', 'asc'); | |
$html .= '<ul class="simple">'; | |
foreach($allCats as $category) | |
{ | |
$html .= '<li>'; | |
$html .= '<a href="' . $category->getUrl(). '">' . $category->getName() . '</a>'; | |
$subcats = $category->hasChildren(); | |
if($subcats){ | |
$html .= $this->getTreeCategories($category->getId(), true); | |
} | |
$html .= '</li>'; | |
} | |
$html .= '</ul>'; | |
return $html; | |
} | |
################################################################### | |
################## filter cms block contetn ########## | |
Cms content :: Hello {{variable1}}, | |
Thanks we will contact you. | |
Thanks | |
['variable2'] | |
Make This function in any of your helper file. | |
public function getFilterContent($html = '') | |
{ | |
if($html != ''){ | |
$array = array(); | |
$array['variable1'] = 'customername'; | |
$array['variable2'] = 'Team'; /* you can set any no of variable like this */ | |
// loading the filter which will get the array we created and parse the block content | |
$filter = Mage::getModel('cms/template_filter'); | |
/* @var $filter Mage_Cms_Model_Template_Filter */ | |
$filter->setVariables($array); | |
// return the filtered block content. | |
return $filter->filter($html); | |
}else{ | |
return; | |
} | |
} | |
} | |
############################################################# | |
################## Resize image using helper function add below function to your helper file and call with helper ########### | |
public function getProductImage($image,$width = 270,$height = 270){ | |
if($image!="" && file_exists(Mage::getBaseDir('media').DS.$image)){ | |
$_imageUrl = Mage::getBaseDir('media').DS.$image; | |
$imageResized = Mage::getBaseDir('media').DS.'resized/'.$image; | |
if (!file_exists($imageResized)&&file_exists($_imageUrl)) { | |
$imageObj = new Varien_Image($_imageUrl); | |
$imageObj->constrainOnly(TRUE); | |
$imageObj->keepAspectRatio(TRUE); | |
$imageObj->keepFrame(FALSE); | |
$imageObj->quality(100); | |
$imageObj->resize($width, null); | |
$imageObj->save($imageResized); | |
$resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized/" . $image; | |
return $resizedURL; | |
} | |
elseif(file_exists($imageResized)) { | |
$resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized/" . $image; | |
return $resizedURL; | |
} | |
else{ | |
return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$image; | |
} | |
} | |
else | |
{ | |
return Mage::getBaseUrl('media')."order_frame_pdf_image/resized/270X/noimage.png"; | |
} | |
} | |
######################################################################################## | |
##################### Discounted price ########################## | |
Mage::getModel('catalogrule/rule')->calcProductPriceRule($product,$product->getPrice()); | |
#################################################################################### | |
########################### Validate form before submit with button event ########### | |
<script> | |
//<![CDATA[ | |
var formId = 'pdfform'; | |
var pdfform = new VarienForm(formId, true); | |
function doValidateform() { | |
if (pdfform.validator.validate()) { | |
$j('#pdfform').submit(); | |
$j.fancybox.close(); | |
} | |
} | |
new Event.observe(formId, 'submit', function(e){ | |
e.stop(); | |
doValidateform(); | |
}); | |
//]]> | |
</script> | |
################################################################################# | |
############### set variblae in CMS block ############################ | |
Create this function in any helper file | |
public function setVariableforCMSBlock($BlockId,$VariableArray=""){ | |
if(!$blockId){ | |
return; | |
} | |
$block = Mage::getModel('cms/block') | |
->setStoreId(Mage::app()->getStore()->getId()) | |
->load($blockId); | |
$filter = Mage::getModel('cms/template_filter'); | |
$filter->setVariables($array); | |
return $filter->filter($block->getContent()); | |
} | |
use of function | |
$test['test1'] = 'test1'; | |
$test['test2'] = 'test2'; | |
Mage::helper('example')->setVariableforCMSBlock('testCmsBlockId',$test); | |
you can set content in cms block like | |
Hello this is {{var test1}} and this is another {{var test2}} | |
OUTPUT :: Hello this is test1 and this is another test2 | |
################################################################################### | |
###################################### Reindeing programatically ############################################ | |
$indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection(); | |
foreach ($indexingProcesses as $process) { | |
$process->reindexEverything(); | |
} | |
############################################################################################################ | |
#################### Enable Developer Mode ############## | |
http://blog.chapagain.com.np/magento-1-x-enabling-viewing-full-error-display/ | |
######################################################### | |
####################### remove folder windows command ########## | |
rmdir /S <Directoryname> | |
######################################################### | |
####################### Get Full Action Name ######################### | |
Mage::app()->getFrontController()->getAction()->getFullActionName(); | |
######################## Display Handle on the page ############### | |
<?php Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles()); exit; ?> | |
##################### Add js to admin grid ################ | |
just add this function in you grid file | |
public function _prepareLayout() | |
{ | |
$head = $this->getLayout()->getBlock('head'); | |
$head->addJs('mage/adminhtml/removeproducts.js'); | |
return parent::_prepareLayout(); | |
} | |
######################## Make customer logged in #################### | |
Mage::getSingleton('customer/session')->setCustomerAsLoggedIn(<loaded customer object>) | |
##################### Debug layout file not loadded ###################### | |
https://stackoverflow.com/questions/7048354/magento-layout-content-is-not-being-rendered-displayed | |
https://magento.stackexchange.com/questions/95/debugging-layout-xml-loading |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment