1

I would like to Set product sale price programmatically in WooCommerce 3 but only for specific products Ids.

Is it possible and how can I target only specific products using this thread code?

I haven't be able to make it work for specific products only.

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Omri Weiss
  • 25
  • 4

1 Answers1

1

To Set only specific product sale price (the products IDs are defined in the 1st function), try this:

// HERE below in the array set your specific product IDs
function specific_product_ids(){
    return array(37, 43, 57); //  <===  <===  <===  <===  Your Product IDs
}

// Generating dynamically the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
    if( ( empty($regular_price) || $regular_price == 0 ) && in_array($product->get_id(), specific_product_ids() ) )
        return $product->get_price();
    else
        return $regular_price;
}


// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
    $rate = 0.8;
    if( ( empty($regular_price) || $regular_price == 0 ) && in_array($product->get_id(), specific_product_ids() ) )
        return $product->get_regular_price() * $rate;
    else
        return $sale_price;
};

// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
    if( $product->is_type('variable') ) return $price_html;

    if( in_array($product->get_id(), specific_product_ids() ) ) {
        $price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display(  $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
    }
    return $price_html;
}

Code goes in function.php file of your active child theme (active theme).

The continuation in:
Set only for specific products the generated sale price in woocommerce cart

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks it worked! I have 2 follow up questions on another question, Can you please answer them? – Omri Weiss Sep 28 '18 at 22:31
  • works almost perfectly :) I have a variable product and want to reduce one variation. on the product page everything looks good, but on the archive it still says "from xxx euro" ... when I manually add a sale price to the variation, this price changes too. so I guess I am missing a filter here ... but which? ! :) thanks!! @loicTheAztec – Ele Oct 11 '19 at 08:14