$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>user meta - Updating WooCommerce product field when product author updates profile field|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

user meta - Updating WooCommerce product field when product author updates profile field

matteradmin10PV0评论

This is a continuation/variation on this issue: Adding existing user custom field value to a woocommerce product

While this solution worked, ideally, it would be great if when the product author updates their bio (description meta tag), all the products they created would also be updated with this update (in this case the $post->post_content meta field).

This is a continuation/variation on this issue: Adding existing user custom field value to a woocommerce product

While this solution worked, ideally, it would be great if when the product author updates their bio (description meta tag), all the products they created would also be updated with this update (in this case the $post->post_content meta field).

Share Improve this question asked Feb 23, 2019 at 17:08 Z CrowZ Crow 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

So combined with that code for setting the value before it's inserted in the db, you also need to add this code to add the post ID to an array stored in the user's meta (after it has been updated):

add_action( 'wp_insert_post', 'smyles_update_user_products', 10, 3 );

function smyles_update_user_products( $post_ID, $post, $update ) {

    if ( $post->post_type !== 'product' ) {
        return;
    }

    $user_id = get_current_user_id();

    if ( ! empty( $user_id ) ) {

        $user_products = get_user_meta( $user_id, 'user_products', true );

        if ( is_array( $user_products ) && ! in_array( $post_ID, $user_products ) ) {
            $user_products[] = $post_ID;
        } else {
            $user_products = array( $post_ID );
        }

        update_user_meta( $user_id, 'user_products', $user_products );
    }
}

Then add a hook when the user's meta is updated, to then trigger the update on all of those associated posts:

add_action( 'updated_user_meta', 'smyles_update_post_on_user_desc_update', 10, 4 );

function smyles_update_post_on_user_desc_update( $meta_id, $user_id, $meta_key, $_meta_value ){

    if( $meta_key !== 'some_meta_key' ){
        return;
    }

    // Probably not necessary, but just in case
    if( ! empty( $user_id ) ){
        $user_products = get_user_meta( $user_id, 'user_products', true );

        if( ! empty( $user_products ) && is_array( $user_products ) ){

            // Remove our action to prevent loop
            remove_action( 'wp_insert_post_data', 'smyles_insert_user_product_data', 10 );

            foreach( (array) $user_products as $product_id ){

                $my_post = array(
                    'ID'           => $product_id,
                    'post_content' => $_meta_value,
                );

                wp_update_post( $my_post );

            }

            // Add it back after were done
            add_action( 'wp_insert_post_data', 'smyles_insert_user_product_data', 10, 2 );
        }
    }
}

You may be able to just remove setting the actual post_content value and just call wp_update_post (and remove the remove_action and add_action calls), allowing that action to still handle this (but i haven not tested the code so i'm not sure if WordPress will still do an update if all you pass is just the ID)

Post a comment

comment list (0)

  1. No comments so far