In Woocommerce, I've set two custom fields in my products:
iadi_price
for a custom sale price.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?