Skip to main content
added 2204 characters in body
Source Link

EDIT: add complete code.

class Customweb_Subscription_IndexController extends Customweb_Subscription_Controller_Abstract {

    /**
     * Apply subscription plan to cart
     */
    public function applyPlanAction(){
        $quote = Mage::getSingleton('checkout/cart')->getQuote();
        
        /**
         * No reason continue with empty shopping cart
         */
        if (!$quote->getItemsCount()) {
            $this->_redirect('checkout/cart');
            return;
        }
        
        $index = $this->getRequest()->getParam('subscription_plan');
        if ($index != '') {
            $subscriptionPlan = Mage::getModel('customweb_subscription/cartPlan')->loadByIndex($index);
        }
        else {
            $subscriptionPlan = null;
        }
        
        $oldSubscriptionPlan = $quote->getSubscriptionPlan();
        
        if ($subscriptionPlan == null && $oldSubscriptionPlan == null) {
            $this->_redirect('checkout/cart');
            return;
        }

        try {
            $quote->setSubscriptionPlan($subscriptionPlan)->collectTotals()->save();
            
            if ($subscriptionPlan != null) {
                Mage::getSingleton('checkout/session')->addSuccess(
                        $this->__('You subscribed to the plan <em>%s</em>.', Mage::helper('core')->htmlEscape($subscriptionPlan->getDescription())));
            }
            else {
                Mage::getSingleton('checkout/session')->addSuccess($this->__('The subscription plan was removed.'));
            }
        }
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('checkout/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('checkout/session')->addError($this->__('Cannot apply the subscription plan.'));
            Mage::logException($e);
        }
        
        $this->_redirect('checkout/cart');
    }
}

EDIT: add complete code.

class Customweb_Subscription_IndexController extends Customweb_Subscription_Controller_Abstract {

    /**
     * Apply subscription plan to cart
     */
    public function applyPlanAction(){
        $quote = Mage::getSingleton('checkout/cart')->getQuote();
        
        /**
         * No reason continue with empty shopping cart
         */
        if (!$quote->getItemsCount()) {
            $this->_redirect('checkout/cart');
            return;
        }
        
        $index = $this->getRequest()->getParam('subscription_plan');
        if ($index != '') {
            $subscriptionPlan = Mage::getModel('customweb_subscription/cartPlan')->loadByIndex($index);
        }
        else {
            $subscriptionPlan = null;
        }
        
        $oldSubscriptionPlan = $quote->getSubscriptionPlan();
        
        if ($subscriptionPlan == null && $oldSubscriptionPlan == null) {
            $this->_redirect('checkout/cart');
            return;
        }

        try {
            $quote->setSubscriptionPlan($subscriptionPlan)->collectTotals()->save();
            
            if ($subscriptionPlan != null) {
                Mage::getSingleton('checkout/session')->addSuccess(
                        $this->__('You subscribed to the plan <em>%s</em>.', Mage::helper('core')->htmlEscape($subscriptionPlan->getDescription())));
            }
            else {
                Mage::getSingleton('checkout/session')->addSuccess($this->__('The subscription plan was removed.'));
            }
        }
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('checkout/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('checkout/session')->addError($this->__('Cannot apply the subscription plan.'));
            Mage::logException($e);
        }
        
        $this->_redirect('checkout/cart');
    }
}
added more tags
Link
user4351
user4351
Source Link
Loading