Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Notice] Undefined property: OrderDiscountModifier::$Amount when adding to cart #93

Closed
adrian-stein opened this issue Mar 20, 2019 · 13 comments

Comments

@adrian-stein
Copy link

I am using "silvershop/core": "1.3.0" and "silvershop/discounts": "1.2.0", old I know but it works for my client.

I just upgraded the server from PHP 7.0 to 7.1 and now it shows this error when I try and add a product to the cart. See the full error below. Any ideas what might be gong on. If i switch back to 7.0 the site runs fine.

[Notice] Undefined property: OrderDiscountModifier::$Amount
GET /products/locks-and-latches/over-centre-catches/adjustable-stainless-steel/ojop-701s-ass-ss-over-centre-catch-non-locking/
Line 3048 in C:\Nimbler\ovesco\framework\model\DataObject.php

Source
3039 $val = get_class($this);
3040 return DBField::create_field('Varchar', $val, $fieldName);
3041
3042 } else if(array_key_exists($fieldName, self::$fixed_fields)) {
3043 return DBField::create_field(self::$fixed_fields[$fieldName], $this->$fieldName, $fieldName);
3044
3045 // General casting information for items in $db
3046 } else if($helper = $this->db($fieldName)) {
3047 $obj = SS_Object::create_from_string($helper, $fieldName);
3048 $obj->setValue($this->$fieldName, $this->record, false);
3049 return $obj;
3050
3051 // Special case for has_one relationships
3052 } else if(preg_match('/ID$/', $fieldName) && $this->hasOneComponent(substr($fieldName,0,-2))) {
3053 $val = $this->$fieldName;
3054 return DBField::create_field('ForeignKey', $val, $fieldName, $this);
Trace
DataObject->dbObject(Amount)
DataObject.php:2681
DataObject->setField(Amount,0)
OrderDiscountModifier.php:50
OrderDiscountModifier->getDiscount()
OrderDiscountModifier.php:82
OrderDiscountModifier->getAmount()
ViewableData.php:120
ViewableData->__get(Amount)
DataObject.php:3048
DataObject->dbObject(Amount)
DataObject.php:2681
DataObject->setField(Amount,0)
OrderDiscountModifier.php:50
OrderDiscountModifier->getDiscount()
OrderDiscountModifier.php:27
OrderDiscountModifier->value(30)
OrderModifier.php:77
OrderModifier->modify(30)
OrderTotalCalculator.php:33
OrderTotalCalculator->calculate()

@wilr
Copy link
Contributor

wilr commented Mar 25, 2019

Can you try 1.3 of Discounts?

@adrian-stein
Copy link
Author

I just tried that and it gives the same error.

@adrian-stein
Copy link
Author

@wilr any other advise or help?

@wilr
Copy link
Contributor

wilr commented Mar 31, 2019

@adrian-stein What is line 50 of your OrderDiscountModifier?

The setField function might be wrong so you could try changing to $this->dbObject('Amount')->setValue($value) rather than $this->setField('Amount', $value);

@adrian-stein
Copy link
Author

https://github.com/silvershop/silvershop-discounts/blob/1/code/modifiers/OrderDiscountModifier.php#L45 is the line causing issues.

It is really line 45 that is causing issues. I had some debug lines in the code to see if I could see what was happening which changed the error line.

so $this->setField('Amount', $amount); inside of getDiscount()

I changed it to $this->dbObject('Amount')->setValue($amount); and it still does the same thing.

@wilr
Copy link
Contributor

wilr commented Apr 1, 2019

Amount should be a database field (https://github.com/silvershop/silvershop-core/blob/1/code/model/OrderModifier.php#L14) but it looks like it is missing from the database. Perhaps put in some breakpoints like you have around line 45 and investigate $this->record to see why $Amount is missing or $this->hasField('Amount') is failing.

@adrian-stein
Copy link
Author

Yes Amount is a database field. and shows up as a field when I do $this->hasField('Amount') or even $this->hasDatabaseField('Amount'). I can also see it in phpMyAdmin as a field on the OrderModifier table. Doing $this->record also shows the record and shows as having Amount.

Could it have something to do with the fact that OrderModifier has a function named 'Amount' and for some reason it is getting confused between that and the DB value, despite the fact that the function just calls $this->Amount?

Doing $this->dbObject('Amount) gives the error but if I try another field such as Type $this->dbObject('Type) it does not produce the error and returns back the object.

I even tried adding a new field named AmountTest and tried doing ->dbObject on that and it returned back a valid response. Its really strange.

@adrian-stein
Copy link
Author

@wilr any more bright ideas? Is this issue just or me or do others have issues with this module and PHP 7.1?

@wilr
Copy link
Contributor

wilr commented Apr 9, 2019

I've setup a test project on PHP 7.1.23 using the below.

"silvershop/core": "^1.3",
"silvershop/discounts": "^1.2",

And I can add a product fine to the cart. Perhaps could you provide more detail on your discount that you have setup? Any other extensions or custom code?

@wilr
Copy link
Contributor

wilr commented Apr 9, 2019

@adrian-stein Try the following patch in the discounts module.

diff --git a/code/modifiers/OrderDiscountModifier.php b/code/modifiers/OrderDiscountModifier.php
index 24dd2bb..8811998 100644
--- a/code/modifiers/OrderDiscountModifier.php
+++ b/code/modifiers/OrderDiscountModifier.php
@@ -42,7 +42,7 @@ class OrderDiscountModifier extends OrderModifier {
         $calculator = new Shop\Discount\Calculator($order, $context);
         $amount = $calculator->calculate();

-        $this->setField('Amount', $amount);
+        $this->dbObject('Amount')->setValue($amount);

         return $amount;
     }
@@ -73,10 +73,6 @@ class OrderDiscountModifier extends OrderModifier {
         );
     }

-    public function getAmount() {
-        return $this->getDiscount();
-    }
-
     public function ShowInTable() {
         return $this->Amount() > 0;
     }

@adrian-stein
Copy link
Author

@wilr If I remove the getAmount() function I can add stuff to the cart. I am not sure if all discounts and what not work correctly with that removed though.

If I just replace $this->setField('Amount', $amount); with $this->dbObject('Amount')->setValue($amount); it still doesnt work.

In terms of other shop modules I use the following

    "silvershop/core": "1.3.0",
    "silverstripe/silverstripe-omnipay": "2.0-alpha as 1.1",
    "silvershop/shipping": "0.3.0",
    "silvershop/discounts": "1.3.0",

    "burnbright/silverstripe-shop-comparison": "1.0.0",

    "markguinn/silverstripe-shop-search": "1.2.x-dev",
    "markguinn/silverstripe-shop-extendedpricing": "1.1.1",
    "markguinn/silverstripe-shop-extendedimages": "1.0.x-dev",
    "markguinn/silverstripe-ajax": "1.0.0",
    "markguinn/silverstripe-shop-ajax": "dev-master",

    "milkyway-multimedia/ss-shop-recommended": "dev-master"

I have 4 discounts in the database but none are active. Does having none active discounts affect it, or do they not do anything if they are set like that.

@wilr
Copy link
Contributor

wilr commented Apr 9, 2019

I tested it with the getAmount removed and it seemed to calculate the amount fine. Will submit a PR and see what tests fail tonight but I think that's your way to go 👍

@adrian-stein
Copy link
Author

Awesome. Thank you very much.

wilr added a commit that referenced this issue Apr 10, 2019
@wilr wilr closed this as completed Apr 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants