Just another WordPress.com weblog

Archive for September, 2009

To add custom date field in custom module of admin panel

You can see the below code to add start date and end date field in admin custom module(Which is created using module creator).

Create two fields “start_date” and “end_date” in database table of your custom module.
$dateFormatIso = Mage::app()->getLocale() ->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField(‘start_date’, ‘date’, array(
‘label’ => Mage::helper(‘banner’)->__(‘Start Date’),
‘title’ => Mage::helper(‘banner’)->__(‘Start Date’),
‘name’ => ‘start_date’,
‘image’ => $this->getSkinUrl(‘images/grid-cal.gif’),
‘format’ => $dateFormatIso,
‘value’ => ‘start_date’,
//’required’ => true,
));
$fieldset->addField(‘end_date’, ‘date’, array(
‘label’ => Mage::helper(‘banner’)->__(‘End Date’),
‘title’ => Mage::helper(‘banner’)->__(‘End Date’),
‘name’ => ‘end_date’,
‘image’ => $this->getSkinUrl(‘images/grid-cal.gif’),
‘format’ => $dateFormatIso,
‘value’ => ‘end_date’,
//’required’ => true,
));

Copy and paste the above code to your custom_module/block/adminhtml/custom_module/Edit/Tab/Form.php file
and than open controller of the same module
Path :: custom_module/controllers/adminhtml/custommoduleController.php

Add below lines before $model->save() to save dates in
function saveAction().

if($data[‘start_date’] != NULL )
{
$date = Mage::app()->getLocale()->date($data[‘start_date’], Zend_Date::DATE_SHORT);
$model->setStartDate($date->toString(‘YYYY-MM-dd HH:mm:ss’));
}
if($data[‘end_date’] != NULL)
{
$date1 = Mage::app()->getLocale()->date($data[‘end_date’], Zend_Date::DATE_SHORT);
$model->setEndDate($date1->toString(‘YYYY-MM-dd HH:mm:ss’));
}

Please give feedback if this post is useful to you. ūüôā

-Bijal Bhavsar ūüôā

To call Static block in phtml

<?php echo $this->getLayout()->createBlock(‘cms/block’)

->setBlockId(‘contact_form_text’)->toHtml() ?>

-Meghna Vora

How to set session and get session in magento

You can create new session using below syntax:

Mage::getSingleton('core/session')->setYourVariable('data');
$Data = Mage::getSingleton('core/session')->getYourVariable();
-Bijal Bhavsar ūüôā

How to add wholesale price in shopping cart

Hi,

If you want to add wholesale price and you want to do some calculations on total price to change subtotal, than follow below steps:

Step1: Create one attribute `wholesale_price` .

Step2: Add getWholesalePrice() function defined bellow in

yourproject/app/code/local/Mage/Checkout/Model/Cart.php

public function getWholesalePrice($pID)
{
$_product = Mage::getModel(‘catalog/product’);
$_product->load($pID);
return¬†¬† $_product->getResource()->getAttribute(‘wholesale_price’)
->getFrontend()->getValue($_product);
}

Step3:  Open file yourproject/app/code/local/Mage/Sales/Model/Quote/item.php

Add below line in¬†¬† function setProduct¬† ->setWholesalePrice(Mage::getModel(‘checkout/cart’)

->getWholesalePrice($product->getId()))

after ->setCost($product->getCost()) statement in function

Step4: To change subtotal open below path file

/yourproject/app/code/local/Mage/Sales/Model/Quote/Address/Total/Subtotal.php

public function collect(Mage_Sales_Model_Quote_Address $address)
{
/**
* Reset subtotal information
*/
$address->setSubtotal(0);
$address->setBaseSubtotal(0);
$address->setTotalQty(0);
$address->setBaseTotalPriceIncTax(0);

/**
* Process address items
*/
$items = $address->getAllItems();
/*******Start:: Changes  for wholesale price calculation********/
$isWholesalePrice = false;
$finaltotal = ”;
/*******END:: Changes  for wholesale price calculation********/
foreach ($items as $item) {
if (!$this->_initItem($address, $item) || $item->getQty()<=0) {
$this->_removeItem($address, $item);
}
/******START:: Changes  for wholesale price calculation*******/
$_totWholesalePrice = $item->getWholesalePrice() * $item->getQty();
$_totPrice = $item->getPrice() * $item->getQty();
if($_totWholesalePrice >= 500 || $_totPrice >= 1000)
{
$isWholesalePrice = true;
}
$finaltotal += $_totWholesalePrice;
/*******END::Changes  for wholesale price calculation*******/
}
/******START::Changes  for wholesale price calculation******/
if($isWholesalePrice)
{
$address->setSubtotal($finaltotal);
}
/******END:: Changes  for wholesale price calculation********/

/*** Initialize grand totals***/

$address->setGrandTotal($address->getSubtotal());
$address->setBaseGrandTotal($address->getBaseSubtotal());
Mage::helper(‘sales’)->checkQuoteAmount($address->getQuote(),

$address->getSubtotal());
Mage::helper(‘sales’)->checkQuoteAmount($address->getQuote(),

$address->getBaseSubtotal());
return $this;
}

Explaination of above function:: In above function we changed calculation of subtotal, here condition is if total wholesale price(wholesale price *  Qty) of any product is more than 500 or total price(Sale price * Qty)  is more than 1000 than subtotal is calculated on wholesale price.

Above four step will help u to change subtotal and grand total according to subtotal for only one page checkout. To reflect the above calculation to multishipping than follow step5also.

Step5: Open file /yourproject/app/code/local/Mage/Sales/Model/Quote/Address/Item.php

Add below line in   function importQuoteItem

->setWholesalePrice($quoteItem->getWholesalePrice())

after ->setPrice($quoteItem->getPrice()) statement.

