How to auto draft WooCommerce product on specific date? - woocommerce

I have a custom date field, added with ACF, for each of my products at which point the product status should change to draft.
I know there are a bunch of schedular and count time plugins, but it comes with a bell and whistle I don't need. Is there a simple way to achieve this
Thank you

You can use WorsPress CRON. You can use wp_schedule_event. You have to get all products and get your ACF field to compare against today or current date then use the wp_update_post function to update posts status. try the below code.
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'check_daily_for_change_product_status' ) ) {
wp_schedule_event( time(), 'daily', 'check_daily_for_change_product_status' );
}
// Hook into that action that'll fire every three minutes
add_action( 'check_daily_for_change_product_status', 'check_daily_for_change_product_status_func' );
function check_daily_for_change_product_status_func() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$get_products = new WP_Query( $args );
if( $get_products->have_posts() ){ while ( $get_products->have_posts() ) { $get_products->the_post();
$draft_date = strtotime( get_field('keyname', get_the_ID() ) );
$current_date = time();
if( $current_date >= $draft_date ){
$my_post = array(
'ID' => get_the_ID(),
'post_status' => 'draft',
);
wp_update_post( $my_post );
}
} wp_reset_postdata(); }
}

Related

Bulk update all taxonomy terms in wordpress

My custom post type "references" has a custom field called "references_count". It has a numeric value.
I have an custom taxonomy called "country" with a custom field called "country_count" for the terms.
Background:
The custom post type "references" saves cities with a number of clients in this city. This value is saved in the field "references_count". In the custom taxonomy there are countries. For each country, there is a total number of references.
Example:
In the city of "Berlin" there are 3 clients. In the city of "Munich" there are 2 clients. The taxonomy term "Germany" includes the sum of all cities in this country. So the value of "country_count" in this example for the taxonomy term "Germany" is 5, being the sum of the references of each city.
I wrote this code which is working, if I'm saving each individual taxonomy term.
add_action( 'edited_country', 'update_counter_for_countries', 10, 2 );
function update_counter_for_countries( $term_id ) {
// Get posts with term
$args = array(
'post_type' => 'reference',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'country',
'field' => 'term_id',
'terms' => $term_id
)
)
);
$the_query = new WP_Query( $args );
// sum values in posts
$sumTerm = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$number = get_field( 'references_count', get_the_ID() );
$sumTerm = $sumTerm + $number;
}
}
wp_reset_postdata();
// update field in term
update_field( 'country_count', $sumTerm, 'country'.'_'.$term_id );
}
Problem:
I have more than 100 countries (taxonomy terms), so I have to save each term individually to get things going.
What I am looking for: Is there a way to update / save all custom taxonomy terms at once, so I don't have to update each term seperately? I checked out a lot of plugins, but couldn't find any plugin which gives the possibility of "bulk edit" or "bulk save" taxonomy terms. I would prefer a solution without plugin if possible. I am very grateful for any hint, thank you very much.
You can use this code to update all terms in one go.
Just make sure to backup your database in case needed.
This code will loop through all the terms and will only run once. after that you can remove this code.
Just to make this code run only on your IP, change 111.111.111.111 to your IP ADDRESS.
if($_SERVER["REMOTE_ADDR"]=='111.111.111.111'){
//run only my ip
add_action("init","update_all_terms_in_one_go");
}
function update_all_terms_in_one_go(){
session_start();
if(isset($_SESSION['all_terms_updated']) && $_SESSION['all_terms_updated'] == "done"){
return;
}
$taxonomy = "country";
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
]);
foreach ($terms as $term) {
update_counter_for_countries( $term->term_id );
}
$_SESSION['all_terms_updated'] = "done";
echo "ALL TAXONOMY TERMS UPDATED";
die();
}
function update_counter_for_countries( $term_id ) {
// Get posts with term
$args = array(
'post_type' => 'reference',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'country',
'field' => 'term_id',
'terms' => $term_id
)
)
);
$the_query = new WP_Query( $args );
// sum values in posts
$sumTerm = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$number = get_field( 'references_count', get_the_ID() );
$sumTerm = $sumTerm + $number;
}
}
wp_reset_postdata();
// update field in term
update_field( 'country_count', $sumTerm, 'country'.'_'.$term_id );
}
I wanted to have a neat admin page with a button to bulk update all my taxonomy terms. The process should work using AJAX so it can take a while without confusing the user. There should be status messages.
So in the first step I added a new admin page to the wordpress backend.
add_action( 'admin_menu', array( $this, 'my_admin_page' ) );
function my_admin_page() {
add_menu_page(
__('Bulk Terms'), // page title
__('Bulk Terms'), // menu title
'manage_options', // user capabilities
'options-page', // menu slug
'my_output_function', // output function
'dashicons-admin-generic', // menu icon
77 // menu position
);
}
In the output function I put a form with a button to bulk update terms. And some status messages.
function my_output_function() {
echo '<div class="wrap">';
echo '<form action="admin.php?page=options-page" method="post">';
wp_nonce_field( 'ajax_validation', 'nonce' ); // security feature
submit_button('Update Terms', 'primary', 'submitOptions', false);
echo '</form>';
echo '<div id="processing" style="display:none;">Please wait</div>';
echo '<div id="error" style="display:none;">Something went wrong</div>';
echo '<div id="success" style="display:none;">Done!</div>';
echo '</div>';
}
In the second step I had to enqueue script file for ajax calls:
add_action( 'admin_enqueue_scripts', 'my_ajax_scripts' );
function my_ajax_scripts() {
// Check if on specific admin page
global $pagenow;
if (( $pagenow == 'admin.php' ) && ($_GET['page'] == 'options-page')):
wp_enqueue_script( 'ajaxcalls', plugin_dir_url( __FILE__ ).'/js/ajax-calls.js', array('jquery'), '1.0.0', true );
wp_localize_script( 'ajaxcalls', 'ajax_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajaxnonce' => wp_create_nonce( 'ajax_validation' )
) );
endif;
}
And create the function to bulk update all my taxnomy terms:
function options_page_action() {
$taxonomy = "country";
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
]);
foreach ($terms as $term) {
$term_id = $term->term_id;
// Get posts with term
$args = array(
'post_type' => 'reference',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'country',
'field' => 'term_id',
'terms' => $term_id
)
)
);
$the_query = new WP_Query( $args );
// sum values in posts
$sumTerm = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$number = get_field( 'references_count', get_the_ID() );
$sumTerm = $sumTerm + $number;
}
}
wp_reset_postdata();
// update field in term
update_field( 'country_count', $sumTerm, 'country'.'_'.$term_id );
}
$result = array( 'status' => 'success' ); // create response
wp_send_json_success( $result ); // send response
wp_die(); // close ajax request
}
As the third step, in my ajax_calls.js file I take the click event to call the function for updating the taxonomy terms using ajax.
$( '#submitOptions' ).click( function(event) {
event.preventDefault();
$('#submitOptions').css('cssText','display:none;');
$('#processing').css('cssText','display: block;');
$.ajax({
type: 'POST',
url: ajax_object.ajaxurl,
data: {
action: 'options_page_action',
nonce: ajax_object.ajaxnonce
},
success: function( response ) {
if( response['data']['status'] == 'success' ) {
$('#processing').css('cssText','display:none;');
$('#success').css('cssText','display:block;');
}
},
error: function() {
$('#processing').css('cssText','display:none;');
$('#error').css('cssText','display:block;');
}
});
});
There are messages indicating that the function is running and it show messages when it's done or has an error. This way the user will always know what's going on when bulk updating terms.

