I'm stumped by the following:
I'm creating a custom storefinder that uses addresses of a 3rd type in the Mage_Customer_Model_Address_Abstract class:
eg:
class Mage_Customer_Model_Address_Abstract extends Mage_Core_Model_Abstract
{
/**
* Possible customer address types
*/
const TYPE_BILLING = 'billing';
const TYPE_SHIPPING = 'shipping';
// add a 3rd
const TYPE_STOREFINDER = 'storefinder';
However, I'm not sure how exactly this can be done, as it also requires an eav attribute:
I want this option to be available in the admin backend only, by displaying it with a checkbox – here is a quick photoshop:
-
the file to duplicate and edit for this checkbox and then place in ones module is:
design/adminhtml/default/default/customer/tab/addresses.phtml
<span class="address-type-line"> <input type="checkbox" <?php if ($this->isReadonly()):?> disabled="disabled"<?php endif;?> value="<?php echo $_address->getId() ?>" id="address_item_storefinder<?php echo $_address->getId() ?>" name="account[storefinder]" title="<?php echo Mage::helper('customer')->__('Set as Storefinder Address') ?>"<?php if($_address->getId()==$customer->getStorefinderAddress()): ?> checked="checked"<?php endif; ?>/> <label for="address_item_storefinder<?php echo $_address->getId() ?>"><?php echo Mage::helper('customer')->__('Default Storefinder Address') ?></label> </span>
I've created a frontend page block which should then grab all customer addresses of type 'storefinder'
and display them on a google map.
-> define $customer->getStorefinderAddress() function – maybe done something like this:
/**
* Get default customer storefinder address
*
* @return Mage_Customer_Model_Address
*/
public function getStorefinderAddress()
{
$addresses = Mage::getResourceModel('customer/address_collection');
foreach ($addresses as $address) {
if($address->getAddressType() == 'storefinder') {
$storefinderaddresses[] = $address;
}
}
return $storefinderaddresses;
}
I need to know how to:
-> define a 3rd address type and know if the function getStorefinderAddress()
work to query these addresses by using:
$address->getAddressType() == 'storefinder'
-> save the value of the added checkbox in the database from the edit customer address in the adminhtml
Alternatively:
I've thought of an alternative way of doing this if adding a 3rd address type is too complex:
-
add an INT column to the customer/address table which simply stores true/false (0/1) for the checkbox field in the customer/tab/addresses.phtml
... ->addColumn( // Alter table to add column $installer->getTable('customer/address'), // get the customer address modules table 'in_storefinder', // column name array( // column info 'type' => Varien_Db_Ddl_Table::TYPE_INTEGER, 'length' => null, 'unsigned' => true, 'nullable' => true, 'default' => '0', 'comment' => ' added by the Storefinder', ) ); $installer->endSetup();
Any help would be greatly appreciated.
Best Answer
First of all I want to say that the
address_type
is not used for customer addresses. It is used only for quote and order addresses, so there is no need to use that in order to flag your own address type.if you need to retrieve only your store finder addresses I have an idea, but later on that.
First, you will need to create a new module to handle your new address type in order not to change the core code.
Let's call this module
StackExchange_Storefinder
.You will need the following files:
app/etc/modules/StackExchange_Storefinder.xml
- the declaration file:app/code/local/StackExchange_Storefinder/etc/config.xml
- the configuration fileapp/code/local/StackExchange/Storefinder/sql/stackexchange_storefinder_setup/install-1.0.0.php
- the install script that adds a new customer attribute to store the store finder address id (just like billing and shipping).app/code/local/StackExchange/Storefinder/Model/Customer/Attribute/Backend/Storefinder.php
- the backend model of the store finder attribute:app/code/local/StackExchange/Storefinder/Helper/Address.php
- a helper so you won't rewrite the customer model:app/code/local/StackExchange/Storefinder/Block/Adminhtml/Customer/Edit/Tab/Addresses.php
- rewrite the admin address block to add the new radio buttonapp/design/adminhtml/default/default/template/stackexchange_storefinder/tab/addresses.phtml
- the new address admin template. Same as the default one but with one extra radio button. I wrote this in a gist because it's too big to put in hereapp/code/local/StackExchange/Storefinder/Model/Adminhtml/Observer.php
- an observer for the customer save event so the new address flag will be recognized:app/code/local/StackExchange/Storefinder/Helper/Data.php
- the module default helper. Not sure this is needed.When you are done clear the cache.
Now, in order to get all addresses that have this new flag set, you first need to get all the customers and get the value of
$customer->getDefaultStoreFinderId()
and then get the address collection where the id is in the values collected above.But from my point of view, these is a totally different question. First check if you can add a third address type by using the code above.
[EDIT]
To get all the addresses that are marked as store finder you need to do something like this: