• Resolved emartens

    (@emartens)


    After upgrading Woocommerce from 7.6.1 to 7.8.0, attempting to use WC()->session->get() to retrieve custom values stored in the Woocommerce session inside a custom theme results in an error:

    Uncaught Error: Call to a member function get() on null in /code/web/wp-content/themes/wvecomm/assets/functions/checkout-functions.php:142

    I realize this is in a custom theme, not the Woocommerce core, and I was able to solve for this issue myself by making sure to instantiate a specific instance of the Woocommerce session handler prior to retrieving the values stored in the session:

    WC()->session = new WC_Session_Handler();

    However, I reviewed the entire changelog for all Woocommerce versions between 7.6.1 and 7.8.0, and I do not see ANY changes related to when and where the Woocommerce session handler class is available. I also do not see any changes that suggest a modification in error reporting which might cause this error to surface after the update.

    I’ve extensively reviewed the file changes in the PR for the upgrade from 7.6.1->7.8.0 and nothing jumps out as to what caused this. Our site uses the Woocommerce session extensively in a custom way, so Im trying to understand the scope of the change and debug. In desperation I’m posting here, hoping someone can help me understand what changed in this latest patch that would effect the way the session handler works in custom themes.

    Thank you very much for your time!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • WC()->session is the current live session and you can access other sessions with WC_Session_Handler() object.

    The error message means that no session object exists. You can check this with isset() before you access it. It could be that no product has been added to the shopping cart yet or that no user is logged in.

    What do the theme developers say about this?

    Thread Starter emartens

    (@emartens)

    I have some additional insights into this report.

    After some additional debugging, I realized the source of this bug comes from an addition in Woocommerce 7.7.0, a change to line 122 of web/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/StoreApi/Schemas/V1/CheckoutSchema.php

    The update replaces this logic:

    ‘enum’ => wc()->payment_gateways->get_payment_gateway_ids(),

    with this logic:

    ‘enum’ => array_values( wp_list_pluck( WC()->payment_gateways->get_available_payment_gateways(), ‘id’ ) ),

    The problem is that this logic calls the get_available_payment_gateways() function which triggers this filter: apply_filters( ‘woocommerce_available_payment_gateways’, $_available_gateways ), and it does it on EVERY REST API call.

    This was a problem for our shop, because we were leveraging the woocommerce_available_payment_gateways filter to add custom payment gateways, and the patch made it so that all our wordpress REST APIs executed logic triggered by this filter (logic which was dependent on the existence of the Woocommerce Session Handler, as per my previous comment).

    I didn’t see any notes describing this change, but it has BIG implications for anyone leveraging the woocommerce_available_payment_gateways filter in a custom way.

    For now our shop will have to patch Woocommerce core until this is fixed. Thank you for your attention to this issue.

    This file “web/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/StoreApi/Schemas/V1/CheckoutSchema.php” does not belong to the WooCommerce plugin itself, but to the “WooCommerce Blocks” plugin: https://woocommerce.com/de-de/products/woocommerce-gutenberg-products-block/
    In the current trunk: https://github.com/woocommerce/woocommerce-blocks/blob/trunk/src/StoreApi/Schemas/V1/CheckoutSchema.php#L124 the line is:

    'enum' => array_values( WC()->payment_gateways->get_payment_gateway_ids() ),

    Which plugin version do you use?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WC()->session->get() errors after 7.8.0 update’ is closed to new replies.