How to create member only module in Magento?

June 13, 2011 14:26

Sometimes you may have to implement the module that is for member only, how to do it in Magento?

In your controller which extends Mage_Core_Controller_Front_Action, add the following function:

        public function preDispatch() {
            parent::preDispatch();

            $action = $this->getRequest()->getActionName();
            $loginUrl = Mage::helper('customer')->getLoginUrl();

            if (!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)) {
                $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            }
        }

If the customer didn’t login, the controller will redirect the page to login page.

Got it?

Happy Magento’ing ~

Share

Override Controllers in Magento

May 30, 2011 16:41

Sometimes you may have to override the core controllers to add some specific functions, don’t know how to override them? Just simply follow below:

If you want to override the some controllers in adminhtml module for admin, add the following XML to your own module’s config.xml:

<config>
    ....
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <my_Module before="Mage_Adminhtml">MyNameSpace_MyModule_Adminhtml</my_Module>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    ...
</config>

If you want to override the some controllers in checkout module for frontend, add the following XML to your own module’s config.xml:

<config>
    ...        
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <module before="Mage_Checkout">MyNameSpace_MyModule_Checkout</module>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
    ...
</config>

That is, easy?

Happy Magento’ing ~

Share

Add session message manually

December 30, 2010 16:31

Sometimes it doesn’t work when using the following codes to show the successful / error message:

$this->getMessagesBlock()->getGroupedHtml();

Doesn’t matter, you can use the below codes to show the successful / error message:

Mage::app()->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton('customform/session')->getMessages(true));
echo Mage::app()->getLayout()->getMessagesBlock()->getGroupedHtml();

That’s easy, right?

Happy Magento’ing ~

Share
Comments Off

Android on Touch Cruise

November 30, 2010 15:00

Recently I didn’t update this blog because I’s working hard to flash my old mobile phone HTC Touch Cruise to Android 2.2.1. After two weeks testing, now should be okay to use.

First you should download many files:
- download Hard SPL
- download latest radio (1.65.38.14)
- download the latest NBH for kernel 2.6.32
- download Incubu26Jc’s Super FroYo 2.2.1 RLS 16 ROM
- download the modules update for 2.6.32 kernel
- download the app2sd update

Do the following steps:
- format your SD card to FAT32 if necessary
- update your device to be Hard SPL following this tutorial
- update the latest radio using ROM Update Utility included in radio’s zip
- use atool to change the kernel settings for your own customization; and create the install script “install-seq.sh”.
- remove all the .NBH files in ROM Update Utility and put the customized NBH into the folder of ROM Update Utility
- rename Incubu26Jc’s Super FroYo 2.2.1 RLS 16 ROM to be “androidinstall.tgz”
- in your SD card, create “andboot” folder, put the “install-seq.sh” (install script), “androidinstall.tgz” (Incubu26Jc’s Super FroYo 2.2.1 RLS 16) and “androidupdate.tgz” (modules update for 2.6.32 kernel) into it
- run ROM Update Utility to flash the Android kernel into your phone
- device will reboot and install the ROM automatically
- after you see “Install module for this kernel”, reboot your device by removing battery and install the modules update by pressing and holding the center button when you see the sentence similar as “press dpad button to enter …” (choose 4. install update, then quit)
- you should then successfully go into the system, then reboot and install the app2sd update
- done and test the device

Please be careful that all the data inside your device will be erased. You may have to reflash, install and test cycling to find the best customization for your own.

If you have any question, please ask in XDA Developer Forum. Sorry that I’m not an expert and not enough to answer any technical question.

Enjoy Android ~~~

Share

How to completely disable Magento module

November 12, 2010 14:31

Disable Magento module? Easy, in admin panel, click the menu “System” -> “Configuration” -> “Advanced” in “Advanced” Section, select “disable” for the module you would like to disable, and it’s finish, right?

Sorry but … NO! If you disable the module in admin panel, it will only disable the module output, not the module functionalities. The Observer, cornjob, setting in config.xml are still in use and running.

Then … how to disable the module completely? Well, it’s also easy, you only have to change the ‘active’ tag to false in module XML file storing in ‘/app/etc/modules/YourNameSpace_YourModuleName.xml’:

<?xml version="1.0"?>
<config>
    <modules>
        <YourNameSpace_YourModuleName>
            <active>false</active>
            <codepool>local</codepool>
        </YourNameSpace_YourModuleName>
    </modules>
</config>

or simply rename the file extension (eg. YourNameSpace_YourModuleName.xml_old); or delete this file which is not recommended.

Happy Magento’ing ~

Share

How to create coupon code for fixed amount discount

November 11, 2010 15:55

In your selling period, sometimes you would like to make offer for promotion or festival. Here I will show you how to create a coupon code between a specific period for all products and customers with unlimited usage.

In admin panel, click the menu “Promotions” -> “Shopping Cart Price Rules”, then the list for all coupon codes for all websites are shown. Clicking “Add New Rule” button on right hand side and input the following:

  • in “Rule Information” Section:
    • Rule Name: the name of the discount.
    • Description: description of this coupon code, will not show to customer.
    • Status: always set to “active” if you want this coupon to be used.
    • Websites: only select the store this coupon code will be available.
    • Customer Groups: select all of them by Ctrl + mouse left button.
    • Coupon: select “Specifc Coupon”, and the “Coupon Code” input box will be shown. Please enter the coupon code in that box.
  • in “Action” Section:
    • Apply: select “Fixed amount discount”
    • Discount Amount: input the fixed discount amount
    • in “Labels” Section:
    • In Default Label:
    • Default Rule Label for All Store Views: Please input the wordings that you want to display in the all documents, shopping cart.

