public function action_save_post($post_id) {
global $wpdb;
$wooqty = $_POST['qty'];
if( !empty( $wooqty ) )
update_post_meta( $post_id, 'qty', esc_attr( $wooqty ) );
}
This will save the data from a Form field in Metabox in wordpess. I would like to use this variable $wooqty which has a value to another function.
public function action_woocommerce_add_order_item_meta($item_id, $values) {
global $wpdb;
$product_no = (int) $values['product_id'];
$qty_no = (int) $values['quantity'];
}
I would like to replace the variable $qty_no data to the $wooqty. my problem is how do i pass the variable $wooqty
to this function so that i can replace the values of $qty_no
.
Seems simple enough to me. You don’t need to set a global, since it is already in WordPress post meta data. Just call
get_post_meta($product_no, 'qty', TRUE);
within the function.So, your new function will look like this:
Not sure why you are globalizing
$wpdb
, since the function doesn’t do any database operations using that variable. You should be fine with removing theglobal $wpdb;
definition at the top of your function as well.Furthermore, I would check
$item_id
variable and see if that is equal to$values['product_id']
, and if it is, use that instead of refetching the product id from the$values
array.Hope I helped, if not be clearer in your question. Question seems a bit old, so you probably already got this figured out by now. But just in case you haven’t.
so you can use the following syntax to set the global from within a function and then access it elsewhere in PHP.
However, it’s not often a good idea and there may be a better way to pass the variable to the functions. How about a third function that sets the variable locally that’s called from within functions 1 and 2 for instance.