如果 Woocommerce 3 中的价格更新,则更改产品状态 [英] Change product status if prices are updated in Woocommerce 3

查看:40
本文介绍了如果 Woocommerce 3 中的价格更新,则更改产品状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在挂钩中更改产品 post_status.每次供应商更改价格时,我都试图让产品回到待定"状态.

I need to change product post_status in a hook. I trying to make product get back to "pending" status everytime vendor change the price.

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';"?

Can someone tell me what function could do this: "$product['post_status'] ='pending';"?

推荐答案

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

The following code will change the product status to pending if anyone else than the "administrator" user role update product prices in backend:

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 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

管理员"用户角色不会受代码影响……您还应该检查供应商用户角色无法更改产品发布状态.

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.

这篇关于如果 Woocommerce 3 中的价格更新,则更改产品状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