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

Getting PHP Fatal Error on getPrice() #5519

Closed
hvohra opened this issue Jul 7, 2016 · 19 comments
Closed

Getting PHP Fatal Error on getPrice() #5519

hvohra opened this issue Jul 7, 2016 · 19 comments
Labels
bug report Component: Catalog Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Comments

@hvohra
Copy link

hvohra commented Jul 7, 2016

Steps to reproduce

I have installed new magento 2.1 with mysql 5.6 , php7 on ubunu 15. Everything works except sometimes when I add a prod to cart , checkout, or login, I get the following error:

[Thu Jul 07 07:26:27.251375 2016] [:error] [pid 5731] [client 127.0.0.1:59868] PHP Fatal error: Uncaught Error: Call to a member function getPrice() on null in /var/www/magento2/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php:43\nStack trace:\n#0 /var/www/magento2/vendor/magento/module-catalog/Model/Product.php(555): Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price->getPrice(Object(Magento\Catalog\Model\Product\Interceptor))\n#1 /var/www/magento2/var/generation/Magento/Catalog/Model/Product/Interceptor.php(89): Magento\Catalog\Model\Product->getPrice()\n#2 /var/www/magento2/vendor/magento/module-quote/Model/Quote/Address/Total/Subtotal.php(110): Magento\Catalog\Model\Product\Interceptor->getPrice()\n#3 /var/www/magento2/vendor/magento/module-quote/Model/Quote/Address/Total/Subtotal.php(53): Magento\Quote\Model\Quote\Address\Total\Subtotal->_initItem(Object(Magento\Quote\Model\Quote\Address), Object(Magento\Quote\Model\Quote\Item))\n#4 /var/www/magento2/vendor/magento/module-quote/Model/Quote/TotalsCollector.php(265): Magento\Quote\Model\Quote\Address\Total in /var/www/magento2/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php on line 43, referer:

Please help

@tipofloe
Copy link

tipofloe commented Jul 19, 2016

I had the same problem. I flushed all Cache from Magento 2 and then reindexed and now it's working.

@veloraven
Copy link
Contributor

@hvohra thank you for your report.
Could you please provide more specific cases when the issue reproduces? Maybe additional settings, product type, etc. As it may be very hard or impossible to reproduce on other environment the issue which appears "sometimes" only.

@MorganGonzales
Copy link

MorganGonzales commented Aug 18, 2016

Also happens to me. I'm using Magento 2.1. It occurs during viewing cart when one of the items in the cart is set to "Out of Stock".

Replication step:

  1. Add a product to cart.
  2. (While product is on cart) Set added product to "Out Of Stock" in Admin
  3. Going back to store site, go to Cart page (View Cart).

Result:
Fatal error: Uncaught Error: Call to a member function getPrice() on null in /Users/user/Sites/classic-spirit/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php:43 Stack trace: #0 /Users/user/Sites/classic-spirit/vendor/magento/module-catalog/Model/Product.php(555): Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price->getPrice(Object(Magento\Catalog\Model\Product\Interceptor))
#1 /Users/user/Sites/classic-spirit/var/generation/Magento/Catalog/Model/Product/Interceptor.php(89): Magento\Catalog\Model\Product->getPrice()
#2 /Users/user/Sites/classic-spirit/vendor/magento/module-quote/Model/Quote/Address/Total/Subtotal.php(110): Magento\Catalog\Model\Product\Interceptor->getPrice()
#3 /Users/user/Sites/classic-spirit/vendor/magento/module-quote/Model/Quote/Address/Total/Subtotal.php(53): Magento\Quote\Model\Quote\Address\Total\Subtotal->_initItem(Object(Magento\Quote\Model\Quote\Address), Object(Magento\Quote\Model\Quote\Item))
#4 /Users/user/Sites/classic-spirit/vendor/magento/ in /Users/user/Sites/classic-spirit/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php on line 43

@isitnikov
Copy link

isitnikov commented Aug 27, 2016

Internal ticket: MAGETWO-57719

@veloraven veloraven added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development bug report Component: Catalog and removed Progress: needs update labels Aug 31, 2016
@webspeaks
Copy link

Any update on this? I am getting this error very frequently..

@hvohra
Copy link
Author

hvohra commented Sep 19, 2016

I had this issue couple of months ago so and I think I was able to resolve (or get around) this issue by downgrading my php from 7 to 5.6.

@paalsoberg
Copy link

Hi, any update on when this will be fixed as Paypal is unusable if you have configurable products @maksek @veloraven @isitnikov

@antbates91
Copy link

antbates91 commented Sep 23, 2016

👍 on this we are having the exact same issue here using magento 2.1 with mysql 5.6 , php 7.0.8

It happens for us under the following circumstances:

    • A user logs into their account on the front end and adds a product to their cart.
    • They then log out or leave the site, magento saves the cart items .
    • Some time passes and the product then goes out of stock.
    • The user then comes back to log into the site on the front end while this product is still out of stock.
    • The above error is then displayed as Magento tries to load up the saved cart items.

@ifuents
Copy link

ifuents commented Oct 6, 2016

We are having the same issue MhorGonzales describes.
Any update so far?

@mark-netalico
Copy link

I'm also running into this.

@tkacheva
Copy link

Issue has been fixed in current develop branch c966dc1
Please check. Reopen if needed

@hostep
Copy link
Contributor

hostep commented Feb 24, 2017

Also running into this issue, using Magento CE 2.1.4
Don't know how to reproduce, but suddenly we started having this problem.

