I'm trying to replace Weight field value in woocommerce with an ACF field value, in WordPress Dashboard.
I'm not sure If I need to use this function, to replace this field
// define the woocommerce_save_product_variation callback
function action_woocommerce_save_product_variation( $variation_id, $i ) {
// make action magic happen here...
};
// add the action
add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );
In your screenshot, you are not working on product variations, so your code is not appropriated.
For all product types (except variable products and it's variations, you will use one of those (where you will replace custom_weight
in the ACF get_field()
function by the right slug) :
add_action( 'woocommerce_process_product_meta', 'update_product_weight_from_acf', 100, 1 );
function update_product_weight_from_acf( $product_id ) {
if( $weight_acf = get_field( 'custom_weight', $product_id ) )
update_post_meta( $product_id, '_weight', $weight_acf );
}
or using WC_Products setter method (introduced with Woocommerce 3):
add_action( 'woocommerce_admin_process_product_object', 'update_product_weight_from_acf', 10, 1 );
function update_product_weight_from_acf( $product ) {
if( $weight_acf = get_field( 'custom_weight', $product->get_id() ) )
$product->set_weight( $weight_acf );
}
Code goes in function.php file of your active child theme (or active theme). It should works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.