0

I am wondering how you would implement the following hook.

do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id );

What is the correct way to call a call the action from a plugin file. Bellow is the code that I am currently using but its not working.

global $post, $thepostid, $woocommerce;

function new_post_meta () {
    if (isset($_POST['location'])) 
        update_post_meta( $post_id, 'location', json_encode($_POST['location']) );      
}

add_action( 'woocommerce_process_product_meta_' .$product_type, 'new_post_meta' );
  • 2
    You have 2 good answers. Why not come back and accept one? – helgatheviking Apr 10 '13 at 17:30
2

If you don't intend your meta field to be specific to a particular product type you can do the following ( which is what I do in my own WooCommerce extension ).

function new_post_meta () {
    if (isset($_POST['location'])) 
        update_post_meta( $post_id, 'location', json_encode($_POST['location']) );      
}

add_action( 'woocommerce_process_product_meta', 'new_post_meta' );

or to limit this to a particular product type:

function new_post_meta () {
    if (isset($_POST['location'])) 
        update_post_meta( $post_id, 'location', json_encode($_POST['location']) );      
}

add_action( 'woocommerce_process_product_meta_simple', 'new_post_meta' );

Where 'simple' is the product type.

0

For example you have two product types: product1 and product2. Then you will have two action calls: woocommerce_process_product_meta_product1 and woocommerce_process_product_meta_product2. So you need to add your hooks like this:

function new_post_meta1 () {
    if (isset($_POST['location'])) 
        update_post_meta( $post_id, 'location', json_encode($_POST['location']) );       
}
add_action( 'woocommerce_process_product_meta_product1', 'new_post_meta1' );

function new_post_meta2 () {
    if (isset($_POST['location'])) 
        update_post_meta( $post_id, 'location', json_encode($_POST['location']) );       
}
add_action( 'woocommerce_process_product_meta_product2', 'new_post_meta2' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.