Files
szkoleniauryzaj.pl/cron-products.php
2024-11-10 21:08:49 +01:00

44 lines
1.2 KiB
PHP

<?php
require( dirname( __FILE__ ) . '/wp-load.php' );
$current_date = date('Y-m-d');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$products = new WP_Query( $args );
if ( $products->have_posts() ) {
echo 'Data serwera: ' . $current_date . '<br /><br /><br />';
while ( $products->have_posts() ) {
$products->the_post();
$product_id = get_the_ID();
$product_title = get_the_title( $product_id );
$acf_field_value = get_field( 'end_date_of_the_offer', $product_id );
$acf_date_object = DateTime::createFromFormat('Y-m-d', $acf_field_value);
if ($acf_date_object !== false) {
$acf_field_value = $acf_date_object->format('Y-m-d');
echo 'Product ID: ' . $product_id . '<br/>';
echo 'Product Title: ' . $product_title . '<br/>';
echo 'Końcowa data oferty: ' . $acf_field_value . '<br />';
echo '-----------------------------------------------------<br/>';
if($acf_field_value && $acf_field_value < $current_date) {
wp_update_post(array(
'ID' => $product_id,
'post_status' => 'draft',
));
delete_field('end_date_of_the_offer', $product_id);
}
}
}
wp_reset_postdata();
}
?>