Remove telephone as required field on shipping but not billing Magento - magento

How do I make the telephone field not required on shipping but have it required on billing in onepage checkout?
I have followed many forums that point to the below method but this disables the required element on both billing and shipping?
http://swarminglabs.com/magento-making-the-telephone-field-not-required-at-checkout/#comment-2687

I do not know any extension that would allow this. If you want to make it work you should work with Mage_Customer_Model_Form. During checkout process magento calls validateData() method of this model. This method is defined in Mage_Eav_Model_Form. You need to rewrite another model, namely Mage_Sales_Model_Quote_Address as its parent Mage_Customer_Model_Address_Abstract has a valid() method that checks if telephone is not emtpy. So, assuming you have removed is_required and validation_rules for this attribute
in a module etc/config.xml
<config>
<global>
<models>
<customer>
<rewrite>
<form>YourNamespace_YourModule_Model_Customer_Form</form>
</rewrite>
</customer>
<sales>
<rewrite>
<quote_address>YourNamespace_YourModule_Model_Quote_Address</quote_address>
</rewrite>
</sales>
</models>
</global>
</config>
in YourNamespace/YourModule/Model/Customer/Form.php
class YourNamespace_YourModule_Model_Customer_Form extends Mage_Customer_Model_Form {
public function validateData(array $data) {
//perform parent validation
$result = parent::validateData($data);
//checking billing address; perform additional validation
if ($this->getEntity()->getAddressType() == Mage_Sales_Model_Quote_Address::TYPE_BILLING) {
$valid = $this->_validatePhoneForBilling($data);
if ($result !== true && $valid !== true) {
$result[] = $valid;
}
elseif ($result === true && $valid !== true) {
$result = $valid;
}
}
return $result;
}
protected function _validatePhoneForBilling($data) {
$errors = array();
if (empty($data['telephone'])) {
$attribute = $this->getAttribute('telephone');
$label = Mage::helper('eav')->__($attribute->getStoreLabel());
$errors[] = Mage::helper('eav')->__('"%s" is a required value.', $label);
}
if (empty($errors)) {
return true;
}
return $errors;
}
}
in YourNamespace/YourModule/Model/Quote/Address.php
class YourNamespace_YourModule_Model_Quote_Address extends Mage_Sales_Model_Quote_Address {
public function validate() {
if ($this->getAddressType() == self::TYPE_SHIPPING) {
$result = parent::validate();
$errorMsg = Mage::helper('customer')->__('Please enter the telephone number.');
if (is_array($result) && in_array($errorMsg, $result)) {
$result = array_diff($result, array($errorMsg));
}
if (empty($result)) {
return true;
}
return $result;
}
else {
return parent::validate();
}
}
}

Related

Implement discounts in magento 1.9 with magento wallet functionality

I am have implemented custom discounts in magento which is working properly.But, when discount is implemented and someone is trying to pay from magento wallet balance, wallet seems to stop working and the amount paid from wallet does not get deducted from the grand total of the cart.
Discount is applied as follows:
config.xml-
<sales>
<quote>
<totals>
<discount>
<class>Uf_Rewards_Model_Discount</class>
<after>subtotal</after>
</discount>
</totals>
</quote>
</sales>
Discount.php:
class Uf_Rewards_Model_Discount extends Mage_SalesRule_Model_Quote_Discount {
public function collect(Mage_Sales_Model_Quote_Address $address) {
if ($address->getData('address_type') == 'billing')
return $this;
$discount = 10; //your discount percent
$grandTotal = $address->getGrandTotal();
$baseGrandTotal = $address->getBaseGrandTotal();
$totals = array_sum($address->getAllTotalAmounts());
$baseTotals = array_sum($address->getAllBaseTotalAmounts());
$address->setFeeAmount(-$discount);
$address->setBaseFeeAmount(-$discount);
$address->setGrandTotal($grandTotal + $address->getFeeAmount());
$address->setBaseGrandTotal($baseGrandTotal + $address->getBaseFeeAmount());
$address->setDiscountAmount(-$discount);
$address->setBaseDiscountAmount(-$discount);
return $this;
}
public function fetch(Mage_Sales_Model_Quote_Address $address) {
if ($address->getData('address_type') == 'billing')
return $this;
$amt = $address->getFeeAmount();
// $amt=300;
if ($amt != 0) {
Mage::log($amt, NULL, 'obsrvr.log');
$address->addTotal(array(
'code' => 'Discount',
'title' => 'Discount',
'value' => $amt
));
}
return $this;
}
}

