I am adding an additional check box for checking Magento to subscribe to the newsletter through the new module.
So far, I have added the code to my layout file (billing.phtml):
<p>Please untick this box if you do not wish to receive infrequent email updates and newsletters from us. <input type="checkbox" name="billing[is_subscribed]" title="" value="1" id="billing:is_subscribed" class="checkbox" checked="checked" /></p>
I extended the class ( app/code/local/Clientname/Checkout/Model/Type/Onepage.php ) - I distributed only the savebilling method:
<?php /** * Magento * * NOTICE OF LICENSE * * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/osl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@magentocommerce.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade Magento to newer * versions in the future. If you wish to customize Magento for your * needs please refer to http://www.magentocommerce.com for more information. * * @category Mage * @package Mage_Checkout * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ /** * One page checkout processing model */ class Eatyourwords_Checkout_Model_Type_Onepage extends Mage_Checkout_Model_Type_Onepage { public function saveBilling($data, $customerAddressId) { if (empty($data)) { return array('error' => -1, 'message' => $this->_helper->__('Invalid data.')); } $address = $this->getQuote()->getBillingAddress(); if (!empty($customerAddressId)) { $customerAddress = Mage::getModel('customer/address')->load($customerAddressId); if ($customerAddress->getId()) { if ($customerAddress->getCustomerId() != $this->getQuote()->getCustomerId()) { return array('error' => 1, 'message' => $this->_helper->__('Customer Address is not valid.') ); } $address->importCustomerAddress($customerAddress); } } else { // process billing address and validate form /* @var $addressForm Mage_Customer_Model_Form */ $addressForm = Mage::getModel('customer/form'); $addressForm->setFormCode('customer_address_edit') ->setEntity($address) ->setEntityType('customer_address') ->setIsAjaxRequest(Mage::app()->getRequest()->isAjax()); // emulate request object $addressData = $addressForm->extractData($addressForm->prepareRequest($data)); $addressErrors = $addressForm->validateData($addressData); if ($addressErrors !== true) { return array('error' => 1, 'message' => $addressErrors); } $addressForm->compactData($addressData); if (!empty($data['save_in_address_book'])) { $address->setSaveInAddressBook(1); } } // validate billing address if (($validateRes = $address->validate()) !== true) { return array('error' => 1, 'message' => $validateRes); } $address->implodeStreetAddress(); if (true !== ($result = $this->_validateCustomerData($data))) { return $result; } if (!$this->getQuote()->getCustomerId() && self::METHOD_REGISTER == $this->getQuote()->getCheckoutMethod()) { if ($this->_customerEmailExists($address->getEmail(), Mage::app()->getWebsite()->getId())) { return array('error' => 1, 'message' => $this->_customerEmailExistsMessage); } } if (!$this->getQuote()->isVirtual()) { /** * Billing address using otions */ $usingCase = isset($data['use_for_shipping']) ? (int)$data['use_for_shipping'] : 0; switch($usingCase) { case 0: $shipping = $this->getQuote()->getShippingAddress(); $shipping->setSameAsBilling(0); break; case 1: $billing = clone $address; $billing->unsAddressId()->unsAddressType(); $shipping = $this->getQuote()->getShippingAddress(); $shippingMethod = $shipping->getShippingMethod(); $shipping->addData($billing->getData()) ->setSameAsBilling(1) ->setShippingMethod($shippingMethod) ->setCollectShippingRates(true); $this->getCheckout()->setStepData('shipping', 'complete', true); break; } } $this->getQuote()->collectTotals(); if (isset($data['is_subscribed'])) { $status = Mage::getModel('newsletter/subscriber')->subscribe($data['email']); } $this->getQuote()->save(); $this->getCheckout() ->setStepData('billing', 'allow', true) ->setStepData('billing', 'complete', true) ->setStepData('shipping', 'allow', true); return array(); } }
Specifically, at the bottom of the class I inserted:
if (isset($data['is_subscribed'])) { $status = Mage::getModel('newsletter/subscriber')->subscribe($data['email']); }
I added a file to tell Magento about the module ( app/etc/modules/Clientname_Checkout.xml ):
<?xml version="1.0"?> <config> <modules> <Clientname_Checkout> <active>true</active> <codePool>local</codePool> </Clientname_Checkout> </modules> </config>
However, I am now stuck in the last step of how I get Magento to learn and use the alternative save_billing method that I expanded. I think I need to add the file /app/code/local/Clientname/config.xml .
But I donβt understand how I set it up so that the new savebilling class is used instead of the original one.
Please help someone in this last step?
thanks
Simon