WordPress WooCommerce: Automate Updating 'New Arrival' Category Based on Product Publish Date [closed]
WordPress WooCommerce: Automate Updating 'New Arrival' Category Based on Product Publish Date [closed]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 20 hours ago.
Improve this question
I am using this code for wordpress woo commerce
what this code doing :-
This code assigning new arrival category to the product which is lesser then 15 days from the publish date.
But it is not working if the product excess the 15 days from the publis hdate and not unassigning new arrival cateogry.
Expected output from the code is :-
Assign "new-arrival categor" if the product publish date is less then 15 days (count days from current date). & if satisfy the condition then assign "new-arrival" category.
If the any product excess the 15 days from the publish date then unassign the "new-arrival" category.
This process should automated without human intervantion and update new-arrival list on daily basis.
the issue where the code is not unassigning the "new-arrival" category for products that have been published for more than 15 days. Clearly state the expected behavior and describe any error messages or unexpected outcomes. Also, mention that the code is scheduled to run daily using wp_schedule_event
`// 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');
}
Explain below code use only terminology abd text dont use code
also a brief of what this code can do
// 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