Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
share|improve this question
2  
You have 2 good answers. Why not come back and accept one? –  helgatheviking Apr 10 '13 at 17:30
add comment

2 Answers

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' );
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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