paolo@bimodesign.com | +34 608 61 64 10

E-Commerce

        

Magento, tips and tricks

The purpose of this post is collecting a list of tips and tricks of Magento, thus avoiding to lose a lot of time finding a solution for simples issues.
This post will be update periodically and currently it contains these topics:
How to...
- Get the validate date from/to of a product
- Get the shipping address
- Get the stock, sold and dummy values
- Redirect to cart instead of oneStepCheckout
- Add custom Terms and Conditions
- Change the top links
- Modify dailydeals code to not duplicate merchant user
- Get the user data
- Make differents menu between User and Merchant
- Solve the DailyDeals Bug (Gracias Javi!)
- Add the phone number variable into the email (Gracias Cata!)


How to get the validate date from/to of a product

$dataDealFromDateNoFormat = $deal->getProduct()->getDealFromDate();
$dataDealFromDateClass = new DateTime($dataDealFromDateNoFormat);
$dataCouponDesde = $dataDealFromDateClass->format('d.m.Y');

$dataDealToDateNoFormat = $deal->getDateToUser();
$dataDealToDateClass = new DateTime($dataDealToDateNoFormat);
$dataCouponTo = $dataDealToDateClass->format('d.m.Y'); 

Note the difference between the get methods.

How to get the shipping address

$collection = Mage::getResourceModel('sales/order_address_collection');
$collection->addAttributeToFilter('parent_id', $order_id);

foreach ($collection->getItems() as $item) {
   if($item['address_type']=='Billing'){
      $firstNameBilling=$item['firstname'];
      $lastNameBilling=$item['lastname'];
      $calleBilling=$item['street'];
      $codigoPostalBilling=$item['postcode'];
      $ciudadBilling=$item['city'];
      $provinciaBilling=$item['region'];
   }else{
      // Envio
   }
}



How to get the stock, sold and dummy values

$deal = $this->getDeal();
$sold_count = $deal->getSoldCount() + $deal->getDummyCount();

$Inventory = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId())->getQty();
    
//Unidades
echo "VENDIDOS--->" . $deal->getSoldCount();
echo "DUMMY--->" . $deal->getDummyCount();
echo "TOTALES--->" . number_format($Inventory, 0);



How to redirect to cart instead of oneStepCheckout
Open the file app/code/local/AdolMedia/DailyDeals/controllers/BuynowController.php change this

// $this->_redirect('checkout/onepage');
   $this->_redirect('checkout/cart');



How to Add custom Terms and Conditions
Open the file app/design/frontend/base/default/template/onestepcheckout/onestepcheckout.phtml then comment this code

//    $('onestepcheckout-toc-link').observe('click', function(e) {
//        e.preventDefault();
//        termsPopup.open();
//    });

then open this file app/design/frontend/base/default/template/onestepcheckout/onestepcheckout/review.phtml and add this

jQuery(document).ready(function(){
    jQuery('#onestepcheckout-toc-link').fancybox({
        titleShow : false,
        autoScale: true
    });
});

then, into the same file, modify this code in this way

<label for="terms_conditions_checkbox_id"><?php echo $this->__('I read and agree to ');?> <a id="onestepcheckout-toc-link" target="_blank" href="/tc.php" ><?php echo $this->__('Terms and Conditions');?></a></label>

Note: you have to create the file tc.php



How to change the top links
You have to modify these two xml files
app/design/frontend/default/default/layout/checkout.xml
app/design/frontend/default/default/layout/customer.xml
finding where is the text name="top.links and comment every menu that you wish to leave. For example, in case of Cart link:

<action method="addCartLink"></action>



How to modify dailydeals code to not duplicate merchant user
Open this file app/code/local/AdolMedia/DailyDeals/Model/Mysql4/Merchant.php and modify the else condition in this way

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT entity_id FROM customer_entity  WHERE email = '". $object->getMerchantEmail() . "' LIMIT 1";
$entityId = $readConnection->fetchOne($query);
$object->setCustomerId($entityId);  



How to get the user data

$sessionCustomer = Mage::getSingleton("customer/session");
print_r($sessionCustomer->getCustomer()); 
and the array _data return has these information:
[_data:protected] => Array
        (
            [entity_id] => 579
            [entity_type_id] => 1
            [group_id] => 1
            [firstname] => paolo
            [lastname] => bindi
            [email] => paolo@mail.com
            [telefono_movil] => 608616410
            [password] => passwd
            [password_hash] => 460f6a7b519d8c8bc9821a7548feee6e:ia
            [confirmation] => 
            [store_id] => 1
            [parent_id] => 0
            [created_at] => 2013-04-10 11:47:03
            [updated_at] => 2013-04-10 11:47:03
            [created_in] => Spanish
            [website_id] => 1
            [disable_auto_group_change] => 0
            [is_just_confirmed] => 1
        )

then to get, for example, the name user

echo $sessionCustomer->getCustomer()->getData('firstname');

or

echo $sessionCustomer->getCustomer()->getFirstname();
echo $sessionCustomer->getCustomer()->getEmail();



How to make differents menu between User and Merchant
Open the file app/design/frontend/base/default/template/customer/account/navigation.phtml and change all its content with

<div class="block block-account">
    <div class="block-title">
        <strong><span><?php echo $this-<__('My Account'); ?></span></strong>
    </div>
    <div class="block-content">
        <ul>
            <?php $isMerchant =
Mage::getSingleton('customer/session')->getCustomer()->getIsMerchant();
?>
            <?php if (!$isMerchant): ?>
            <li><a href="<?php echo Mage::getBaseUrl();
?>customer/address/edit">Mis Datos</a><li>
            <li><a href="<?php echo Mage::getBaseUrl();
?>deals/customer/coupon/">Mis pedidos</a><li>
            <li><a href="<?php echo Mage::getBaseUrl();
?>sales/order/history/">Mis cupones</a><li>
            <li><a href="<?php echo Mage::getBaseUrl();
?>newsletter/manage/">Newsletters</a><li>
            <?php else: ?>
             <li><a href="<?php echo Mage::getBaseUrl();
?>customer/address/edit">Mis Datos</a><li>
            <li><a href="<?php echo Mage::getBaseUrl();
?>deals/merchant/dashboard/">Panel Merchant</a><li>
            <li><a href="<?php echo Mage::getBaseUrl();
?>deals/merchant/redeemcoupon/"><?php echo $this->__('Redeem Coupon');
?></a><li>
            <?php endif; ?>
        </ul>
    </div>
</div>



How to solve the DailyDeals Bug (Gracias Javi!)
Open the file app/code/local/AdolMedia/DailyDeals/Model/Mysql4/History/Collection.php and change

public function getCustomerCoupons($customer_id, $customer_email){
	$this->getSelect()->where('customer_email="'.$customer_email.'" OR customer_id = "$customer_id"');
        return $this;
}

with

public function getCustomerCoupons($customer_id, $customer_email){
	$this->getSelect()->where('customer_email="'.$customer_email.'" OR customer_id = "'.$customer_id.'"');
	return $this;
}



Add the phone number variable into the email (Gracias Cata!)
From the admin panel, go the menu system/general/information and add the phone number into the Store contact telephone field.
Then into the email templates, add this variable in order to print the number.

 {{config path='general/store_information/phone'}}

or/and

echo Mage::getStoreConfig('general/store_information/phone');