如果在Woocommerce 3中更新了价格,则更改产品状态

时间:2018-10-24 20:16:43

标签: php wordpress woocommerce product user-roles

我需要在挂钩中更改产品post_status。每当供应商更改价格时,我都试图使产品回到“待处理”状态。

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';”吗?

1 个答案:

答案 0 :(得分:1)

如果除“管理员”用户角色以外的任何人都在后端更新产品价格,则以下代码将将产品状态更改为待定

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文件中。经过测试,可以正常工作。

  

“管理员”用户角色不会受到代码的影响……您还应该检查“供应商用户角色不能更改产品发布状态。

相关问题