If this post is helpful to you than please do write feedback.

Thanks

-Bijal Bhavsar

Notice : Undefined index: website_price in /app/code/core/Mage/Catalog/Model/Product/Type/Price.php

if u find any notice related to website_price in tier price functionality when user is logged in :

set the Catalog Price Scope (system->config->catalog->price) “Global”¬† to ‚ÄúWebsite”

– Meghna Vora

How to import categories ?

contact  : meghna.vora@indianic.com

– Meghna Vora

How to get different URLs?

* Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
http://magesite.extension/js/
* Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
http://magesite.extension/index.php/
* Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
http://magesite.extension/media/
* Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
http://magesite.extension/skin/
* Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
http://magesite.extension/

–¬† Vishal Surani

SQL to delete all orders

This query is very helpful to delete all records of orders from the database.

Please replace “YOUR_DB_NAME” with your database name

— Reset Magento TEST Data

SET FOREIGN_KEY_CHECKS=0;

— reset dashboard search queries
TRUNCATE `catalogsearch_query`;
ALTER TABLE `catalogsearch_query` AUTO_INCREMENT=1;

— reset sales order info
TRUNCATE `sales_order`;
TRUNCATE `sales_order_datetime`;
TRUNCATE `sales_order_decimal`;
TRUNCATE `sales_order_entity`;
TRUNCATE `sales_order_entity_datetime`;
TRUNCATE `sales_order_entity_decimal`;
TRUNCATE `sales_order_entity_int`;
TRUNCATE `sales_order_entity_text`;
TRUNCATE `sales_order_entity_varchar`;
TRUNCATE `sales_order_int`;
TRUNCATE `sales_order_text`;
TRUNCATE `sales_order_varchar`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sendfriend_log`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `wishlist`;
TRUNCATE `log_quote`;
TRUNCATE `report_event`;

ALTER TABLE `sales_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_datetime` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_decimal` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_int` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_text` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_varchar` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;

— Reset all ID counters
TRUNCATE `eav_entity_store`;
ALTER TABLE  `eav_entity_store` AUTO_INCREMENT=1;

SET FOREIGN_KEY_CHECKS=1;

— set appropriate prefixes for orders, invoices, shipments, credit memos
INSERT INTO¬† `YOUR_DB_NAME`.`eav_entity_store` (`entity_store_id` ,`entity_type_id` ,`store_id` ,`increment_prefix` ,`increment_last_id`) VALUES (‘1′,¬† ’11’,¬† ‘1’,¬† ‘1’,¬† ‘000000000’);
update `eav_entity_store` set `increment_prefix`= 1 where `entity_type_id`=’4′ and `store_id`=’1′;
update `eav_entity_store` set `increment_last_id`= ‘000000000’ where `entity_type_id`=’4′ and `store_id`=’1′;

INSERT INTO¬† `YOUR_DB_NAME`.`eav_entity_store` (`entity_store_id` ,`entity_type_id` ,`store_id` ,`increment_prefix` ,`increment_last_id`) VALUES (‘2′,¬† ’16’,¬† ‘1’,¬† ‘2’,¬† ‘000000000’);
update `eav_entity_store` set `increment_prefix`= 2 where `entity_type_id`=’18’ and `store_id`=’1′;
update `eav_entity_store` set `increment_last_id`= ‘000000000’ where `entity_type_id`=’18’ and `store_id`=’1′;

INSERT INTO¬† `YOUR_DB_NAME`.`eav_entity_store` (`entity_store_id` ,`entity_type_id` ,`store_id` ,`increment_prefix` ,`increment_last_id`) VALUES (‘3′,¬† ’19’,¬† ‘1’,¬† ‘3’,¬† ‘000000000’);
update `eav_entity_store` set `increment_prefix`= 3 where `entity_type_id`=’24’ and `store_id`=’1′;
update `eav_entity_store` set `increment_last_id`= ‘000000000’ where `entity_type_id`=’24’ and `store_id`=’1′;

INSERT INTO¬† `YOUR_DB_NAME`.`eav_entity_store` (`entity_store_id` ,`entity_type_id` ,`store_id` ,`increment_prefix` ,`increment_last_id`) VALUES (‘4′,¬† ’23’,¬† ‘1’,¬† ‘4’,¬† ‘000000000’);
update `eav_entity_store` set `increment_prefix`= 4 where `entity_type_id`=’28’ and `store_id`=’1′;
update `eav_entity_store` set `increment_last_id`= ‘000000000’ where `entity_type_id`=’28’ and `store_id`=’1′;

If this post is helpful to you than please do write feedback.

– Bijal Bhavsar

To get all level categories name and url…

<?php
require_once ‘app/Mage.php’;
Mage::app();
$category = Mage::getModel(‘catalog/category’);
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$arr = array();
if ($ids):
foreach ($ids as $id){
$cat = Mage::getModel(‘catalog/category’);
$cat->load($id);
if($id != 3):
if($cat->getIsActive()):
$catName[] =  $cat->getName();
$path[] = $cat->getUrl();
endif;
endif;
}
?>

<?php

require_once ‘app/Mage.php’;

Mage::app();

$category = Mage::getModel(‘catalog/category’);

$tree = $category->getTreeModel();

$tree->load();

$ids = $tree->getCollection()->getAllIds();   // we can get all level categories id

if ($ids):

foreach ($ids as $id){

$cat = Mage::getModel(‘catalog/category’);

$cat->load($id);

if($id != 3): // if ¬†category id is not “root catalog” id – here root catalog id is 3

if($cat->getIsActive()): // if category is active

$catName[] =  $cat->getName(); //  To get name of the category

$path[] = $cat->getUrl(); // to get url of category

endif;

endif;

}

?>

If these post is useful to you than please give your feedback.

– Bijal Bhavsar