0

I am PHP newbie. I've got this code, and I want to pass global variable called $sum defined in function post_order to function update_custom_meta. Still no luck. What am I doing wrong? Thank you in advance!

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' ) ) ),
    );

    $orders_ids = wc_get_orders( $args );

    foreach ($orders_ids as $order_id) {
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
        global $sum;
        foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            if($product_id == $_GET['post']) {
                $product_qty = $item->get_quantity();
                $sum += $product_qty;
            }
        }
    }
}    

add_action('init', 'post_order'); 

function update_custom_meta() {
    global $post_id;
    echo $sum;
    $custom_value = $_POST['auto_restock_value'] - $sum;
    update_post_meta($post_id, 'auto_restock_value', $custom_value);
    //get_post_meta($post -> ID, 'daily_restock_amount', true);
    update_post_meta($post_id, '_stock', $custom_value);
}

function update_cart_stock() {
    global $post_id;
    //echo get_post_meta($post_id, 'total_sales', true);
    update_post_meta($post_id, '_stock', $custom_value);
}

add_action( 'save_post', 'update_custom_meta' , 10, 2 );

2 Answers 2

0

So, if anyone has the very same problem, as I had - here is an answer. Basically, before I write any kind of post with asking for help, I try to make sure, that I have used every solution, that I know, or have found on forums. BUT still, there is a chance, that I get something wrong, or use other combination.

And that was one of those times. So anyway, global $sum inside post_orderfunction should be outside foreach loop. I have placed it just before loop. Then, in function update_custom_meta I didn't post global $sum, now it's fixed.

And that's it! I thought, that i have tried those, but it turned out I didn't. Hope it helps someone in the future :)

0

I don't think you need to use global variables for this. That hook that you have defined:

add_action( 'save_post', 'update_custom_meta' , 10, 2 );

You have specified two parameters to be passed to them. If you look at the save_post API, you can see that you can define that callback as:

function update_cart_stock($post_ID, $post) {
    global $post_id;
    //echo get_post_meta($post_id, 'total_sales', true);
    update_post_meta($post_id, '_stock', $custom_value);
}

So the post ID that is invoking the save event is coming thru as a parameter.

You can then define your other function as:

function update_custom_meta( $post_id ) {

And pass it the post from update_cart_stock, does that make sense?

1
  • Yeah, that didn't work. I needed variable $sum, not $post_id. But thank you anyway! Commented Apr 5, 2020 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.