0

Is there a way to enable a free shipping for the first order for all of customers? and that is without the need of generating/entering a coupon code.

1 Answer 1

0

Yes, You can do this by creating a new shipping method in magento and checking whether the customer had placed any orders before by using their account.

etc/config.xml

<config>
<global>
    <models>
        <inchoo_shipping>
            <class>Inchoo_Shipping_Model</class>
        </inchoo_shipping>
    </models>
    <helpers>
        <inchoo_shipping>
            <class>Inchoo_Shipping_Helper</class>
        </inchoo_shipping>
    </helpers>
</global>
<default>
    ...
    <carriers>
        <inchoo_shipping>
            <active>1</active>
            <model>inchoo_shipping/carrier</model>
            <title>Inchoo Shipping Carrier</title>
            <sort_order>10</sort_order>
            <sallowspecific>0</sallowspecific>
            <express_max_weight>1</express_max_weight>
        </inchoo_shipping>
    </carriers>
    ...
</default>
...

Model/Carrier.php

class Inchoo_Shipping_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface
{

    protected $_code = 'inchoo_shipping';

    public function getAllowedMethods()
    {
        return array(
            'free' => 'Free delivery',
        );
    }

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        /** @var Mage_Shipping_Model_Rate_Result $result */
        $result = Mage::getModel('shipping/rate_result');

        /** @var Inchoo_Shipping_Helper_Data $expressMaxProducts */
        $isEligible = Mage::helper('inchoo_shipping')->isCustomerEligibleForFreeDelivery();

        if ($isEligible) {
            $result->append($this->_getFreeShippingRate());
        }

        return $result;
    }

    protected function _getFreeShippingRate()
    {
        /** @var Mage_Shipping_Model_Rate_Result_Method $rate */
        $rate = Mage::getModel('shipping/rate_result_method');

        $rate->setCarrier($this->_code);
        $rate->setCarrierTitle($this->getConfigData('title'));
        $rate->setMethod('free');
        $rate->setMethodTitle('Free delivery');
        $rate->setPrice(0);
        $rate->setCost(0);

        return $rate;
    }

}

Helper/Data.php

class Inchoo_Shipping_Helper_Data extends Mage_Core_Helper_Abstract
{

    public function isCustomerEligibleForFreeDelivery()
    {
        if (!Mage::helper('customer')->isLoggedIn()) {
            return false;
        }

        $customer = Mage::helper('customer')->getCustomer();
        $orders = Mage::getResourceModel('sales/order_collection')
            ->addFieldToSelect('*')
            ->addFieldToFilter('customer_id', $customer->getId());
        $orders->setPageSize(1);
        if ($orders->count() == 0) {
            return true;
        }

        return false;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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