php - How to change post meta for all products on product add in WooCommerce?

674

So I am using woocommerce and am using a plugin called WP-All-Import to import a database of products into my Quickbooks connected e-commerce platform. Now I need to change the meta "_sync_status" to "on" for all products after this is complete. How would I do that for all products as they get added?

475

Answer

Solution:

You first need to fetch all of the WooCommerce Products (post type of "product"), loop over each of them, and update the post meta for each. You can run this code by placing it in yourfunctions.php in your theme, a file in the/wp-content/mu-plugins "must-use" plugins directory, or anonymously using a plugin like WordPress Developer + Console.

// args to fetch all products
$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1
);

// create a custom query
$products = new WP_Query( $args );

// if products were returned...
if ( $products->have_posts() ):
    // loop over them....
    while ( $products->have_posts() ):
        // using the_post() to set up the $post
        $products->the_post();

        // use $post->ID to update the '_sync_status' post meta value
        update_post_meta( $post->ID, '_sync_status', 'on' );
    endwhile; 
endif;
90

Answer

Solution:

Thank you its work for me i can update and add new post meta to my products.

// args to fetch all products
$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1
);
// create a custom query
$products = new WP_Query( $args );
// if products were returned...
if ( $products->have_posts() ):
    // loop over them....
    while ( $products->have_posts() ):
        $products->the_post();
            if (get_post_meta(get_the_ID(),'slide_template')=="" || !get_post_meta(get_the_ID(),'slide_template')){
                update_post_meta( get_the_ID(), 'slide_template', 'default' );
                echo get_the_ID().' do it'.'<br>';
            }
    endwhile;
endif;
493

Answer

Solution:

Thepre_post_update hook is called before the post is saved.

If you wish to do this when they get created or updated you can hijack the REQUEST array

add_action('pre_post_update', 'before_data_is_saved_function');
function before_data_is_saved_function($post_id) {
  if ($_REQUEST['post_type'] == 'product') {
    $_REQUEST['_sync_status'] = 'on';
  }
}

If you only want to do this when they 'get added' use:

add_action('pre_post_update', 'before_data_is_saved_function');
function before_data_is_saved_function($post_id) {
  if ($_REQUEST['post_type'] == 'product' && $_REQUEST['publish'] = 'Publish') {
    $_REQUEST['_sync_status'] = 'on';
  }
}

People are also looking for solutions to the problem: php - Translate custom form type in symfony2

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.