Plugin Directory

Changeset 3833112


Ignore:
Timestamp:
11/29/2026 01:51:43 PM (less than one hour ago)
Author:
jayarajesvar
Message:

1.1.22: mock WC()->session, null since 1.1.21's native pipeline reads/writes it

Switching to WooCommerce's native calculate_shipping() pipeline (1.1.21)
surfaced 'Call to a member function get() on null' - this time from
WC()->session, which that pipeline commonly reads/writes to cache
calculated rates per package hash. A headless REST request has no
session cookie, so WC()->session is null here, same class of problem
WC()->customer being null caused earlier.

Added a minimal mock (same pattern as the existing cart/customer mocks):
get() always returns the default, set() is a no-op. Rates are simply
recalculated fresh on every request instead of relying on session-based
caching across requests - the correct behavior for a stateless REST
endpoint regardless.

Location:
grafo-for-woocommerce/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • grafo-for-woocommerce/trunk/grafo-for-woocommerce.php

    r3833092 r3833112  
    33 * Plugin Name: Grafo for WooCommerce
    44 * Description: Creates WooCommerce orders and updates delivery addresses, with automatic shipping and fee calculation, from Grafo.
    5  * Version: 1.1.21
     5 * Version: 1.1.22
    66 * Author: Grafo
    77 * Author URI: https://www.grafoflow.com/
    … …  
    137137        }
    138138
     139        /* Ensure WC()->session exists. Now that shipping rates are calculated
     140         * through WooCommerce's own native WC_Shipping::calculate_shipping()
     141         * pipeline (see below), that code path commonly caches calculated
     142         * rates per package hash via WC()->session->get()/->set() -- and in
     143         * a headless REST request there is no session cookie, so WC()->session
     144         * is null here, causing "Call to a member function get() on null".
     145         * This mock never actually caches anything (get() always returns the
     146         * default, set() is a no-op), which just means rates are recalculated
     147         * fresh on every request instead of being cached across requests --
     148         * exactly the right behavior for a stateless REST endpoint. */
     149        $had_session = !is_null(WC()->session);
     150        if (!$had_session) {
     151            WC()->session = new class {
     152                public function get( $key, $default = null ) { return $default; }
     153                public function set( $key, $value ) {}
     154                public function __get( $key ) { return null; }
     155                public function __set( $key, $value ) {}
     156                public function __isset( $key ) { return false; }
     157                public function __unset( $key ) {}
     158                public function has_session() { return false; }
     159                public function get_session_cookie() { return false; }
     160                public function get_customer_id() { return 0; }
     161                public function __call( $name, $args ) { return null; }
     162            };
     163        }
     164
    139165        /* WBS (Weight-Based Shipping) v5.0.9+ reads the cart from $package['cart']
    140166         * rather than from WC()->cart ("User package passed to calculate_shipping()
    … …  
    191217        if (!$had_customer)
    192218            WC()->customer = null;
     219        if (!$had_session)
     220            WC()->session = null;
    193221
    194222    } catch (Throwable $e) {
  • grafo-for-woocommerce/trunk/readme.txt

    r3833077 r3833112  
    77WC requires at least: 7.0
    88WC tested up to: 11.1
    9 Stable tag: 1.1.21
     9Stable tag: 1.1.22
    1010License: GPLv2 or later
    1111License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    6767== Changelog ==
    6868
     69= 1.1.22 =
     70* Fix: switching to WooCommerce's native calculate_shipping() pipeline in 1.1.21 surfaced a new "Call to a member function get() on null" fatal, the same class of error WC()->customer null caused earlier (1.1.x) - this time from WC()->session, which the native pipeline commonly reads/writes to cache calculated rates per package hash. A headless REST request has no session cookie so WC()->session is null here. Added a minimal mock (get() always returns the default, set() is a no-op) so rates are simply recalculated fresh on every request instead of relying on session-based caching, which is the correct behavior for a stateless REST endpoint anyway.
     71
    6972= 1.1.21 =
    7073* Change: shipping rate lookup now goes through WooCommerce's own standard WC()->shipping()->calculate_shipping() pipeline again, instead of the hand-rolled zone-matching-plus-manual-global-method-instantiation loop added in 1.1.9. WBS Legacy's own get_instance_id() override returns -1 specifically to avoid being skipped by WC_Shipping::calculate_shipping_for_package() - it is written to work correctly through this exact native pipeline, including its own is_available() check being called the normal way. This removes all the plugin-specific hand-rolled special-casing that loop accumulated, while keeping every fix made to the mock cart/customer objects (1.1.13-1.1.18) and the $package['cart'] injection (1.1.15) that this pipeline still depends on.
    … …  
    143146== Upgrade Notice ==
    144147
     148= 1.1.22 =
     149Fixes a fatal error ("Call to a member function get() on null") introduced by 1.1.21's switch to WooCommerce's native shipping pipeline, caused by WC()->session being null in a headless REST request.
     150
    145151= 1.1.21 =
    146152Switches shipping rate lookup back to WooCommerce's own native calculation pipeline, which WBS Legacy's own code is specifically written to integrate with, removing the hand-rolled method-loop special-casing added since 1.1.9.
Note: See TracChangeset for help on using the changeset viewer.