• Resolved alx359

    (@alx359)


    I’m sporadically getting this entry in the logs:
    PHP Notice: Trying to get property of non-object in \plugins\wc-abandoned-carts-by-small-fish-analytics\class-sfa-abandoned-carts-recorder.php on line 96

    The offending part happens in this check:

    //Don't create a record unless a user is logging in with something in their cart
    if (!$woocommerce->cart->cart_contents) {
        return;
    }

    Not sure why’s this happening to propose a fix. Of course we could use a slightly different syntax like
    if ( WC()->cart->get_cart_contents_count() == 0 )
    but this is just masking the issue, as an empty cart should be handled gracefully anyway. Please advise.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author smallfishes

    (@smallfishes)

    Hey @alx359

    Thanks for reaching out with another bug report.

    I spent the last half hour with my debugger turned on trying various methods to get to that line of code without a cart record existing. I can’t even when trying really round about ways such as clearing cookies right before creating an account in an incognito browser.

    Even if I can’t replicate it I still believe the fix is to just throw a check in that if statement like below. If a cart doesn’t exist, there is nothing to record. As the user navigates through the store and adds something to their cart the plugin will begin to record.

    In the copy of code on my Google Drive below I’ve changed the code to be as follows. Would you give it a try for me?

    //Don’t create a record unless a user is logging in with something in their cart
    if (!isset($woocommerce->cart) && !$woocommerce->cart->cart_contents)

    https://drive.google.com/file/d/1UJdedausdq6KQ7SOWQ-RIGrGh7D7jyIu/view?usp=sharing

    Also I haven’t forgot your request about the Windows money_format issue.

    Thread Starter alx359

    (@alx359)

    Thanks, I tried almost the same piece of code before reporting, as it’s apparent it should address directly the issue, but just couldn’t believe that such rather crude workaround was the proper WP way of doing this!

    Think the proposed fix has a typo though. If $woocommerce->cart isn’t set it will throw the same PHP notice when trying to access cart_contents after the &&. This should work instead:

    if(!isset($woocommerce->cart) || !$woocommerce->cart->cart_contents)

    Plugin Author smallfishes

    (@smallfishes)

    Oops you’re right! I’ve updated the test version on my Google Drive.

    How often were you seeing the notices in your log file?

    Thread Starter alx359

    (@alx359)

    Not sure. I’m syncin’ localhost (wamp) with staging (serverpilot Ubuntu) from time to time. Perhaps cart objects get messed up somehow when changing platforms? Can’t recall where I saw it first. I won’t apply the fix for now and keep an eye at the logs to try to narrow down the culprit and report back.

    Thread Starter alx359

    (@alx359)

    Just uploaded a new version of the project on stage (using AIO WP Migration) and this time got a similar issue from the imported cart but with another plugin. Just checked both WP configs have different salts, so I suspect there might lay the culprit of these errors. Will test this theory with future deployments. In the meantime, it’s probably an edge case with no enough merit ATM to fix your side.

    Plugin Author smallfishes

    (@smallfishes)

    Sounds good. Please follow up if you need more help with this in the future.

    Thread Starter alx359

    (@alx359)

    Think finally found the culprit and why it was sporadic: Flatsome’s Maintenance mode. It seems to break havoc with cart sessions when enabled.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘PHP Notice: Trying to get property of non-object’ is closed to new replies.