简体   繁体   中英

After Wordpress Update Hook error

I have updated my Wordpress to Version 4.9.4 Since then I have an issue with a custom hook, which I created. Basically it set products (Woocommerce) to stock manage yes and stock quantity 1. Then it set the default language to English (WPML Plugin). Before the update, the hook worked without any error. I cannot find the bug in this code. Can you please support me?

add_action('save_post', 'myWoo_savePost', 10, 2);

function myWoo_savePost($postID, $post) {
    if (isset($post->post_type) && $post->post_type == 'product') {

    update_post_meta($post->ID, '_manage_stock', 'yes');
        update_post_meta($post->ID, '_stock', '1');

    }

    //start translation
        $inserted_post_ids = $post->ID;

    if ( $inserted_post_ids) {
        // https://wpml.org/wpml-hook/wpml_element_type/
        $wpml_element_type = apply_filters( 'wpml_element_type', 'post_product' );

        // get the language info of the original post
        // https://wpml.org/wpml-hook/wpml_element_language_details/
        $get_language_args = array('element_id' => $inserted_post_ids, 'element_type' => 'post_product' );
        $original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );

        $set_language_args = array(
            'element_id'    => $inserted_post_ids,
            'element_type'  => $wpml_element_type,
            'trid'   => $original_post_language_info->trid,
            'language_code'   => 'en',
            'source_language_code' => $original_post_language_info->language_code
        );

        do_action( 'wpml_set_element_language_details', $set_language_args );
    }
    //end translation
}

I fixed the bug. I just need to enclose the second if in the first if state. So basically, move the closing bracket ("}") after update_post_meta($post->ID, '_stock', '1'); to the end. Here the final code:

add_action('save_post', 'myWoo_savePost', 10, 2);

function myWoo_savePost($postID, $post) {
    if (isset($post->post_type) && $post->post_type == 'product') {

    update_post_meta($post->ID, '_manage_stock', 'yes');
        update_post_meta($post->ID, '_stock', '1');


    //start translation
        $inserted_post_ids = $post->ID;

    if ( $inserted_post_ids) {
        // https://wpml.org/wpml-hook/wpml_element_type/
        $wpml_element_type = apply_filters( 'wpml_element_type', 'post_product' );

        // get the language info of the original post
        // https://wpml.org/wpml-hook/wpml_element_language_details/
        $get_language_args = array('element_id' => $inserted_post_ids, 'element_type' => 'post_product' );
        $original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );

        $set_language_args = array(
            'element_id'    => $inserted_post_ids,
            'element_type'  => $wpml_element_type,
            'trid'   => $original_post_language_info->trid,
            'language_code'   => 'en',
            'source_language_code' => $original_post_language_info->language_code
        );

        do_action( 'wpml_set_element_language_details', $set_language_args );
    }
    //end translation
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM