In WooCommerce, when I add couple of code lines to change the product description, on product status change, the website throw an error when submitting (saving).
My code:
add_action('transition_post_status', 'new_product_add', 10, 3);
function new_product_add($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'pending'
&& !empty($post->ID)
&& in_array( $post->post_type,
array( 'product')
)
) {
$term = get_term_by('name', 'فروش پیج اینستاگرام', 'product_cat');
wp_set_object_terms($post->ID, $term->term_id, 'product_cat', true);
/******************************/
$product = wc_get_product($post->ID);
$product->set_description("something");
$product->save();
/******************************/
}
}
as you see, these last 3 lines lead to the problem the page refreshes and also update the product description but give me and error
“THE SITE IS EXPERIENCING TECHNICAL DIFFICULTIES”
and when I remove them it works okay and show me the successful message
UPDATE
I changed these 3 line with Wordpress way
wp_update_post( array('ID' => $post->ID, 'post_content' => "something"));
still do the job but instead of successful message gives me this technical error
UPDATE II
When I open Chrome console I see this error also
Failed to load resource: the server responded with a status of 500 () (index):1
In WooCommerce, when I add couple of code lines to change the product description, on product status change, the website throw an error when submitting (saving).
My code:
add_action('transition_post_status', 'new_product_add', 10, 3);
function new_product_add($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'pending'
&& !empty($post->ID)
&& in_array( $post->post_type,
array( 'product')
)
) {
$term = get_term_by('name', 'فروش پیج اینستاگرام', 'product_cat');
wp_set_object_terms($post->ID, $term->term_id, 'product_cat', true);
/******************************/
$product = wc_get_product($post->ID);
$product->set_description("something");
$product->save();
/******************************/
}
}
as you see, these last 3 lines lead to the problem the page refreshes and also update the product description but give me and error
“THE SITE IS EXPERIENCING TECHNICAL DIFFICULTIES”
and when I remove them it works okay and show me the successful message
UPDATE
I changed these 3 line with Wordpress way
wp_update_post( array('ID' => $post->ID, 'post_content' => "something"));
still do the job but instead of successful message gives me this technical error
UPDATE II
When I open Chrome console I see this error also
Failed to load resource: the server responded with a status of 500 () (index):1
Share
Improve this question
edited May 20, 2019 at 12:25
LoicTheAztec
3,39117 silver badges24 bronze badges
asked May 18, 2019 at 11:59
zEn feeLozEn feeLo
2073 silver badges18 bronze badges
1
- can it be related to using wp_set_object_terms before wp_update_post ?? or using update_post_meta before that ? – zEn feeLo Commented May 18, 2019 at 12:23
1 Answer
Reset to default 2To avoid an infinite loop on transition_post_status
hook when using wp_update_post()
, try:
add_action('transition_post_status', 'new_product_add', 10, 3);
function new_product_add($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'pending'
&& !empty($post->ID)
&& $post->post_type === 'product'
) {
$term = get_term_by('name', 'فروش پیج اینستاگرام', 'product_cat');
wp_set_object_terms($post->ID, $term->term_id, 'product_cat', true);
// unhook this function so it doesn't loop infinitely
remove_action( 'transition_post_status', 'new_product_add', 10 );
wp_update_post( array('ID' => $post->ID, 'post_content' => "something"));
// re-hook this function
add_action( 'transition_post_status', 'new_product_add', 10, 3 );
}
}
Or:
add_action('transition_post_status', 'new_product_add', 10, 3);
function new_product_add($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'pending'
&& !empty($post->ID)
&& $post->post_type === 'product'
) {
$term = get_term_by('name', 'فروش پیج اینستاگرام', 'product_cat');
wp_set_object_terms($post->ID, $term->term_id, 'product_cat', true);
$product = wc_get_product($post->ID);
// unhook this function so it doesn't loop infinitely
remove_action( 'transition_post_status', 'new_product_add', 10 );
$product->set_description("something");
$product->save();
// re-hook this function
add_action( 'transition_post_status', 'new_product_add', 10, 3 );
}
}
It may solve your issue.
This is documented here on
wp_update_post()
forsave_post
hook.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745480337a4629529.html
评论列表(0条)