After all input finished, click “Save Rule”. This coupon code is available to use. You can check it by adding a product to shopping cart and enter the coupon code.

Happy Magento’ing ~

Share
Comments Off

Shortening Increment ID length for Orders, Invoices, Creditmemo and Shipments

November 8, 2010 11:52

By default, Magento use 8 digits for Orders, Invoices, Creditmemo and Shipments’ increment ID. However, sometime you would like to change the length to meet 3rd party’s system requirement. This can be easily accomplished by:

  • modify the field “increment_pad_length” in the “eav_entity_type” table for whatever entity you’re wanting to modify.
  • modify the field “increment_last_id” in “eav_entity_store” table with the same “entity_type_id” in table “eav_entity_type” which the document type’s increment ID you would like to change. Remember to change all existing records for this “entity_type_id”.

After you changed them, the coming new document will use the new increment ID.

Happy Magento’ing ~

Share

How to get attribute option label in Magento with different store view

October 29, 2010 11:37

In Magento customization, sometimes you may want to get the value of attribute’s option. In below example I will show you how to do it.

Assuming that there is an attribute named “dropdown_field” and contains 4 options label: “Option 1″, “Option 2″, “Option 3″, “Option 4″. The codes below get the option label which is set in product:

// get product Model
$_product = Mage::getModel('catalog/product');

// get the attribute object
$dropdownAttributeObj = $_product->getResource()->getAttribute("dropdown_field");

// if input type for this attribute is dropdown or multi-select
if ($dropdownAttributeObj->usesSource()) {
    // get the option label
    $dropdown_option_label = $dropdownAttributeObj->getSource()->getOptionText($_product->getData('dropdown_field'));
}

How about multiple websites / store / store view? No worry, it’s automatically get the correct label for you; or you can use $this->__() translation method in your template files.

Happy Magento’ing ~

Share
Comments Off

Delete all orders, reset bestseller and most viewed products in Magento

October 28, 2010 09:48

In your Magento development period, you may create many order to test the checkout process. Before the website launch, you should find that there is no “delete” action for orders. What? Is it crazy?

Unfortunately, it’s really crazy (maybe) that you cannot delete order in Magento by default. Like me, there are over hundred orders leave in backend which make our sales confused. In this case, you can execute following SQL statements to delete the ALL orders in MySQL database:

TRUNCATE TABLE {prefix}sales_flat_order;
TRUNCATE TABLE {prefix}sales_order_tax;
TRUNCATE TABLE {prefix}downloadable_link_purchased;
DELETE FROM {prefix}eav_entity_store WHERE entity_type_id IN (SELECT entity_type_id FROM {prefix}eav_entity_type WHERE entity_type_code IN ('order', 'invoice', 'creditmemo', 'shipment'));

Then go to backend, then Reports > Refresh Statistics, select all items and run Refresh Lifetime Statistics.

But wait, most viewed products and bestseller haven’t reset yet. How?

Fine, here is the SQL statement to reset the most viewed products report:

DELETE FROM {prefix}report_event WHERE event_type_id IN (SELECT event_type_id FROM {prefix}report_event_types WHERE event_name IN ('catalog_product_view'));

If you need to delete other reports, you can add the following types to the previous SQL construction in last WHERE statement: ‘sendfriend_product’, ‘catalog_product_compare_add_product’, ‘checkout_cart_add_product’, ‘wishlist_add_product’, ‘wishlist_share’. These types are easily to find in {prefix}report_event_types table.

To truncate Bestsellers you will need to truncate the table ‘sales_bestsellers_aggregated_daily’, ‘sales_bestsellers_aggregated_monthly’ and ‘sales_bestsellers_aggregated_yearly’. Here are the SQL statements to truncate them:

TRUNCATE TABLE {prefix}sales_bestsellers_aggregated_daily;
TRUNCATE TABLE {prefix}sales_bestsellers_aggregated_monthly;
TRUNCATE TABLE {prefix}sales_bestsellers_aggregated_yearly;

That is. It’s not easy job and you may have to get some basic SQL / MySQL knowledge. Of course, using phpMyAdmin is the better way.

Thanks for Magento Support for this help.

Happy Magento’ing ~

Share
Comments Off

Add custom layout in CMS page

October 25, 2010 15:55

You may design some nice templates and layouts for your Magento shopping cart, but you cannot choose these layouts in CMS.

You can add the following XML codes in “global” section in either one of config.xml in any module, but I suggest you to create a new custom module to change it.

For example, if you’ve added a new layout named “Two-Column Right Column Full Layout Pages” in “page.xml”:

<layout>
    ...
    <page_two_columns_right_full translate="label">
        <label>Two-Column Right Column Full Layout Pages</label>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right-full.phtml</template></action>
            <!-- Mark root page block that template is applied -->
            <action method="setIsHandle"><applied>1</applied></action>
        </reference>
    </page_two_columns_right_full>
    ...
</layout>

Then in the config.xml in a new custom module or a existing module:

<global>
    ...
    <page>
        <layouts>
            <two_columns_right_full module="page" translate="label">
                <label>Two-Column Right Column Full Layout Pages</label>
                <template>page/2columns-right-full.phtml</template>
                <layout_handle>page_two_columns_right_full</layout_handle>
            </two_columns_right_full>
        </layouts>
    </page>
    ...
</global>

Then in your backend CMS page, you should be able to choose this new layout.

Happy Magento’ing ~

Share
Comments Off