0

I need assistance with creating a custom button that appears in the Customer Account sidebar menu if enabled in their customer account admin area.

This button's URL should also be able to be managed from the Magento Admin Customer Accounts area. For instance, if I want a customer that clicks on this custom button to be taken to google.com I should be able to insert that URL in the Magento Customer management area for that customer.

1

Your question is a bit generic, and so my answer can only cover so many bases.

In your module, create an install script that sets up a new attribute:

$this->addAttribute(
    'customer', 
    'custom_attribute', 
    array(
        'type'      => 'int',
        'label'     => 'Custom Attribute',
        'visible'   => true,
        'required'  => false,
        'input'     => 'select',
        'source'    => 'adminhtml/system_config_source_yesno',
    )
);

More information about installing attributes can be found here:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/installing_custom_attributes_with_your_module

And specific to customer attributes, see this related SO post:

https://stackoverflow.com/questions/5961290/adding-attributes-to-customer-entity

Once installed, you can access your attribute on the customer model:

$customer->getCustomAttribute()

Where CustomAttribute gets mapped to the custom_attribute ID you specified for the customer entity when installing the attribute. With this attribute now available on the frontend, you can use it to control display logic for your button.

Considering the above requires you to have a module to work from, then add to its config.xml some provisions for a layout update:

<!-- app/code/local/Namespace/Module/etc/config.xml -->
...
<frontend>
    <layout>
        <updates>
            <yourmodule>
                <file>yourmodule.xml</file>
            </yourmodule>
        </updates>
    </layout>
</frontend>
...

And in that layout XML file, add this:

<!-- app/design/frontend/[package]/[theme]/layout/yourmodule.xml -->
<customer_account>
    <reference name="left">
        <block type="core/template" name="customer.account.custom.attribute" as="custom_attribute" template="customer/account/custom_attribute.phtml" />
    </reference>
</customer_account>

Here we're tapping into the left area of the page layout and appending a new block of generic type. We also reference a template here that you'll need to build:

<?php # app/design/frontend/[package]/[theme]/template/customer/account/custom_attribute.phtml ?>
<?php $_customer = Mage::getSingleton('customer/session')->getCustomer(); ?>
<?php if ($_customer && $_customer->getCustomAttribute()) : ?>
<button type="button">Button Label</button>
<?php endif; ?>

Ideally here you would have your own block type which tucks away the call for the customer model from the session, but for the sake of brevity, and because I really don't know what you're working with currently, this should be sufficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.