Plugin Directory

Changeset 3833007


Ignore:
Timestamp:
11/29/2026 12:37:54 PM (112 minutes ago)
Author:
jayarajesvar
Message:

1.1.18: mock cart's get_cart_contents() now returns the real line items

1.1.17 stopped the fatal and stopped WBS's exceptions, but it still
silently returned no rate. Grafo_Mock_Cart::get_cart_contents() was
hardcoded to return an empty array, so any shipping method that computes
weight/price by walking the cart's actual line items (rather than
trusting the pre-computed get_cart_contents_weight()/get_subtotal()
totals) saw a cart with zero items and had nothing to match a weight-tier
rule against - no error, just quietly zero rates.

grafo_mock_cart() now takes the already-resolved line items
($packagecontents/$pkg_items - each entry already has a real
WC_Product in 'data' and a 'quantity', from grafo_resolve_product()'s
existing null-filtering) and Grafo_Mock_Cart returns them from
get_cart_contents(), with get_cart_contents_count() summing real
quantities instead of hardcoding 1.

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

Legend:

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

    r3832987 r3833007  
    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.17
     5 * Version: 1.1.18
    66 * Author: Grafo
    77 * Author URI: https://www.grafoflow.com/
    … …  
    8787add_action( 'woocommerce_loaded', 'grafo_load_mock_cart_class' );
    8888
    89 function grafo_mock_cart( $weight = 0, $subtotal = 0 )
    90 {
    91     return new Grafo_Mock_Cart( $weight, $subtotal );
     89function grafo_mock_cart( $weight = 0, $subtotal = 0, $contents = [] )
     90{
     91    return new Grafo_Mock_Cart( $weight, $subtotal, $contents );
    9292}
    9393
    … …  
    9999        $had_cart = !is_null(WC()->cart);
    100100        if (!$had_cart) {
    101             WC()->cart = grafo_mock_cart($package['contents_weight'] ?? 0, $package['contents_cost'] ?? 0);
     101            WC()->cart = grafo_mock_cart($package['contents_weight'] ?? 0, $package['contents_cost'] ?? 0, $package['contents'] ?? []);
    102102        }
    103103
    … …  
    312312        $had_cart = !is_null(WC()->cart);
    313313        if (!$had_cart) {
    314             WC()->cart = grafo_mock_cart($total_weight, $pkg_cost);
     314            WC()->cart = grafo_mock_cart($total_weight, $pkg_cost, $pkg_items);
    315315        }
    316316
  • grafo-for-woocommerce/trunk/includes/class-grafo-mock-cart.php

    r3832987 r3833007  
    1919class Grafo_Mock_Cart extends WC_Cart {
    2020
    21     private $_gw, $_gs;
     21    private $_gw, $_gs, $_contents;
    2222    public $_fees_api = [];
    2323
    24     public function __construct( $w = 0, $s = 0 ) {
     24    public function __construct( $w = 0, $s = 0, $contents = [] ) {
    2525        $this->_gw = $w;
    2626        $this->_gs = $s;
     27        /* $contents mirrors $package['contents']: already shaped like a real
     28         * WC cart-contents array (each entry has 'data' => WC_Product,
     29         * 'quantity', 'line_total', etc. — see grafo_create_order()). Some
     30         * shipping plugins (WBS's PackageConverter, observed via its
     31         * "rates=0, no exception" behavior once the cart/compound-param
     32         * fatals were fixed) compute weight/price by walking
     33         * get_cart_contents() directly rather than trusting
     34         * get_cart_contents_weight()/get_subtotal(); an empty array here
     35         * silently zeroes their calculation instead of throwing, which is
     36         * why this was hard to spot from the logs alone. */
     37        $this->_contents = $contents;
    2738        // parent::__construct() intentionally skipped — hits DB/session.
    2839    }
    … …  
    4051     * 1.1.17, being the first one actually hit). */
    4152    public function get_cart_contents_weight( ...$args )  { return $this->_gw; }
    42     public function get_cart_contents_count( ...$args )   { return 1; }
     53    public function get_cart_contents_count( ...$args )   {
     54        $count = 0;
     55        foreach ( $this->_contents as $item ) {
     56            $count += (int) ( $item['quantity'] ?? 1 );
     57        }
     58        return $count ?: 1;
     59    }
    4360    public function get_displayed_subtotal( ...$args )    { return $this->_gs; }
    4461    public function get_subtotal( ...$args )               { return $this->_gs; }
    4562    public function get_cart_subtotal( ...$args )          { return $this->_gs; }
    46     public function get_cart_contents( ...$args )          { return []; }
     63    public function get_cart_contents( ...$args )          { return $this->_contents; }
    4764    public function is_empty( ...$args )                   { return false; }
    4865    public function needs_shipping( ...$args )             { return true; }
  • grafo-for-woocommerce/trunk/readme.txt

    r3832987 r3833007  
    77WC requires at least: 7.0
    88WC tested up to: 11.1
    9 Stable tag: 1.1.17
     9Stable tag: 1.1.18
    1010License: GPLv2 or later
    1111License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    6767== Changelog ==
    6868
     69= 1.1.18 =
     70* Fix: 1.1.17 stopped the fatal and stopped WBS's exceptions, but it still silently returned no rate. Grafo_Mock_Cart::get_cart_contents() was hardcoded to return an empty array, so any shipping method (like WBS) that computes weight/price by walking the cart's actual line items, rather than trusting the pre-computed get_cart_contents_weight()/get_subtotal() totals, saw a cart with zero items and had nothing to match a weight-tier rule against - no error, just quietly zero rates. The mock cart now holds the real line items (the same product/quantity data already resolved for the order) and returns them from get_cart_contents(), with get_cart_contents_count() summing real quantities instead of hardcoding 1.
     71
    6972= 1.1.17 =
    7073* Fix: the real cause of the fatal error reported on 1.1.15/1.1.16 - nothing to do with when/where the class was declared. Grafo_Mock_Cart::get_cart_subtotal() was declared with no parameters, but WC_Cart::get_cart_subtotal($compound = false) declares one; PHP fatals ("Declaration ... must be compatible with ...") the instant a class with an incompatible override is declared, regardless of timing. Every override in Grafo_Mock_Cart now accepts (...$args) instead of a hand-matched signature, which is unconditionally compatible with any parent signature made of optional parameters - closing off this entire category of mismatch instead of fixing one method at a time as each is discovered.
    … …  
    134137== Upgrade Notice ==
    135138
     139= 1.1.18 =
     140Fixes shipping methods (e.g. WBS) that compute weight/price by walking the cart's line items directly - the mock cart returned an empty item list, so such a method silently found nothing to match and quoted no rate at all.
     141
    136142= 1.1.17 =
    137143Fixes the actual fatal error from 1.1.15/1.1.16: a mismatched method signature on the mock cart class, not a class-loading timing issue. All its overrides are now signature-safe against any WC_Cart version.
Note: See TracChangeset for help on using the changeset viewer.