vendidero / woocommerce-germanized

Adapt WooCommerce to the German Market with Germanized for WooCommerce
https://vendidero.de/woocommerce-germanized
Other
51 stars 41 forks source link

Different result when using `has_term()` on `$product` and `$gzd_product`. #200

Closed mariusmateoc closed 6 months ago

mariusmateoc commented 7 months ago

I'm using the following filter and getting different results when I use has_term() using $product than when I use it with $gzd_product.

Don't know if this is what should happen or is a bug.

// Dynamically recalculate unit prices before retrieving them
add_filter( 'woocommerce_gzd_before_get_unit_price_html', function( $gzd_product ) {
    global $product;

    // when using $product this return true
    if ( has_term( 'leuchten', 'product_cat', $product->get_id() ) ) {
        return;
    }

    // when using $gzd_product this return false
    if ( has_term( 'leuchten', 'product_cat', $gzd_product->get_id() ) ) {
        return;
    }

    $gzd_product->recalculate_unit_price();
}, 10 );
dennisnissle commented 6 months ago

Well, obviously the global $product you are referring to does not match the actual product instance injected into the filter. If you wanted to get the original $product from the $gzd_product you could use:

$wc_product = $gzd_product->get_wc_product();

Using the global $product doesn't make sense within the filter.

Best, Dennis