0

I created an extension following Inchoo's tutorial but still confusing at how to toggle the shipping method.

Once customers about to see the shipping method page, I need to check the customer's attribute. If Yes the display my custom shipping method, otherwise hide. This is required for both frontend and admin placing order.

I have a few thoughts but all out of my ability to start. I need some hints about which idea is the most efficient and what files should I created:

  1. Using event: set an event such as before_shipping_method_render(I made up this one), then I can setup a function in Observer.php to remove the shipping method from the collection before rendering but I don't know the event name and how to get the collection.
  2. Rewriting Magento function: rewrite the function that puts all enabled shipping methods together. I can put my condition check function in the iterate then allow my custom shipping method if condition is Yes otherwise vice versa.
0

Update your module's config.xml file with the following details:

<global>
        <blocks>
            <checkout>
                <rewrite>
                    <onepage_shipping_method_available>Inchoo_Shipping_Block_Method</onepage_shipping_method_available>
                </rewrite>
            </checkout>
        </blocks>
<global>

Create a new file Inchoo/Shipping/Block/Method.php, which remove your shipping method:

<?php
class Inchoo_Shipping_Block_Method extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
{
    public function getShippingRates()
    {
        $youCustomAttribute = false;
        // Check if any customer is logged in or not
        if (Mage::getSingleton('customer/session')->isLoggedIn()) {  
            // Load the customer's data
            $customer = Mage::getSingleton('customer/session')->getCustomer();   
            // mange your code to get the customer attribute 
            $youCustomAttribute = $customer->getYourCustomerAttribute();

        }
        $rates = parent::getShippingRates();
        if (array_key_exists('inchoo_shipping', $rates) && $youCustomAttribute!==false) {
            unset($rates['inchoo_shipping']);
        }

        return $rates;
    }
}

For more see here.

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.