How to send two email in magento with different template?

Is it possible to send two email in magento on order with different template, please tell how, or any extension available?
Sorry, my solution not very clear sometimes.
For test purposes I created new template, based on existing 'New order' in System / Email Templates.
Then create system.xml file where we can add new fields with template selectors:
<?xml version="1.0"?>
<config>
<sections>
<sales_email>
<groups>
<order>
<fields>
<template2>
<label>222 New Order Confirmation Template</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</template2>
<guest_template2 translate="label">
<label>222 New Order Confirmation Template for Guest</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</guest_template2>
</fields>
</order>
</groups>
</sales_email>
</sections>
</config>
As you can see we created two new fields template2 and guest_template2.
Then create your own new model, which extends !!!NOT REWRITES!!! Mage_Sales_Model_Order
Add there two new constants
class Vendor_Somemodule_Model_Order extends Mage_Sales_Model_Order
{
const XML_PATH_EMAIL_TEMPLATE = 'sales_email/order/template2';
const XML_PATH_EMAIL_GUEST_TEMPLATE = 'sales_email/order/guest_template2';
and copy-paste method sendNewOrderEmail(). In this method remove two rows at the bottom
$this->setEmailSent(true);
$this->_getResource()->saveAttribute($this, 'email_sent');
And your result will be:
public function sendNewOrderEmail()
{
$storeId = $this->getStore()->getId();
if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) {
return $this;
}
$emailSentAttributeValue = $this->load($this->getId())->getData('email_sent');
$this->setEmailSent((bool)$emailSentAttributeValue);
if ($this->getEmailSent()) {
return $this;
}
// Get the destination email addresses to send copies to
$copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
$copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);
// Start store emulation process
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
try {
// Retrieve specified view block from appropriate design package (depends on emulated store)
$paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment())
->setIsSecureMode(true);
$paymentBlock->getMethod()->setStore($storeId);
$paymentBlockHtml = $paymentBlock->toHtml();
} catch (Exception $exception) {
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
throw $exception;
}
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
// Retrieve corresponding email template id and customer name
if ($this->getCustomerIsGuest()) {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
$customerName = $this->getBillingAddress()->getName();
} else {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
$customerName = $this->getCustomerName();
}
$mailer = Mage::getModel('core/email_template_mailer');
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($this->getCustomerEmail(), $customerName);
if ($copyTo && $copyMethod == 'bcc') {
// Add bcc to customer email
foreach ($copyTo as $email) {
$emailInfo->addBcc($email);
}
}
$mailer->addEmailInfo($emailInfo);
// Email copies are sent as separated emails if their copy method is 'copy'
if ($copyTo && $copyMethod == 'copy') {
foreach ($copyTo as $email) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($email);
$mailer->addEmailInfo($emailInfo);
}
}
// Set all required params and send emails
$mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId($templateId);
$mailer->setTemplateParams(array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml
)
);
$mailer->send();
return $this;
}
Then you just need to call this method before magento call this method itself after order placed.
put in config.xml following:
<global>
<models>
<somemodule>
<class>Vendor_Somemodule_Model</class>
</somemodule>
</models>
<events>
<checkout_type_onepage_save_order_after>
<observers>
<send_duplicate_email>
<type>singleton</type>
<class>somemodule/observer</class>
<method>saveDuplicateEmail</method>
</send_duplicate_email>
</observers>
</checkout_type_onepage_save_order_after>
</events>
</global>
and create necessary observer:
class Vendor_Somemodule_Model_Observer
{
public function saveDuplicateEmail($observer)
{
$orderId = $observer->getOrder()->getId();
$order = Mage::getModel('somemodule/order')->load($orderId);
$order->sendNewOrderEmail();
}
}

remove all other shipping method if there's a method having shipping amount zero

