How do I run code every 24 hours?

Question

I am writing custom plugin for Woocommerce based bakery. The thing is, that it has stocks based on orders from two days before. Product A is produced in an amount of 500 pieces. Stock is reduced by the number of items ordered 2 days ago ($sum global). I wrote that function, and it works (I guess), BUT still i have to call this function every day at midnight (24:00 / 12 a.m.)

The best solution is cron, I heard that WP has its own crown-like function, that works only if there is traffic.

Do you think that it’s good idea to use WP cron over real cron? There are about 100 people visiting page daily.

Could you please help me edit the code below, to archive these points?

1) Fire function everyday at 24:00
2) Update stock with value inside custom_post_meta auto_restock_value minus orders qty from 2 days before (global variable $sum). This should work (?)
3) Save values and repeat everyday.

Thank you in advance!

$post_id = $_GET['post'];

function update_restock_value() {
    global $post_id;
    $val = get_post_meta( $post_id, 'auto_restock_value', true );
    echo '<div class="option_group">';

    woocommerce_wp_text_input( array(
        'id'          => 'auto_restock_value_id',
        'value'       => $val,
        'type'        => 'number',
        'label'       => 'Auto - uzupełniany stan magazynowy',
        'desc_tip'    => true,
        'name'        => 'auto_restock_value',
        'description' => 'Ta wartość, codziennie o 24:00, nadpisze stan magazynowy danego produktu',
    ) );

    echo '</div>';
}


//register_activation_hook( __FILE__, 'my_custom_cron' );
//add_action( 'my_hourly_event', 'do_this_hourly' );

//function my_custom_cron() {
    //wp_schedule_event( time(), 'hourly', 'update_cart_stock' );
//}

function post_order() {
    $args = array(
        'type' => 'shop_order',
        'status' => 'processing',//zmienić//zmienić na completed
        'return' => 'ids',
        'date_created' => ( date("F j, Y", strtotime( '-2 days' ) ) ),//list orders from 2 days before
    );
    global $sum;
    $orders_ids = wc_get_orders( $args );

    foreach ($orders_ids as $order_id) {//get order Id, and show product qty of this product
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            if($product_id == $_GET['post']) {//If product if is the same as current product, then update global $sum
                $product_qty = $item->get_quantity();
                $sum += $product_qty;
            }
        }
    }
}

add_action('init', 'post_order'); 

function update_cart_meta() {
    global $post_id;
    global $sum;
    echo '<p id="sum">' . $sum . '</p>';//checking if it works
    update_post_meta($post_id, '_stock', $sum);//update stock with orders qty from 2 das before
}

add_action('init', 'update_cart_meta');

function update_custom_meta() {
    global $post_id;
    $custom_value = $_POST['auto_restock_value'];
    update_post_meta($post_id, 'auto_restock_value', $custom_value);//update custom post meta field
}

add_action('save_post', 'update_custom_meta');

EDIT:

Question number 2 – why this action returns zero?

function update_cart_meta() {
    global $post_id;
    global $sum;
    echo '<p id="sum">' . $sum . '</p>';//checking if it works (it works)
    $custom_value = $_POST['auto_restock_value']; //(it works)
    $custom_output = $custom_value - $sum; // doesn't work, return 0
    update_post_meta($post_id, '_stock', $custom_value);//update stock with orders qty from 2 days before
}

add_action('init', 'update_cart_meta');
0
, Slingy 3 years 2020-04-05T08:51:00-05:00 0 Answers 91 views 0

Leave an answer

Browse
Browse