The above commit mentioned by @tkacheva obviously has nothing to do with this. It was included in Magento 2.1.3, but it doesn't help with this particular problem.

We just fixed it as follows (not sure if this is the correct way, but whatever):

diff --git a/Model/Product/Type/Configurable/Price.php b/Model/Product/Type/Configurable/Price.php
index fe4428cf589..3704b47f516 100644
--- a/Model/Product/Type/Configurable/Price.php
+++ b/Model/Product/Type/Configurable/Price.php
@@ -39,7 +39,7 @@ class Price extends \Magento\Catalog\Model\Product\Type\Price
      */
     public function getPrice($product)
     {
-        if ($product->getCustomOption('simple_product')) {
+        if ($product->getCustomOption('simple_product') && $product->getCustomOption('simple_product')->getProduct()) {
             return $product->getCustomOption('simple_product')->getProduct()->getPrice();
         } else {
             return 0;

@orlangur
Copy link
Contributor

@maghamed, looks like another good place for defensive programming to be applied :)

@peterhirn
Copy link

peterhirn commented Mar 23, 2017

I had to patch two additional files. My cart included a outdated configurable product.

diff --git a/app/code/Magento/ConfigurableProduct/Block/Cart/Item/Renderer/Configurable.php b/app/code/Magento/ConfigurableProduct/Block/Cart/Item/Renderer/Configurable.php
index b401e1a..ab8d764 100644
--- a/app/code/Magento/ConfigurableProduct/Block/Cart/Item/Renderer/Configurable.php
+++ b/app/code/Magento/ConfigurableProduct/Block/Cart/Item/Renderer/Configurable.php
@@ -65,7 +65,7 @@ class Configurable extends Renderer implements IdentityInterface
             self::CONFIG_THUMBNAIL_SOURCE,
             \Magento\Store\Model\ScopeInterface::SCOPE_STORE
         ) == ThumbnailSource::OPTION_USE_PARENT_IMAGE ||
-            !($this->getChildProduct()->getThumbnail() && $this->getChildProduct()->getThumbnail() != 'no_selection')
+            !($this->getChildProduct() && $this->getChildProduct()->getThumbnail() && $this->getChildProduct()->getThumbnail() != 'no_selection')
         ) {
             $product = $this->getProduct();
         } else {
diff --git a/app/code/Magento/ConfigurableProduct/CustomerData/ConfigurableItem.php b/app/code/Magento/ConfigurableProduct/CustomerData/ConfigurableItem.php
index 7614eef..dab847d 100644
--- a/app/code/Magento/ConfigurableProduct/CustomerData/ConfigurableItem.php
+++ b/app/code/Magento/ConfigurableProduct/CustomerData/ConfigurableItem.php
@@ -60,7 +60,7 @@ class ConfigurableItem extends DefaultItem
         );

         $product = $config == ThumbnailSource::OPTION_USE_PARENT_IMAGE
-            || (!$this->getChildProduct()->getThumbnail() || $this->getChildProduct()->getThumbnail() == 'no_selection')
+            || (!$this->getChildProduct() || !$this->getChildProduct()->getThumbnail() || $this->getChildProduct()->getThumbnail() == 'no_selection')
             ? $this->getProduct()
             : $this->getChildProduct();

diff --git a/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable/Price.php b/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable/Price.php
index fe4428c..3704b47 100644
--- a/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable/Price.php
+++ b/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable/Price.php
@@ -39,7 +39,7 @@ class Price extends \Magento\Catalog\Model\Product\Type\Price
      */
     public function getPrice($product)
     {
-        if ($product->getCustomOption('simple_product')) {
+        if ($product->getCustomOption('simple_product') && $product->getCustomOption('simple_product')->getProduct()) {
             return $product->getCustomOption('simple_product')->getProduct()->getPrice();
         } else {
             return 0;

@Pigau
Copy link

Pigau commented Apr 5, 2017

We has the same issue (on magento 2.1.4), and the previous patch work for us, thank @peterhirn.
The problem seem to occur when the children product of a configurable is considered as deleted in module-quote/Model/ResourceModel/Quote/Item/Collection.php:258

@leandrosunrise
Copy link

@peterhirn patch fixed my Issue is this going to be pushed on the next release of magento?

@peterhirn
Copy link

@leandrosunrise as this is just an extended hack based on the patch from @hostep I think someone more familiar with the code should check this and create a pull request.

@stephan-cream
Copy link
Contributor

@peterhirn Although this hack prevents the error from showing it does not fix the issue of the simple product not being set within the protected $_product variable within vendor/magento/module-quote/Model/Quote/Item/Option.php.

Strangely the product option 'simple_product' does exist in the database within the table 'quote_item_option' and holds the correct product id towards the simple product in the value column.
While debugging i noticed that it does get the correct option from that table but never sets the product that is associated to it.

On a side note, i tried changing the stock settings for a Configurable product.
Namely by changing "Manage Stock" from Yes to No as this is what we generally use ourselves.
This somehow solved the issue for this specific product (do note we did several other changes on the product which had no effect) and showed the product properly in the cart.
Strangely once i change the settings back to what they were it will no longer give this error so it is not reliant on this setting.
Hence i believe this goes wrong in the indexing process, but as to why / where i am not yet sure.
Perhaps this information somehow helps others find the actual cause.

@magento-team
Copy link
Contributor

Internal ticket to track issue progress: MAGETWO-72251

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Component: Catalog Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development
Projects
None yet
Development

No branches or pull requests