Could it be possible to remove all the shipping methods except the one having shipping amount 0?
There may be a promotion rule which will return the amount 0 for shipping, so it must not be removed.
Display only shipping methods with price 0 (zero) if any
In my module I'd overridden the Mage_Checkout_Block_Onepage_Shipping_Method_Available block as:
config.xml
<global>
<blocks>
<checkout>
<rewrite>
<onepage_shipping_method_available>Excellence_Subscription_Block_Checkout_Onepage_Shipping_Method_Available</onepage_shipping_method_available>
</rewrite>
</checkout>
</blocks>
</global>
Excellence_Subscription_Block_Checkout_Onepage_Shipping_Method_Available
<?php
class Excellence_Subscription_Block_Checkout_Onepage_Shipping_Method_Available extends Mage_Checkout_Block_Onepage_Shipping_Method_Available {
public function getShippingRates() {
$groups = parent::getShippingRates();
$free = array();
foreach ($groups as $code => $_rates) {
foreach ($_rates as $_rate) {
if (!$_rate->getPrice() > 0) {
$free[$code] = $_rates;
}
}
}
if (!empty($free)) {
return $this->_rates = $free;
}
return $groups;
}
}

include tracking number in emails subject part

As we send shippment emails to customers, in this shippment email, we get tracking number from track.phtml and now the subject part of the email is like, order number and shippment number. I would like to change the subject of the email in which we are senidng as shippment emails. The subject has to be with tracking number. For order number we can get with "order.increment_id", But I dont know how to get the tracking number. SO how can I display tracking number in subject of email?
This cannot be done only by using a variable name like "order.increment_id" in the email template. You have to send the tracking data to the email template processor to achieve this with an example in 4 steps.
Step1>> Add Module configuration
In app/etc/modules/Eglobe_Sales.xml
<?xml version="1.0"?>
<config>
<modules>
<Eglobe_Sales>
<active>true</active>
<codePool>local</codePool>
</Eglobe_Sales>
</modules>
</config>
Step2>> Add config.xml(app/code/local/Eglobe/Sales/etc/config.xml)
<?xml version="1.0"?>
<config>
<modules>
<Eglobe_Sales>
<version>0.1.0</version>
</Eglobe_Sales>
</modules>
<global>
<models>
<sales>
<rewrite>
<order_shipment>Eglobe_Sales_Model_Order_Shipment</order_shipment>
</rewrite>
</sales>
</models>
</global>
</config>
Step3>> Override Mage_Sales_Model_Order_Shipment::sendEmail()
<?php
class Eglobe_Sales_Model_Order_Shipment extends Mage_Sales_Model_Order_Shipment {
/**
* Send email with shipment data
*
* #param boolean $notifyCustomer
* #param string $comment
* #return Mage_Sales_Model_Order_Shipment
*/
public function sendEmail($notifyCustomer = true, $comment = '')
{
$order = $this->getOrder();
$storeId = $order->getStore()->getId();
if (!Mage::helper('sales')->canSendNewShipmentEmail($storeId)) {
return $this;
}
// Get the destination email addresses to send copies to
$copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
$copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);
// Check if at least one recepient is found
if (!$notifyCustomer && !$copyTo) {
return $this;
}
// Start store emulation process
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
try {
// Retrieve specified view block from appropriate design package (depends on emulated store)
$paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
->setIsSecureMode(true);
$paymentBlock->getMethod()->setStore($storeId);
$paymentBlockHtml = $paymentBlock->toHtml();
} catch (Exception $exception) {
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
throw $exception;
}
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
// Retrieve corresponding email template id and customer name
if ($order->getCustomerIsGuest()) {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
$customerName = $order->getBillingAddress()->getName();
} else {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
$customerName = $order->getCustomerName();
}
$mailer = Mage::getModel('core/email_template_mailer');
if ($notifyCustomer) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($order->getCustomerEmail(), $customerName);
if ($copyTo && $copyMethod == 'bcc') {
// Add bcc to customer email
foreach ($copyTo as $email) {
$emailInfo->addBcc($email);
}
}
$mailer->addEmailInfo($emailInfo);
}
// Email copies are sent as separated emails if their copy method is 'copy' or a customer should not be notified
if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
foreach ($copyTo as $email) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($email);
$mailer->addEmailInfo($emailInfo);
}
}
// Set all required params and send emails
$mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId($templateId);
$mailer->setTemplateParams(array(
'order' => $order,
'shipment' => $this,
'comment' => $comment,
'billing' => $order->getBillingAddress(),
'payment_html' => $paymentBlockHtml,
//setting the `track number here, A shihpment can have more than one track numbers so seperating by comma`
'tracks' => new Varien_Object(array('tracking_number' => implode(',', $this->getTrackingNumbers())))
)
);
$mailer->send();
return $this;
}
//Creating track number array
public function getTrackingNumbers()
{
$tracks = $this->getAllTracks();
$trackingNumbers = array();
if (count($tracks)) {
foreach ($tracks as $track) {
$trackingNumbers[] = $track->getNumber();
}
}
return $trackingNumbers;
}
}
Step4>> Modify your shipment email tempate subject by adding {{var tracks.track_number}}

