Jump to content
thirty bees forum
  • 0

Adding discount to order throws exception


30knees

Question

In the current 1.0.x bleeding edge (this didn't happen before), adding a discount to the order throws the below exception for me. Does this happen to others, too?

Stack trace:

1. classes/ObjectModel.php:324 source ObjectModelCore->validateFields()
2. classes/order/Order.php:285 source ObjectModelCore->getFields()
3. classes/ObjectModel.php:755 source OrderCore->getFields()
4. controllers/admin/AdminOrdersController.php:1532 source ObjectModelCore->update()
5. classes/controller/Controller.php:197 source AdminOrdersControllerCore->postProcess()
6. classes/Dispatcher.php:837 source ControllerCore->run()
7. administrator/index.php:63 source DispatcherCore->dispatch()

Property Order->total_discounts is not valid

in file classes/ObjectModel.php at line 1037
1034:            $message = $this->validateField($field, $this->$field);
1035:            if ($message !== true) {
1036:                if ($die) {
1037:                    throw new PrestaShopException($message);
1038:                }
1039:
1040:                return $errorReturn ? $message : false;
Edited by 30knees
Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Some thoughts on debugging this:

 - the "total_discounts" field for the order is checked for being a float and that check fails.

 - so it is interesting to know what its contents is.

 - to see that you can change line 1037 to 

throw new PrestaShopException($message." - ".$this->$field);
Edited by musicmaster
Link to comment
Share on other sites

  • 0

@datakickYes, it happens with all orders.

@musicmasterUnfortunately, the whole site doesn't load anymore when I use that code. 😞

@wakabayashiI tried both commas and points and get the error.

Perhaps this helps for further troubleshooting. The discount is registered but not processed, see screenshot. I also get an error thrown when I delete the (not processed) discount voucher from the order. 

Thanks!

image.png

Link to comment
Share on other sites

  • 0

Thanks, @musicmaster.

I think the new code gives me more or less the same error:

Stack trace

1. classes/ObjectModel.php:324 source ObjectModelCore->validateFields()
2. classes/ObjectModel.php:755 source ObjectModelCore->getFields()
3. controllers/admin/AdminOrdersController.php:1518 source ObjectModelCore->update()
4. classes/controller/Controller.php:197 source AdminOrdersControllerCore->postProcess()
5. classes/Dispatcher.php:837 source ControllerCore->run()
6. administrator/index.php:63 source DispatcherCore->dispatch()
1018:     * @throws PrestaShopException
1019:     *
1020:     * @since   1.0.0
1021:     * @version 1.0.0 Initial version
1022:     */
1023:    public function validateFields($die = true, $errorReturn = false)
1024:    {
1025:        foreach ($this->def['fields'] as $field => $data) {
1026:            if (!empty($data['lang'])) {
1027:                continue;
1028:            }
1029:
1030:            if (is_array($this->update_fields) && empty($this->update_fields[$field]) && isset($this->def['fields'][$field]['shop']) && $this->def['fields'][$field]['shop']) {
1031:                continue;
1032:            }
1033:
1034:            $message = $this->validateField($field, $this->$field);
1035:            if ($message !== true) {
1036:                if ($die) {
1037:                    throw new PrestaShopException($message." - ".$this->$field);
1038:                }
1039:
1040:                return $errorReturn ? $message : false;
1041:            }
1042:        }
1043:
1044:        return true;
1045:    }
1046:
1047:    /**
Link to comment
Share on other sites

  • 0

Previously your error message was: 

Property Order->total_discounts is not valid

The purpose of my improvement was to show after that message the content of that total_discounts variable. You didn't show that content in your last post. Even if it was empty at least the dash (-) would be shown.

 

Edited by musicmaster
Link to comment
Share on other sites

  • 0

Yes, sorry! I forgot that.

I get this when trying to add a discount code.

Property OrderInvoice->total_paid_tax_excl is not valid
in file classes/ObjectModel.php at line 1036
And I get this when trying to remove the discount code that was already there.
Property OrderInvoice->total_discount_tax_excl is not valid
in file classes/ObjectModel.php at line 1036
 
Edited by 30knees
Link to comment
Share on other sites

  • 0

I have the same error, happens both on FO and on BO side. 

It only happens in certain cases, not sure when it does and when it not happens.

 

## ThirtyBeesException  
**Property Order->total_paid_tax_excl is not valid**  
 *at line* **1027** *in file* `classes/ObjectModel.php`  
```php  
1022.                }
1023.    
1024.                $message = $this->validateField($field, $this->$field);
1025.                if ($message !== true) {
1026.                    if ($die) {
1027. =>                     throw new PrestaShopException($message);
1028.                    }
1029.    
1030.                    return $errorReturn ? $message : false;
1031.                }
1032.            }
```  
- **ObjectModelCore->validateFields** - [line `322` - `classes/ObjectModel.php`]  
```php  
317.         * @since   1.0.0
318.         * @version 1.0.0 Initial version
319.         */
320.        public function getFields()
321.        {
322. =>         $this->validateFields();
323.            $fields = $this->formatFields(static::FORMAT_COMMON);
324.    
325.            // For retro compatibility
326.            if (Shop::isTableAssociated($this->def['table'])) {
327.                $fields = array_merge($fields, $this->getFieldsShop());
```  
- **ObjectModelCore->getFields** - [line `285` - `classes/order/Order.php`]  
```php  
280.        {
281.            if (!$this->id_lang) {
282.                $this->id_lang = Configuration::get('PS_LANG_DEFAULT', null, null, $this->id_shop);
283.            }
284.    
285. =>         return parent::getFields();
286.        }
287.    
288.        /**
289.         * Add this Order
290.         *
```  
- **OrderCore->getFields** - [line `8` - `override/classes/order/Order.php`]  
```php  
3.    {
4.    	public 	$payment_fee;
5.    	public 	$payment_fee_rate;
6.    	public function getFields()
7.    	{
8. => 		$fields = parent::getFields();
9.    		$fields['payment_fee'] = (float)($this->payment_fee);
10.    		$fields['payment_fee_rate'] = (float)($this->payment_fee_rate);
11.    		return $fields;
12.    	}
13.    }
```  
- **Order->getFields** - [line `748` - `classes/ObjectModel.php`]  
```php  
743.    
744.            if (Shop::checkIdShopDefault($this->def['table']) && !$this->id_shop_default) {
745.                $this->id_shop_default = (in_array(Configuration::get('PS_SHOP_DEFAULT'), $idShopList) == true) ? Configuration::get('PS_SHOP_DEFAULT') : min($idShopList);
746.            }
747.            // Database update
748. =>         if (!$result = Db::getInstance()->update($this->def['table'], $this->getFields(), '`'.pSQL($this->def['primary']).'` = '.(int) $this->id, 0, $nullValues)) {
749.                return false;
750.            }
751.    
752.            // Database insertion for multishop fields related to the object
753.            if (Shop::isTableAssociated($this->def['table'])) {
```  
- **ObjectModelCore->update** - [line `326` - `classes/order/Order.php`]  
 - [1 Arguments]  
```php  
321.         */
322.        public function update($nullValues = true)
323.        {
324.            $this->roundAmounts();
325.    
326. =>         return parent::update($nullValues);
327.        }
328.    
329.        /**
330.         * This function rounds all the decimal properties of this Object
331.         *
```  
```Argument [0]  
1
```  
- **OrderCore->update** - [line `1685` - `controllers/admin/AdminOrdersController.php`]  
```php  
1680.                                $order->total_paid_tax_incl -= $orderCartRule->value;
1681.                                $order->total_paid_tax_excl -= $orderCartRule->value_tax_excl;
1682.                            }
1683.    
1684.                            // Update Order
1685. =>                         $res &= $order->update();
1686.                        }
1687.    
1688.                        if ($res) {
1689.                            Tools::redirectAdmin(static::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
1690.                        } else {
```  
- **AdminOrdersControllerCore->postProcess** - [line `251` - `classes/controller/Controller.php`]  
```php  
246.            if ($this->checkAccess()) {
247.                if (!$this->content_only && ($this->display_header || (isset($this->className) && $this->className))) {
248.                    $this->setMedia();
249.                }
250.    
251. =>             $this->postProcess();
252.    
253.                if (!empty($this->redirect_after)) {
254.                    $this->redirect();
255.                }
256.    
```  
- **ControllerCore->run** - [line `837` - `classes/Dispatcher.php`]  
```php  
832.                if (isset($paramsHookActionDispatcher)) {
833.                    Hook::exec('actionDispatcher', $paramsHookActionDispatcher);
834.                }
835.    
836.                // Running controller
837. =>             $controller->run();
838.            } catch (PrestaShopException $e) {
839.                $e->displayMessage();
840.            }
841.        }
842.    
```  
- **DispatcherCore->dispatch** - [line `63` - `admin/index.php`]  
```php  
58.    if (!isset($_REQUEST['controller']) && isset($_REQUEST['tab'])) {
59.        $_REQUEST['controller'] = strtolower($_REQUEST['tab']);
60.    }
61.    
62.    // Prepare and trigger admin dispatcher
63. => Dispatcher::getInstance()->dispatch();
```  

After adding we get the error, then the discount is shown in the order but not working.

@musicmaster was your solution a fix or a work around or?

Link to comment
Share on other sites

  • 0

Its very irritating as when it happens in FO the order comes not through in BO when the customer comes back from payment provider. So the order is not seen in the orderlist and still payed. In cases it is not 0.00.

Link to comment
Share on other sites

  • 0
19 hours ago, Smile said:

Its very irritating as when it happens in FO the order comes not through in BO when the customer comes back from payment provider. So the order is not seen in the orderlist and still payed. In cases it is not 0.00.

I dont understand this. Sounds to me like a different problem. 

In general TB often forces a payment in order to add an order. Since we ship a lot of unpaid orders. I wanted to create orders with payment equal 0.00. This was not possible as the order wasn't created. That's why I assume the same problem here with the discount. 

Similar stuff was discussed here: https://github.com/thirtybees/thirtybees/issues/460

Link to comment
Share on other sites

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
×
×
  • Create New...