Search code examples
phpwordpressdatewoocommerce

Set Product sale price conditionally based on custom fields in Woocommerce


In Woocommerce, I've set two custom fields in my products:

  1. iadi_price for a custom sale price.
  2. iadi_date for a custom date.

I would like to update the sales price if there is less than 3 days between the specified date and today.

I wrote the following code:

function fiyatidegistir() {
global $product;
$bugun = time();
$yenifiyat = get_post_meta($product->ID, 'iadi_price', true); //new sale price
$kgn = get_post_meta($product->ID, 'iadi_date', true); // date
if(!empty($kgn)) {
    $kalan = $kgn - $bugun;
    $dakika = $kalan / 60;
    $saat = $dakika / 60;
    $gun = $saat / 24;
    $yil = floor($gun/365);
    $gun_farki = floor($gun - (floor($yil) * 365));
    if($gun_farki<4 && !empty($yenifiyat)) {
        update_post_meta($product->ID, '_price', $yenifiyat);
    }
}
}
add_action( 'woocommerce_before_main_content', 'fiyatidegistir'); 

But it doesn't work and nothing happen.

What am I doing wrong? How can I change a product sale price programmatically based on a price and date custom fields as explained?


Solution

  • To do it that way is not a good idea as:

    • How you will manage the products that have been updated to avoid multiple successive prices updates.
    • Using that way, it will just kill your web site (to many processes)
    • Your products will not be on sale as you update the active price
    • You will not be able to revert back to the initial price when the period is finished.

    Also your code is full of mistakes regarding WC_Products in Woocommerce and regarding date calculations. Last thing, when you write code, is better to name variables and functions in english, to comment your code in english too as anybody can understand it.

    Instead try the following that will work for simple products, displaying a sale price for the related products when your conditions match (date and custom price):

    add_filter( 'woocommerce_product_get_price', 'conditional_product_sale_price', 20, 2 );
    add_filter( 'woocommerce_product_get_sale_price', 'conditional_product_sale_price', 20, 2 );
    function conditional_product_sale_price( $price, $product ) {
        if( is_admin() ) return $price;
    
        $new_price = get_post_meta( $product->get_id(), 'iadi_price', true ); //new sale price
        $date      = get_post_meta( $product->get_id(), 'iadi_date', true ); // date
    
        if( ! empty($date) && ! empty($new_price) ) {
            $date_time      = (int) strtotime($date); // Convert date in time
            $now_time       = (int) strtotime("now"); // Now time in seconds
            $one_day        = 86400; // One day in seconds
    
            // Calculate the remaining days
            $remaining_days = floor( ( $date_time - $now_time ) / $one_day );
    
            if( $remaining_days >= 0 && $remaining_days < 4  )
                $price = $new_price;
        }
        return $price;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    enter image description here