最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Global variable doesnt't pass to another function

programmeradmin1浏览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 );

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 );
Share Improve this question asked Apr 4, 2020 at 17:20 SlingySlingy 311 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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 :)

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?

发布评论

评论列表(0)

  1. 暂无评论