![](/img/trans.png)
[英]Checkout price issue using woocommerce_product_get_price hook
[英]Update product price using a hook in WooCommerce
當產品在_regular_price
-admin.
我想要的用戶流程是:
add_action( 'woocommerce_process_product_meta', 'update_test' );
function update_test( $post_id ) {
update_post_meta( $post_id, '_regular_price', 20 );
}
請幫助我找出我在上述功能中做錯了什么,並讓我知道任何其他方法來實現這一點。
更新(2018 年 8 月)
您的代碼是正確的,但鈎子是為 Metaboxes 自定義字段制作的。
您應該改用save_post_{$post->post_type}
Wordpress hook僅針對產品帖子類型。
此外,您可能需要更新有效價格並使用函數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 文件中或任何插件文件中。
經過測試和工作......
要處理 woocommerce_process_product_meta,我猜您缺少參數。 我希望下面的代碼可以滿足您的需要。
add_action( 'woocommerce_process_product_meta', $wc_meta_box_product_data_save, $int, $int );
參數 (3)
您可以在此鏈接中找到詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.