• Resolved SLV

    (@dwnl)


    How can I set this up WITHOUT an extra plugin?

    The regular product price is 100,00
    The sale price is 50,00
    We have 12 in stock

    When these 12 are sold, the sale price must automatically be removed and only the normal price of 100,00 must be used again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Shameem – a11n

    (@shameemreza)

    Hi @dwnl

    You will need to add a custom code snippet to your theme’s functions.php file or use the Code Snippets plugin to add the code. The following code snippet will automatically remove the sale price once the stock reaches zero:

    add_action( 'woocommerce_product_set_stock', 'wc_custom_remove_sale_price', 10, 1 );
    function wc_custom_remove_sale_price( $product ) {
    if ( ! $product->is_type( 'simple' ) ) return;
    
    $stock_quantity = $product->get_stock_quantity();
    $sale_price = $product->get_sale_price();
    
    if ( $stock_quantity <= 0 && ! empty( $sale_price ) ) {
    $product->set_sale_price( '' );
    $product->save();
    }
    }

    Please note that modifying your theme’s functions.php file can cause issues if not done correctly, so I recommend creating a backup of your website before making any changes. Alternatively, you can use the Code Snippets plugin to safely add custom code to your website.

    Thanks!

    Thread Starter SLV

    (@dwnl)

    Hello @shameemreza

    I see: if ( ! $product->is_type( ‘simple’ ) ) return;
    Does this mean it is only functioning with simple products?
    If so, I need it to work with variable products…

    And we have many other products where we do not just in stock quantity.
    So the box for stock quantity is empty or 0 or -5 when that product variation is sold.
    Sale prices come and go. Sometimes we do work with only sale prices when 5 (for example) are in stock after that 5 are sold, the sale price must be removed, other times sale prices stay until we remove them, no matter the stock.

    Does your code work for all this?

    Plugin Support Shameem – a11n

    (@shameemreza)

    You’re correct that the provided code snippet only works for simple products. To make it work with variable products, you can modify the code snippet like below:

    add_action( 'woocommerce_product_set_stock', 'wc_custom_remove_sale_price', 10, 1 );
    function wc_custom_remove_sale_price( $product ) {
    if ( ! $product->is_type( 'simple' ) && ! $product->is_type( 'variation' ) ) return;
    
    $stock_quantity = $product->get_stock_quantity();
    $sale_price = $product->get_sale_price();
    
    if ( $stock_quantity <= 0 && ! empty( $sale_price ) ) {
    $product->set_sale_price( '' );
    $product->save();
    }
    }

    This updated code snippet will work with both simple and variable products. However, it will still only remove the sale price when the stock quantity reaches zero or less. If you need the sale price to be removed after a certain number of products are sold, you would need to implement a custom solution to track the number of products sold and then remove the sale price accordingly.

    Regarding your concern about products with empty or negative stock quantities, the provided code snippet will remove the sale price when the stock quantity is zero or negative, as long as a sale price is set for the product. If you want the sale price to remain for products with empty or negative stock quantities, you can adjust the code snippet by changing the condition in the “if” statement.

    Please remember to create a backup of your website before making any changes to your theme’s functions.php file, or alternatively, use the Code Snippets plugin to safely add custom code to your website.

    Thread Starter SLV

    (@dwnl)

    and what is this custom solution then?

    “If you need the sale price to be removed after a certain number of products are sold, you would need to implement a custom solution to track the number of products sold and then remove the sale price accordingly.”

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Remove sale price and use regular price after set stock is 0’ is closed to new replies.