Wordpress Update Hook错误后

时间:2018-02-17 09:53:37

标签: php wordpress

我已将Wordpress更新为4.9.4版 从那时起,我遇到了一个自定义钩子的问题,我创建了它。基本上它将产品(Woocommerce)设置为库存管理是和库存数量1.然后它将默认语言设置为英语(WPML插件)。在更新之前,挂钩工作没有任何错误。我在这段代码中找不到bug。你能支持我吗?

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
}

1 个答案:

答案 0 :(得分:0)

我修复了这个错误。我只需要在第一个if状态中包含第二个if。所以基本上,在update_post_meta($ post-> ID,' _stock',' 1')之后移动右括号("}");到最后。这里是最终的代码:

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
 }
}