Change the status of orders automatically

I need to change the status of my orders to (Cancelled) automatically after 7 days if it is not processed.
I need to change the status from: New Request to Cancelled.
Or
I need to change the status from: Processing to Cancelled.
Thanks
Adnan
I did that with the help of this code:
function autocancel_wc_orders(){
$query = ( array(
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
'status' => array( 'wc-pending', 'wc-ywraq-new', 'wc-ywraq-
pending')
) );
$orders = wc_get_orders( $query );
foreach( $orders as $order ){
$date = new DateTime( $order->get_date_created() );
$today = new DateTime();
$interval = $date->diff($today);
$datediff = $interval->format('%a');
if( $datediff > 2 ){
$order->update_status('cancelled', 'Cancelled for missing
payment');
}
}
}
add_action( 'admin_init', 'autocancel_wc_orders' );
I found this answer online in the link:
https://samuelsilva.pt/cancel-woocommerce-not-paid-orders-automatically/
Helpful documentation - https://developer.wordpress.org/reference/classes/wp_query/#date-parameters
https://woocommerce.wp-a2z.org/oik_api/wc_get_order_statuses/
//My server cronjob is targeting wp-cron.php
function ss_cancel_failed_orders() {
$held_duration = 15; //minutes how often my cron job to run per day/hour
$data_store = WC_Data_Store::load('order');
//Change post_status for desired order statuses
//Change date_query before value to desired time compare with modified post date
$unpaid_orders = get_posts(array('posts_per_page' => -1, 'post_type' => 'shop_order', 'post_status' => array('wc-failed','wc-on-hold'), 'date_query' => array(array('before' => '15 minutes ago', 'column'=>'post_modified' ))));
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = wc_get_order( $unpaid_order );
$order->update_status( 'cancelled', __( 'Order has expired.', 'woocommerce' ) ); // Status here is without wc- prefix
}
}
wp_clear_scheduled_hook( 'ssa_cancel_failed_orders' );
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'ssa_cancel_failed_orders' ); // you can remove wp cron and work with server cron only
}
add_action('ssa_cancel_failed_orders', 'ss_cancel_failed_orders');