Trying get dynamic content hole-punched through Magento's Full Page Cache

I am using Magento Enterprise 1.10.1.1 and need to get some dynamic content on our product pages. I am inserting the current time in a block to quickly see if it is working, but can't seem to get through full page cache.
I have tried a variety of implementations found here:
http://tweetorials.tumblr.com/post/10160075026/ee-full-page-cache-hole-punching
http://oggettoweb.com/blog/customizations-compatible-magento-full-page-cache/
http://magentophp.blogspot.com/2011/02/magento-enterprise-full-page-caching.html
Any solutions, thoughts, comments, advice is welcome.
here is my code:
app/code/local/Fido/Example/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Fido_Example>
<version>0.1.0</version>
</Fido_Example>
</modules>
<global>
<blocks>
<fido_example>
<class>Fido_Example_Block</class>
</fido_example>
</blocks>
</global>
</config>
app/code/local/Fido/Example/etc/cache.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<placeholders>
<fido_example>
<block>fido_example/view</block>
<name>example</name>
<placeholder>CACHE_TEST</placeholder>
<container>Fido_Example_Model_Container_Cachetest</container>
<cache_lifetime>86400</cache_lifetime>
</fido_example>
</placeholders>
</config>
app/code/local/Fido/Example/Block/View.php
<?php
class Fido_Example_Block_View extends Mage_Core_Block_Template
{
private $message;
private $att;
protected function createMessage($msg) {
$this->message = $msg;
}
public function receiveMessage() {
if($this->message != '') {
return $this->message;
}
else {
$this->createMessage('Hello World');
return $this->message;
}
}
protected function _toHtml() {
$html = parent::_toHtml();
if($this->att = $this->getMyCustom() && $this->getMyCustom() != '') {
$html .= '<br />'.$this->att;
}
else {
$now = date('m-d-Y h:i:s A');
$html .= $now;
$html .= '<br />' ;
}
return $html;
}
}
app/code/local/Fido/Example/Model/Container/Cachetest.php
<?php
class Fido_Example_Model_Container_Cachetest extends Enterprise_PageCache_Model_Container_Abstract {
protected function _getCacheId()
{
return 'HOMEPAGE_PRODUCTS' . md5($this->_placeholder->getAttribute('cache_id') . $this->_getIdentifier());
}
protected function _renderBlock()
{
$blockClass = $this->_placeholder->getAttribute('block');
$template = $this->_placeholder->getAttribute('template');
$block = new $blockClass;
$block->setTemplate($template);
return $block->toHtml();
}
protected function _saveCache($data, $id, $tags = array(), $lifetime = null) { return false; }
}
app/design/frontend/enterprise/[mytheme]/template/example/view.phtml
<?php echo $this->receiveMessage() ?>
snippet from app/design/frontend/enterprise/[mytheme]/layout/catalog.xml
<reference name="content">
<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
<block type="fido_example/view" name="product.info.example" as="example" template="example/view.phtml" />
The <name> in the cache.xml must match your blocks full name in the layout, not the alias, e.g. <name>product.info.example</name>
Also, _getIdentifier() isn't implemented on Enterprise_PageCache_Model_Container_Abstract, just remove it from the string returned by your _getCacheId().
If you need to add some variants, implement _getIdentifier() to return a session id or whatever you need.

Resources