使用 Woocommerce 中的挂钩更新产品价格
Update product price using a hook in Woocommerce
当产品在 wp-admin 中更新时,我正在尝试使用带有整数或字符串的元键 _regular_price
更新产品正常价格。
我想要的用户流程是:
- 打开产品编辑页面
- 点击更新按钮
- 看到页面重新加载后 _regular_price 设置为 20。
add_action( 'woocommerce_process_product_meta', 'update_test' );
function update_test( $post_id ) {
update_post_meta( $post_id, '_regular_price', 20 );
}
请帮我找出我在上面的函数中做错了什么,并让我知道任何其他方法来完成这个。
要处理 woocommerce_process_product_meta,我猜你缺少参数。希望以下代码能满足您的需求。
add_action( 'woocommerce_process_product_meta', $wc_meta_box_product_data_save, $int, $int );
参数(3)
- $wc_meta_box_product_data_save(字符串)=> 'WC_Meta_Box_Product_Data::save'
wc meta box产品数据保存。
- $int (int) => 10 int.
- $int (int) => 2 int.
您可以找到详细信息 in this link。
已更新 (2018 年 8 月)
您的代码是正确的,但挂钩是为 Metaboxes 自定义字段创建的。
您应该改用 save_post_{$post->post_type}
Wordpress 钩子 定位产品 post 仅键入 .
此外,您可能需要更新有效价格,并使用函数wc_delete_product_transients()
刷新产品临时缓存。
因此您的代码将是:
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
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试并有效……
当产品在 wp-admin 中更新时,我正在尝试使用带有整数或字符串的元键 _regular_price
更新产品正常价格。
我想要的用户流程是:
- 打开产品编辑页面
- 点击更新按钮
- 看到页面重新加载后 _regular_price 设置为 20。
add_action( 'woocommerce_process_product_meta', 'update_test' );
function update_test( $post_id ) {
update_post_meta( $post_id, '_regular_price', 20 );
}
请帮我找出我在上面的函数中做错了什么,并让我知道任何其他方法来完成这个。
要处理 woocommerce_process_product_meta,我猜你缺少参数。希望以下代码能满足您的需求。
add_action( 'woocommerce_process_product_meta', $wc_meta_box_product_data_save, $int, $int );
参数(3)
- $wc_meta_box_product_data_save(字符串)=> 'WC_Meta_Box_Product_Data::save' wc meta box产品数据保存。
- $int (int) => 10 int.
- $int (int) => 2 int.
您可以找到详细信息 in this link。
已更新 (2018 年 8 月)
您的代码是正确的,但挂钩是为 Metaboxes 自定义字段创建的。
您应该改用 save_post_{$post->post_type}
Wordpress 钩子 定位产品 post 仅键入 .
此外,您可能需要更新有效价格,并使用函数wc_delete_product_transients()
刷新产品临时缓存。
因此您的代码将是:
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
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试并有效……