Skip to content

Getting PHP Fatal Error on getPrice() #10206

@jhruehl

Description

@jhruehl

Seems to have happened while I was testing enabling/disabling child products. Saw it just later in the error.log on our testserver and because our customer got the error cached in firefox.

Preconditions

Magento 2.1.7

Steps to reproduce

hard to reproduce. maybe it helps to deactive/delete a child product in the backend product listing without further steps. then have the given configured product in the wishlist or cart.

Expected result

nothing

Actual result

[11-Jul-2017 08:38:42 UTC] PHP Fatal error: Uncaught Error: Call to a member function getPrice() on null in /srv/httpd/test/src/www/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php:43
Stack trace:
#0 /srv/httpd/test/src/www/vendor/magento/module-catalog/Model/Product.php(554): Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price->getPrice(Object(Magento\Catalog\Model\Product\Interceptor))
#1 /srv/httpd/test/src/www/var/generation/Magento/Catalog/Model/Product/Interceptor.php(89): Magento\Catalog\Model\Product->getPrice()
#2 /srv/httpd/test/src/www/vendor/magento/module-quote/Model/Quote/Address/Total/Subtotal.php(110): Magento\Catalog\Model\Product\Interceptor->getPrice()
#3 /srv/httpd/test/src/www/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\Interceptor))
#4 /srv/httpd/test/src/www/vendor/magento/module-quote/Model/Quote/TotalsCollector.p in /srv/httpd/test/src/www/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php on line 43

  1. [Screenshot, logs]

Fix

The Fix is already given in Issue #5519
#5519

But you closed the issue before actual all possible causes were fixed. I applied the following patch to prevent a future circumstancial call on null in the getPrice() function:

diff --git a/www/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php b/www/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php
index 9c7efd803..db45d8ece 100644
--- a/www/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php
+++ b/www/vendor/magento/module-configurable-product/Model/Product/Type/Configurable/Price.php
@@ -39,7 +39,9 @@ class Price extends \Magento\Catalog\Model\Product\Type\Price
*/
public function getPrice($product)
{

  •    if ($product->getCustomOption('simple_product')) {
    
  •    // REF-MOD START under certain circumstances getPrice is called on null
    
  •    if ($product->getCustomOption('simple_product') && $product->getCustomOption('simple_product')->getProduct()) {
    
  •    // REF-MOD END
           return $product->getCustomOption('simple_product')->getProduct()->getPrice();
       } else {
           return 0;
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fixed in 2.2.xThe issue has been fixed in 2.2 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentbug report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions