Code Not Updating New Arrival Category After Scheduled Cron Job in woo commerce wordpress [closed]
Code Not Updating New Arrival Category After Scheduled Cron Job in woo commerce wordpress [closed]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed yesterday.
Improve this question
I am using this code for wordpress woo commerce
this code automates the process of marking products as "new arrivals" based on their publish date. The update_new_arrivals function runs daily, while the update_new_arrival_category_on_save function is triggered whenever a product is saved. Together, they manage the assignment and removal of the "new-arrival" category for products in a timely and automated manner.
But this code not working automatically and not removing automatically from the new arrrival category.
This code consists of two main functions and some additional actions and hooks related to updating and managing new arrivals for products in a WordPress environment. Let's break down the functionality using terminology:
update_new_arrivals() Function:
Purpose: This function is designed to update the "new arrival" category for products based on their publish date. Process: It uses the WP_Query class to retrieve all products ordered by date, with a limit of the last 15 days. It checks the publish date of each product and calculates the difference in days between the current date and the product's publish date. If a product is published within the last 15 days, it assigns the "new-arrival" category to that product and displays its content using a WooCommerce template. This function is scheduled to run every 24 hours. update_new_arrival_category_on_save($post_id) Function:
Purpose: This function is triggered when a product is saved, and it updates the "new arrival" category based on the product's publish date. Process: It checks if the saved post is of type 'product'. It calculates the days difference between the current date and the product's publish date. If the product is published within the last 15 days, it assigns the "new-arrival" category; otherwise, it removes this category. Scheduled Event:
The wp_schedule_event function is used to schedule the update_new_arrivals function to run daily, starting from the current time. Hooks:
The add_action function is used to associate the update_new_arrivals function with the scheduled event 'update_new_arrivals', ensuring it runs at the specified interval. Another add_action is used to link the update_new_arrival_category_on_save function with the 'save_post' action. This ensures that the product category is updated whenever a product is saved.
`// Function to update new arrivals
function update_new_arrivals() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'date_query' => array(
array(
'column' => 'post_date',
'after' => '-15 days',
),
),
);
$products_query = new WP_Query($args);
if ($products_query->have_posts()) :
$new_arrival_category = get_term_by('slug', 'new-arrival', 'product_cat');
while ($products_query->have_posts()) :
$products_query->the_post();
$publish_date = get_post_time('U', true, get_the_ID());
$current_date = current_time('timestamp');
$days_difference = floor(($current_date - $publish_date) / (60 * 60 * 24));
if ($days_difference <= 15) {
wp_set_object_terms(get_the_ID(), $new_arrival_category->term_id, 'product_cat', true);
wc_get_template_part('content', 'product');
}
endwhile;
wp_reset_postdata();
else :
// Handle the case where there are no posts.
endif;
}
// Function to update category on save
function update_new_arrival_category_on_save($post_id) {
if (get_post_type($post_id) === 'product') {
$new_arrival_category = get_term_by('slug', 'new-arrival', 'product_cat');
$publish_date = get_post_time('U', true, $post_id);
$current_date = current_time('timestamp');
$days_difference = floor(($current_date - $publish_date) / (60 * 60 * 24));
if ($days_difference <= 15) {
wp_set_object_terms($post_id, $new_arrival_category->term_id, 'product_cat', true);
} else {
wp_remove_object_terms($post_id, $new_arrival_category->term_id, 'product_cat');
}
}
}
// Schedule the function to run every 24 hours (daily).
if (!wp_next_scheduled('update_new_arrivals')) {
wp_schedule_event(current_time('timestamp'), 'daily', 'update_new_arrivals');
}
// Hook the function to the scheduled event.
add_action('update_new_arrivals', 'update_new_arrivals');
// Hook the function to save_post action.
add_action('save_post', 'update_new_arrival_category_on_save');
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand