1
votes

I am using WooCommerce Dynamic pricing, and trying to have it run on my site on all products but excluding those products that are on sale.

Here's the code I tried with, but it's not working at all. Here's the code that WooCommerce Dynamic pricing docs suggest (that breaks my site)

add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );

function is_product_eligible( $eligible, $product, $discounter_name, $discounter_object ) {
    remove_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );

    if ( $product->is_on_sale() ) {
        $eligible = false;
    }

    add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );

    return $eligible;
}

Here is the code I tried and that doesn't break my site but doesn't work:

function is_product_eligible( $eligible, $product, $discounter_name, $discounter_object ) {

    if ( $product->is_on_sale() ) {
        $eligible = false;
    }

    add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );

    return $eligible;
}
1
Still searching for an answer :SErika Johansson

1 Answers

0
votes

After a bit of research here is the solution. The code it self was breaking my site, but it was because my database was corrupt. With a new database the code didn't break my site but didn't work as intended. So with a bit of alteration the code for excluding sale items and their variations:

add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );

function is_product_eligible( $eligible, $product, $discounter_name, $discounter_object ) {
remove_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );

        if ( $product->get_sale_price() ) {
            $eligible = false;
        }

        add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );

        return 

$eligible;
}