Jump to content

Override Of Ordercontroller From Module Doesn't Work Properly


GT ITECH
 Share

Recommended Posts

Hello, i have a little issue with the overriding of the OrderController.php file.

 

I want to change how the $minimal_purchase variable is set, to do that i must override the init() function of the controller.

class OrderController extends OrderControllerCore
{
    public $step;
    const STEP_SUMMARY_EMPTY_CART = -1;
    const STEP_ADDRESSES = 1;
    const STEP_DELIVERY = 2;
    const STEP_PAYMENT = 3;

    /**
     * Initialize order controller
     * @see FrontController::init()
     */
    public function init()
    {
        global $orderTotal;

        parent::init();

        $this->step = (int)Tools::getValue('step');
        if (!$this->nbProducts) {
            $this->step = -1;
        }

        $product = $this->context->cart->checkQuantities(true);

        if ((int)$id_product = $this->context->cart->checkProductsAccess()) {
            $this->step = 0;
            $this->errors[] = sprintf(Tools::displayError('An item in your cart is no longer available (%1s). You cannot proceed with your order.'), Product::getProductName((int)$id_product));
        }

        // If some products have disappear
        if (is_array($product)) {
            $this->step = 0;
            $this->errors[] = sprintf(Tools::displayError('An item (%1s) in your cart is no longer available in this quantity. You cannot proceed with your order until the quantity is adjusted.'), $product['name']);
        }

        // Check minimal amount
        $currency = Currency::getCurrency((int)$this->context->cart->id_currency);

        $orderTotal = $this->context->cart->getOrderTotal();
		
        $query = Db::getInstance()->getRow('SELECT * FROM '._DB_PREFIX_.'currency WHERE id_currency = '.(int)$this->context->cart->id_currency, true, false);
		
		$moabg_cgroups = Customer::getGroupsStatic($this->context->cart->id_customer);
		
		if (empty($moabg_cgroups))
		{
		$minimal_purchase = (float)Configuration::get('PS_PURCHASE_MINIMUM')*(float)$query['conversion_rate'];
		$minimal_purchase = (float)number_format((float)$minimal_purchase, 2, ',', '');
		}
		else
		{
		$arr = array();
		
			foreach ($moabg_cgroups as $field)
			{
				$query2 = Db::getInstance()->getRow('SELECT * FROM '._DB_PREFIX_.'moabg WHERE customer_group = '.(int)$field, true, false);
			
				if (empty($query2))	
				$arr[] = Configuration::get('PS_PURCHASE_MINIMUM');		
				else	
				$arr[] = $query2['amount'];
			
			}
			
		$minimal_purchase = (float)min($arr)*(float)$query['conversion_rate'];
		$minimal_purchase = (float)number_format((float)$minimal_purchase, 2, ',', '');
		}
		
		if ($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS) < $minimal_purchase && $this->step > 0) {
			
            $_GET['step'] = $this->step = 0;
            $this->errors[] = sprintf(
                Tools::displayError('A minimum purchase total of %1s (tax excl.) is required to validate your order, current purchase total is %2s (tax excl.).'),
                Tools::displayPrice($minimal_purchase, $currency), Tools::displayPrice($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS), $currency)
            );
			
        }
		
        if (!$this->context->customer->isLogged(true) && in_array($this->step, array(1, 2, 3))) {
            $params = array();
            if ($this->step) {
                $params['step'] = (int)$this->step;
            }
            if ($multi = (int)Tools::getValue('multi-shipping')) {
                $params['multi-shipping'] = $multi;
            }

            $back_url = $this->context->link->getPageLink('order', true, (int)$this->context->language->id, $params);

            $params = array('back' => $back_url);
            if ($multi) {
                $params['multi-shipping'] = $multi;
            }
            if ($guest = (int)Configuration::get('PS_GUEST_CHECKOUT_ENABLED')) {
                $params['display_guest_checkout'] = $guest;
            }

            Tools::redirect($this->context->link->getPageLink('authentication', true, (int)$this->context->language->id, $params));
        }

        if (Tools::getValue('multi-shipping') == 1) {
            $this->context->smarty->assign('multi_shipping', true);
        } else {
            $this->context->smarty->assign('multi_shipping', false);
        }

        if ($this->context->customer->id) {
            $this->context->smarty->assign('address_list', $this->context->customer->getAddresses($this->context->language->id));
        } else {
            $this->context->smarty->assign('address_list', array());
        }
    }
}

If i make a call to var_dump($minimal_purchase) just before the if statement that checking if the ordered amount is less than the minimal amount required, it show me the good value from the db (140).

 

But as you can see on the screenshot below, Prestashop continue after that to set the variable again with the default value from the configuration table (50) :

 

390574Capture.png

 

It seem like my overriden file is read and executed but just after the original file is executed too :/

 

It's the only one explanation i founded for the moment.

 

Please can anyone help me ?

 

Thanks in advance ^^

Share this post


Link to post
Share on other sites

ModuleFrontController also appears to change this value.  You may need to use a file search tool to locate any other places that touch this variable

    public function initContent()
    {
        if (Tools::isSubmit('module') && Tools::getValue('controller') == 'payment') {
            $currency = Currency::getCurrency((int)$this->context->cart->id_currency);
            $orderTotal = $this->context->cart->getOrderTotal();
            $minimal_purchase = Tools::convertPrice((float)Configuration::get('PS_PURCHASE_MINIMUM'), $currency);
            if ($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS) < $minimal_purchase) {
                Tools::redirect('index.php?controller=order&step=1');
            }
        }
        parent::initContent();
    }

 

Share this post


Link to post
Share on other sites

Ohhhhhh i will see that, thank you :)

 

edit : There is no $minimal_purchase in ModuleFrontController for me (Prestashop 1.6.1.1), and i don't find any other script that set this variable except the original OrderController.

 

The strang thing is if i manually comment the $minimal_purchase definition in the original core controller file and use my module, it seems all work fine and Prestashop use as 100% my override file.

 

It's just bad because it's for a module and i can't tell to people to manually comment a line on their OrderController.php :/

 

Strangely, it work fine with no problem with the OrderOpcController (one page checkout).

 

The unique difference between theses two files is where is the $minimal_purchase definition.

 

For OrderOpcController its in the _getPaymentMethods() function.

 

For the OrderController its in the init() function

Edited by GT ITECH (see edit history)

Share this post


Link to post
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More