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