woocommerce / woocommerce

A customizable, open-source ecommerce platform built on WordPress. Build any commerce solution you can imagine.
https://woocommerce.com
9.41k stars 10.76k forks source link

Variable data store read_price_data bypassing transients when hooked using add_filter(..., [$this, 'hookname']) #37547

Open midweste opened 1 year ago

midweste commented 1 year ago

Prerequisites

Describe the bug

Bug:

Expected behavior

read_price_data does not recompute prices every time and reads from transient

Actual behavior

read_price_data recomputes price data each time because $price_hash changes every run

First run: image $price_hash array on line 425 of woocommerce/includes/data-stores/class-wc-product-variable-data-store-cpt.php - evaluates to "56a3c2b4e9a476f6abd6426393fe6f74"

Second run - Same exact hooks - should be the same price_hash, read_price_data should not recompute all prices: image $price_hash array on line 425 of woocommerce/includes/data-stores/class-wc-product-variable-data-store-cpt.php - evaluates to "3fa707c32a07b825b5380d9b38095cd6"

Line 274 of never uses cache

        if ( empty( $this->prices_array[ $price_hash ] ) ) {

IMO

Steps to reproduce

Add class and make sure it runs:

<?php

class WooPricehashBug
{
    public $time;

    public function __construct()
    {
        $this->time = microtime(true);
        add_filter('woocommerce_variation_prices_sale_price', [$this, 'product_sale_price'], 10, 3);
    }

    public function product_sale_price($price, $variation, $product)
    {
        return $price;
    }
}

new WooPricehashBug();

Set a breakpoint on line 274 of woocommerce/includes/data-stores/class-wc-product-variable-data-store-cpt.php Observe transient cache never being used on line 274

WordPress Environment

Not needed

Isolating the problem

github-actions[bot] commented 1 year ago

Hi @midweste,

Thank you for opening the issue! It requires further feedback from the WooCommerce Core team.

We are adding the needs developer feedback label to this issue so that the Core team could take a look.

Please note it may take a few days for them to get to this issue. Thank you for your patience.

jorgeatorres commented 1 year ago

Hi @midweste,

Thanks for reaching out and for all the details you've shared in the issue. We're definitely interested in improving performance, and so we're marking this as a high priority enhancement. Thanks again!

barryhughes commented 1 year ago

May be a good candidate to pair with #37629.