Php 使用Woocommerce中的挂钩更新产品价格
在wp admin中更新产品时,我尝试使用带有整数或字符串的元键Php 使用Woocommerce中的挂钩更新产品价格,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,在wp admin中更新产品时,我尝试使用带有整数或字符串的元键\u regular\u price更新产品常规价格 我想要的用户流是: 打开产品编辑页面 单击“更新”按钮 重新加载页面后,请确保将_常规_价格设置为20 请帮助我找出我在上述函数中的错误,并让我知道实现此功能的任何其他方法。要处理woocommerce\u process\u product\u meta,我猜您缺少参数。我希望下面的代码可以满足您的需要 add_action( 'woocommerce_process_pro
\u regular\u price
更新产品常规价格
我想要的用户流是:
请帮助我找出我在上述函数中的错误,并让我知道实现此功能的任何其他方法。要处理woocommerce\u process\u product\u meta,我猜您缺少参数。我希望下面的代码可以满足您的需要
add_action( 'woocommerce_process_product_meta', $wc_meta_box_product_data_save, $int, $int );
参数(3)
- $wc_meta_box_产品_数据_保存(字符串)=>“wc_meta_box_产品_数据::保存” wc meta box产品数据保存
- $int(int)=>10整数
- $int(int)=>2整数
add_action( 'save_post', 'update_the_product_price', 10, 3 );
function update_the_product_price( $post_id, $post, $update ) {
if ( $post->post_type != 'product') return; // Only products
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
$price = 50; // <=== <=== <=== <=== <=== <=== Set your price
$product = wc_get_product( $post_id ); // The WC_Product object
// if product is not on sale
if( ! $product->is_on_sale() ){
update_post_meta( $post_id, '_price', $price ); // Update active price
}
update_post_meta( $post_id, '_regular_price', $price ); // Update regular price
wc_delete_product_transients( $post_id ); // Update product cache
}
add_action('save_post','update_the_product_price',10,3);
函数更新产品价格($post\U id、$post$update){
if($post->post_type!=“product”)返回;//仅限产品
//如果这是自动保存,则我们的表单尚未提交,因此我们不想执行任何操作。
if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE)
返回$post_id;
//检查用户的权限。
如果(!当前用户可以('编辑产品',$post\U id))
返回$post_id;
$price=50;//我添加了10,2
作为整数,但它没有创建所需的结果。其他东西,如\u stock
和我的所有自定义元,都可以使用我的上述函数进行良好更新。只有\u regular\u price
才导致问题。这太完美了!
add_action( 'save_post', 'update_the_product_price', 10, 3 );
function update_the_product_price( $post_id, $post, $update ) {
if ( $post->post_type != 'product') return; // Only products
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
$price = 50; // <=== <=== <=== <=== <=== <=== Set your price
$product = wc_get_product( $post_id ); // The WC_Product object
// if product is not on sale
if( ! $product->is_on_sale() ){
update_post_meta( $post_id, '_price', $price ); // Update active price
}
update_post_meta( $post_id, '_regular_price', $price ); // Update regular price
wc_delete_product_transients( $post_id ); // Update product cache
}