Update Post Category if ACF Date has passed with Cron Job in WordPress

My case is that we have 2 Post Categories. 1. Upcoming Events and 2.Passed Events.
We create a post with ACF event_end_date which is an ACF Date picker. What we need to do is have a cron job that checks if the event_end_date has passed so it updates the Post Category from Upcoming Events to Passed Events. I just cant make it work. Here is the code if you guys got any input i would be thankful!
/* Scheduled Action Hook */
function check_event_end_date() {
global $post;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => array(33), /*Upcoming Events Category ID*/
'posts_per_page' => -1,
);
$listings = get_posts( $args );
foreach($listings as $post) : setup_postdata($post);
$today = date( 'Ymd' );
$expire = get_field( 'event_end_date', false, false );
if ( $expire > $today ) :
$post_categories="36"; /*Passed Events Category ID*/
$append=false;
wp_set_post_categories( $post_ID, $post_categories, $append );
endif;
endforeach;
}
// Schedule Cron Job Event
if ( ! wp_next_scheduled( 'event_end_date_cron_job' ) ) {
wp_schedule_event( date( 'Ymd' ), 'daily', 'event_end_date_cron_job' );
}
add_action( 'event_end_date_cron_job', 'check_event_end_date' );

How to change status of wordpress post after period of time?

I have a wordpress site where users can upload products. The default product status is pending,however I want to change it to published if it has passed 12 hours after the post_time.
I am using woocommerce.
I´ve tried to use wp_insert_post_data:
function reset_post_date_wpse_121565($data,$postarr) {
if ($data['post_type'] == 'product'){
$data['post_date'] = '2018-06-03 22:50:00';
}
return $data;
}
add_filter('wp_insert_post_data','reset_post_date_wpse_121565');
But it sets the post to published, what can I do to accomplish this auto update?
You can try WordPress Cron run to achieve this,
function myprefix_custom_cron_schedule( $schedules ) {
$schedules['every_tewlve_hours'] = array(
'interval' => 43200, // Every 12 hours
'display' => __( 'Every 12 hours' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
wp_schedule_event( time(), 'every_tewlve_hours', 'myprefix_cron_hook' );
}
///Hook into that action that'll fire every six hours
add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );
//create your function, that runs on cron
function myprefix_cron_function() {
$args = array(
'numberposts' => -1,
'post_type' => 'product',
'post_status' => array( 'pending' ),
'author' => $userid
);
$pending_posts = get_posts( $args );
//update status code
}
Hope this will helps you.
For more example visit,
wp cron demo

Woocommerce product search not checking "product_tag"?

It appears as tho the search functionality for WooCommerce products does not check "product_tag" taxonomy terms, nor SKU field? I added the SKUs as product tags to their respective products, but it still returns nothing when I search for the SKU.... How do I make the search functionality check product_tag terms? I have tried many many many things from adding tax_query to pre_get_post filter, to a whole new WP_Query loop, it just fails to search product_tags for some reason....so what is the point in a Product Tag???
I've tried the code from woo and others to search SKU with no results. However, the plugin search-everything, download from repository, does the job.
I got the solution to search the products by SKU code.
It is too simple just paste the following code in function.php and then you can see the changes in search results.
function search_by_id_only( $search, &$query_vars ) {
global $wpdb, $pagenow;
if ( 'edit.php' == $pagenow || empty($search) ) {
return $search;
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_sku',
'value' => $query_vars->query['s'],
'compare' => 'LIKE'
)
)
);
$posts = get_posts( $args );
if ( empty( $posts ) ) return $search;
$get_post_ids = array();
foreach($posts as $post){
$get_post_ids[] = $post->ID;
}
if ( sizeof( $get_post_ids ) > 0 ) {
$search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
}
return $search;
}
add_filter( 'posts_search', 'search_by_id_only', 999, 2 );

Resources