如果价格在 Woocommerce 3 中更新,则更改产品状态
Change product status if prices are updated in Woocommerce 3
我需要在挂钩中更改产品 post_status。每当供应商更改价格时,我都试图让产品恢复到 "pending" 状态。
add_action( 'updated_post_meta', 'mp_sync_on_product_save', 10, 4 );
function mp_sync_on_product_save( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( $meta_key == '_price' ) { // edited price
if ( get_post_type( $post_id ) == 'product' ) {
$product = wc_get_product( $post_id );
$product['post_status'] = 'pending'; //how to update this?
// var_dump($product_id);var_dump($product);die('test');
}
}
}
谁能告诉我什么函数可以做到这一点:“$product['post_status'] ='pending';”?
以下代码将将产品状态更改为待处理 如果 "administrator" 用户角色以外的任何人在后端更新产品价格:
add_action( 'woocommerce_product_object_updated_props', 'change_status_on_product_object_updated_prices', 10, 2 );
function change_status_on_product_object_updated_prices( $product, $updated_props ) {
$changed_props = $product->get_changes();
if ( $product->get_status() !== 'pending' && ( in_array( 'regular_price', $updated_props, true ) ||
in_array( 'sale_price', $updated_props, true ) ) && ! current_user_can( 'administrator' ) )
{
wp_update_post( array( 'ID' => $product->get_id(), 'post_status' => 'pending' ) );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
The "administrator" user role will not be affected by the code… You should also check that "Vendors user role is not able to change the product post status.
我需要在挂钩中更改产品 post_status。每当供应商更改价格时,我都试图让产品恢复到 "pending" 状态。
add_action( 'updated_post_meta', 'mp_sync_on_product_save', 10, 4 );
function mp_sync_on_product_save( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( $meta_key == '_price' ) { // edited price
if ( get_post_type( $post_id ) == 'product' ) {
$product = wc_get_product( $post_id );
$product['post_status'] = 'pending'; //how to update this?
// var_dump($product_id);var_dump($product);die('test');
}
}
}
谁能告诉我什么函数可以做到这一点:“$product['post_status'] ='pending';”?
以下代码将将产品状态更改为待处理 如果 "administrator" 用户角色以外的任何人在后端更新产品价格:
add_action( 'woocommerce_product_object_updated_props', 'change_status_on_product_object_updated_prices', 10, 2 );
function change_status_on_product_object_updated_prices( $product, $updated_props ) {
$changed_props = $product->get_changes();
if ( $product->get_status() !== 'pending' && ( in_array( 'regular_price', $updated_props, true ) ||
in_array( 'sale_price', $updated_props, true ) ) && ! current_user_can( 'administrator' ) )
{
wp_update_post( array( 'ID' => $product->get_id(), 'post_status' => 'pending' ) );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
The "administrator" user role will not be affected by the code… You should also check that "Vendors user role is not able to change the product post status.