Woocommerce 挂钩,用于在save_柱上设置产品重量

Woocommerce 挂钩,用于在save_柱上设置产品重量,woocommerce,save,product,Woocommerce,Save,Product,有没有办法用save\u posthook设置产品重量 我有以下代码,但我不知道如何覆盖权重: add_action( 'save_post', 'change_weight' ); function change_weight($post_id) { $WC_Product = wc_get_product($post_id); } 要设置权重,您需要更新post meta。可以这样做: update_post_meta( $post_id, '_weight', $weight )

有没有办法用
save\u post
hook设置产品重量

我有以下代码,但我不知道如何覆盖权重:

add_action( 'save_post', 'change_weight' );
function change_weight($post_id) {
    $WC_Product = wc_get_product($post_id);
}

要设置权重,您需要更新post meta。可以这样做:

update_post_meta( $post_id, '_weight', $weight );
if ( get_post_type ( $post_id ) == 'shop_order' ) {
    update_post_meta( $post_id, '_weight', $weight );
}
$product = wc_get_product( $post_id );
$weight = $product->get_weight();
上述代码中的$weight是一个变量,包含您希望权重为的值。但是,每次保存任何帖子时都会触发save_post钩子,因此博客帖子、页面、产品等等。您可能需要验证该帖子是否为产品。您可以这样做:

update_post_meta( $post_id, '_weight', $weight );
if ( get_post_type ( $post_id ) == 'shop_order' ) {
    update_post_meta( $post_id, '_weight', $weight );
}
$product = wc_get_product( $post_id );
$weight = $product->get_weight();
此外,如果您想在更改产品之前获得产品的当前重量,您可以这样做:

update_post_meta( $post_id, '_weight', $weight );
if ( get_post_type ( $post_id ) == 'shop_order' ) {
    update_post_meta( $post_id, '_weight', $weight );
}
$product = wc_get_product( $post_id );
$weight = $product->get_weight();

要设置权重,您需要更新post meta。可以这样做:

update_post_meta( $post_id, '_weight', $weight );
if ( get_post_type ( $post_id ) == 'shop_order' ) {
    update_post_meta( $post_id, '_weight', $weight );
}
$product = wc_get_product( $post_id );
$weight = $product->get_weight();
上述代码中的$weight是一个变量,包含您希望权重为的值。但是,每次保存任何帖子时都会触发save_post钩子,因此博客帖子、页面、产品等等。您可能需要验证该帖子是否为产品。您可以这样做:

update_post_meta( $post_id, '_weight', $weight );
if ( get_post_type ( $post_id ) == 'shop_order' ) {
    update_post_meta( $post_id, '_weight', $weight );
}
$product = wc_get_product( $post_id );
$weight = $product->get_weight();
此外,如果您想在更改产品之前获得产品的当前重量,您可以这样做:

update_post_meta( $post_id, '_weight', $weight );
if ( get_post_type ( $post_id ) == 'shop_order' ) {
    update_post_meta( $post_id, '_weight', $weight );
}
$product = wc_get_product( $post_id );
$weight = $product->get_weight();
如果您使用了,那么您就不必担心nonce,因为您可以借助WooCommerce的理智检查

// This will work in both WC 2.6 and WC 2.7
add_action( 'woocommerce_process_product_meta_simple', 'so_42445796_process_meta' );
function so_42445796_process_meta( $post_id ) {
    $weight = 100;
    update_post_meta( $post_id, '_weight', $weight );
}
WC 2.7将引入CRUD方法,以抽象数据的保存方式。我怀疑他们最终会将产品和产品元移出默认的WordPress表,但我不能确定。在2.7中,您可以在保存
$product
对象之前使用钩子修改该对象

// Coming in WC2.7 you can use the CRUD methods instead
add_action( 'woocommerce_admin_process_product_object', 'so_42445796_process_product_object' );
function so_42445796_process_product_object( $product ) {
    $weight = 100;
    $product->set_weight( $weight );
}
如果您使用了,那么您就不必担心nonce,因为您可以借助WooCommerce的理智检查

// This will work in both WC 2.6 and WC 2.7
add_action( 'woocommerce_process_product_meta_simple', 'so_42445796_process_meta' );
function so_42445796_process_meta( $post_id ) {
    $weight = 100;
    update_post_meta( $post_id, '_weight', $weight );
}
WC 2.7将引入CRUD方法,以抽象数据的保存方式。我怀疑他们最终会将产品和产品元移出默认的WordPress表,但我不能确定。在2.7中,您可以在保存
$product
对象之前使用钩子修改该对象

// Coming in WC2.7 you can use the CRUD methods instead
add_action( 'woocommerce_admin_process_product_object', 'so_42445796_process_product_object' );
function so_42445796_process_product_object( $product ) {
    $weight = 100;
    $product->set_weight( $weight );
}