$installer = $this;
$installer->startSetup();
$connection = $installer->getConnection();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->removeAttribute('customer', 'name_goes_here');
$setup->addAttribute('customer', 'name_goes_here', array(
'input' => 'text',
'type' => 'varchar',
'label' => 'name_goes_here',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->endSetup();
$installer->installEntities();
use this in mysql4-upgrade-0.1.0-0.1.1.php or any latest sql folder script
Search This Blog
Tuesday, October 18, 2011
Tuesday, September 20, 2011
How to Remove Product from cart on checkout page or by coding in magento
$session= Mage::getSingleton('checkout/session');
$quote = $session->getQuote();
$cart = Mage::getModel('checkout/cart');
$cartItems = $cart->getItems();
foreach ($cartItems as $item)
{
$quote->removeItem($item->getId())->save();
}
$quote = $session->getQuote();
$cart = Mage::getModel('checkout/cart');
$cartItems = $cart->getItems();
foreach ($cartItems as $item)
{
$quote->removeItem($item->getId())->save();
}
Wednesday, August 24, 2011
Magento display gift message in front end
$message = Mage::getModel('giftmessage/message');
/* Add Gift Message*/
$gift_message_id = $order->getOrder()->getGiftMessageId();
if(!is_null($gift_message_id)) {
$message->load((int)$gift_message_id);
$gift_sender = $message->getData('sender');
$gift_recipient = $message->getData('recipient');
echo $gift_message = $message->getData('message');
}
http://www.magentocommerce.com/boards/viewthread/11346/
/* Add Gift Message*/
$gift_message_id = $order->getOrder()->getGiftMessageId();
if(!is_null($gift_message_id)) {
$message->load((int)$gift_message_id);
$gift_sender = $message->getData('sender');
$gift_recipient = $message->getData('recipient');
echo $gift_message = $message->getData('message');
}
http://www.magentocommerce.com/boards/viewthread/11346/
Friday, August 12, 2011
best php export to csv without space or comma
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// DB Connection here
$db = mysql_select_db("test",$con);
$result = mysql_query("SELECT * FROM test1");
csvToExcelDownloadFromResult($result);
function csvToExcelDownloadFromResult($result, $showColumnHeaders = true, $asFilename = 'data.csv') {
setExcelContentType();
setDownloadAsHeader($asFilename);
return csvFileFromResult('php://output', $result, $showColumnHeaders);
}
function csvFileFromResult($filename, $result, $showColumnHeaders = true) {
$fp = fopen($filename, 'w');
$rc = csvFromResult($fp, $result, $showColumnHeaders);
fclose($fp);
return $rc;
}
function setExcelContentType() {
if(headers_sent())
return false;
header('Content-type: application/vnd.ms-excel');
return true;
}
function setDownloadAsHeader($filename) {
if(headers_sent())
return false;
header('Content-disposition: attachment; filename=' . $filename);
return true;
}
function csvFromResult($stream, $result, $showColumnHeaders = true) {
if($showColumnHeaders) {
$columnHeaders = array();
$nfields = mysql_num_fields($result);
for($i = 0; $i < $nfields; $i++) { $field = mysql_fetch_field($result, $i); $columnHeaders[] = $field->name;
}
fputcsv($stream, $columnHeaders);
}
$nrows = 0;
while($row = mysql_fetch_row($result)) {
fputcsv($stream, $row);
$nrows++;
}
return $nrows;
}
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// DB Connection here
$db = mysql_select_db("test",$con);
$result = mysql_query("SELECT * FROM test1");
csvToExcelDownloadFromResult($result);
function csvToExcelDownloadFromResult($result, $showColumnHeaders = true, $asFilename = 'data.csv') {
setExcelContentType();
setDownloadAsHeader($asFilename);
return csvFileFromResult('php://output', $result, $showColumnHeaders);
}
function csvFileFromResult($filename, $result, $showColumnHeaders = true) {
$fp = fopen($filename, 'w');
$rc = csvFromResult($fp, $result, $showColumnHeaders);
fclose($fp);
return $rc;
}
function setExcelContentType() {
if(headers_sent())
return false;
header('Content-type: application/vnd.ms-excel');
return true;
}
function setDownloadAsHeader($filename) {
if(headers_sent())
return false;
header('Content-disposition: attachment; filename=' . $filename);
return true;
}
function csvFromResult($stream, $result, $showColumnHeaders = true) {
if($showColumnHeaders) {
$columnHeaders = array();
$nfields = mysql_num_fields($result);
for($i = 0; $i < $nfields; $i++) { $field = mysql_fetch_field($result, $i); $columnHeaders[] = $field->name;
}
fputcsv($stream, $columnHeaders);
}
$nrows = 0;
while($row = mysql_fetch_row($result)) {
fputcsv($stream, $row);
$nrows++;
}
return $nrows;
}
Monday, July 25, 2011
Using php to display visitor / user information such as their IP address
$ip = $_SERVER['REMOTE_ADDR'];
$hostaddress = gethostbyaddr($ip);
$browser = $_SERVER['HTTP_USER_AGENT'];
$referred = $_SERVER['HTTP_REFERER']; // a quirky spelling mistake that stuck in php
print "Display IP address:
\n";
print "$ip
\n";
print "More detailed host address:
\n";
print "$hostaddress
\n";
print "Display browser info:
\n";
print "$browser
\n";
print "Where you came from (if you clicked on a link to get here:
\n";
if ($referred == "") {
print "Page was directly requested";
}
else {
print "$referred";
}
$hostaddress = gethostbyaddr($ip);
$browser = $_SERVER['HTTP_USER_AGENT'];
$referred = $_SERVER['HTTP_REFERER']; // a quirky spelling mistake that stuck in php
print "Display IP address:
\n";
print "$ip
\n";
print "More detailed host address:
\n";
print "$hostaddress
\n";
print "Display browser info:
\n";
print "$browser
\n";
print "Where you came from (if you clicked on a link to get here:
\n";
if ($referred == "") {
print "Page was directly requested";
}
else {
print "$referred";
}
magento how to use distinct or get unique records from collection
magento how to use distinct or get unique records from collection
$collection = Mage::getModel('catalog/product')->getCollection()
->distinct(true)
->addAttributeToSelect('color')
->load();
$collection = Mage::getModel('catalog/product')->getCollection()
->distinct(true)
->addAttributeToSelect('color')
->load();
Sunday, July 17, 2011
Magento send mails like contact us form or forms or how to send mails in magento
if(!empty($post)) { $translate = Mage::getSingleton('core/translate');
$translate->setTranslateInline(false); try { $name = $_POST['name'];
$email = $_POST['email']; $to_admin =
Mage::getStoreConfig('trans_email/ident_general/email'); $name_admin =
Mage::getStoreConfig('trans_email/ident_general/name'); $subject =
'Contact Us'; /*$vars = array('Name'=>$name, 'Email Id' =>$email ); */
//const EMAIL_TEMPLATE_XML_PATH = 'customer/testemail/email_template';
//$templateId = Mage::getStoreConfig(EMAIL_TEMPLATE_XML_PATH);
$templateId=1; $sender = array('name' => $name,'email' => $email);
$translate=Mage::getModel('core/email_template')
->setTemplateSubject($subject) ->sendTransactional($templateId, $sender,
$to_admin, $name_admin, array('data' => $vars));
$translate->setTranslateInline(true);
Mage::getSingleton('core/session')->addSuccess(Mage::helper('customer')->__('Data
Sent Successfully')); $url = Mage::getUrl().'thanks';
Mage::app()->getFrontController()->getResponse()->setRedirect($url); }
catch (Exception $e) {
Mage::getSingleton('core/session')->addError(Mage::helper('customer')->__('Unable
to send email.')); $url = Mage::getUrl().'contactus';
Mage::app()->getFrontController()->getResponse()->setRedirect($url);
return; } }
$translate->setTranslateInline(false); try { $name = $_POST['name'];
$email = $_POST['email']; $to_admin =
Mage::getStoreConfig('trans_email/ident_general/email'); $name_admin =
Mage::getStoreConfig('trans_email/ident_general/name'); $subject =
'Contact Us'; /*$vars = array('Name'=>$name, 'Email Id' =>$email ); */
//const EMAIL_TEMPLATE_XML_PATH = 'customer/testemail/email_template';
//$templateId = Mage::getStoreConfig(EMAIL_TEMPLATE_XML_PATH);
$templateId=1; $sender = array('name' => $name,'email' => $email);
$translate=Mage::getModel('core/email_template')
->setTemplateSubject($subject) ->sendTransactional($templateId, $sender,
$to_admin, $name_admin, array('data' => $vars));
$translate->setTranslateInline(true);
Mage::getSingleton('core/session')->addSuccess(Mage::helper('customer')->__('Data
Sent Successfully')); $url = Mage::getUrl().'thanks';
Mage::app()->getFrontController()->getResponse()->setRedirect($url); }
catch (Exception $e) {
Mage::getSingleton('core/session')->addError(Mage::helper('customer')->__('Unable
to send email.')); $url = Mage::getUrl().'contactus';
Mage::app()->getFrontController()->getResponse()->setRedirect($url);
return; } }
Subscribe to:
Posts (Atom)