0

i built custom script to import woocommerce product using (wp_insert_post) and i want to call other plugins to insert their data to each new products to db for example:

after inserting product if i opened that product and clicked update the yoast seo insert to db all required info like _yoast_wpseo_primary_product_brand and so on, and second example if during the insert the product stock was 0 woocommerce by default put the product in-stock till i click update .

i tried to call wp_update_post ( $post_id ) but nothing has been changed.

can you please advice :)

0

I don't completely understand your question - maybe this will help:

wp_insert_post(array(
    // your woocomm post being inserted
));

// at insert, `save_post` will fire
add_action( 'save_post', 'my_plugin_save_post' , 10 , 3 );

function my_plugin_save_post($post_id, $post, $update) {

    // check other firing of this: if this is a revision, get real post ID
    if ( $parent_id = wp_is_post_revision( $post_id ) ) 
        $post_id = $parent_id;

    // you can condialize if you want to exicute on new posts (like `wp_insert_post`), or if you're UPDATEing a post
    if ($update) {
        // only run if post has been saved before

        // ------ example: you can UPDATE the `post` itself with ------

        // unhook this function so it doesn't loop infinitely
        remove_action( 'save_post', 'my_plugin_save_post' );

        // update the post, which calls save_post again
        wp_update_post( 
            array( 
                'ID' => $post_id, 
                'post_status' => 'private' 
            ) 
        );

        // re-hook this function
        add_action( 'save_post', 'my_plugin_save_post' );

        // ------ example: or you can do post meta work ------

        if ($somthing == true)
            update_post_meta($post_id, ...

        // ------ example: or "call" other plugins ----

        wc_update_product_stock(...);

    } else {
        // only run if post is brand new
    }   
}
  • Thanks David that's exactly what am looking for. and sorry for not being clear in my question :) – kashalo Jan 31 '18 at 13:20

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.