哪个钩子可以在 WooCommerce 中保存 WooCommerce 产品元数据? [英] Which hook to save WooCommerce product meta in WooCommerce?

查看:26
本文介绍了哪个钩子可以在 WooCommerce 中保存 WooCommerce 产品元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新 Woocommerce 产品更新事件的自定义元数据.我读过我应该使用 woocommerce_update_product 而不是 save_post 但我不明白为什么只有 save_post 在我的情况下有效.

I am trying to update a custom meta on Woocommerce product update event. I've read that I should use woocommerce_update_product rather than save_post but I cannot figure out why only save_post works in my case.

以下代码有效

add_action( 'save_post', 'mp_sync_on_product_save', 20);
function mp_sync_on_product_save( $product_id ) {
     update_post_meta($product_id, 'test_acf_product', "text");
};

下面的代码不起作用

add_action( 'woocommerce_update_product', 'mp_sync_on_product_save', 20);
function mp_sync_on_product_save( $product_id ) {
    update_post_meta($product_id, 'test_acf_product', "text");
};

我偶然发现,如果我在最后添加 exit; 如下,上面的代码可以工作(打破页面但元保存在数据库中)

I accidentally found that if I add exit; at the end as below, the above code works (breaks page but meta saved in DB)

add_action( 'woocommerce_update_product', 'mp_sync_on_product_save', 20);
function mp_sync_on_product_save( $product_id ) {
    update_post_meta($product_id, 'test_acf_product', "text");
    exit;
};

我可以使用 save_post 但我很想知道为什么 woocommerce_update_product 不起作用如果有人能给我一些提示,我将不胜感激.

I can get away with save_post but I'd love to know why woocommerce_update_product won't work I'd appreciate if anyone could give me some hint.

谢谢!

推荐答案

在后台保存产品时,有多种保存产品元数据的方式:

There are multiple ways to save product meta data when saving the product in backend:

1) 从 WooCommerce 3 开始,您可以使用:

1) Since WooCommerce 3 you can use:

add_action( 'woocommerce_admin_process_product_object', 'action_save_product_meta' );
function action_save_product_meta( $product ) {
    $product->update_meta_data( 'test_acf_product', 'text' );
}

2) 或者旧的 WooCommerce 方式:

2) Or the old WooCommerce way:

add_action( 'woocommerce_process_product_meta', 'action_save_product_meta' );
function action_save_product_meta( $product_id ) {
    update_post_meta($product_id, 'test_acf_product', 'text' );
}

3) 或 Wordpress 方式(针对产品"自定义帖子类型):

3) Or the Wordpress way (targeting "product" custom post type):

add_action( 'save_post_product', 'action_save_product_data', 20);
function action_save_product_data( $post_id ) {
     update_post_meta($post_id, 'test_acf_product', 'text');
}

4) 或者也使用 save_post 钩子(并针对产品"自定义帖子类型,以避免为所有帖子类型和自定义帖子类型保存此元数据):

4) Or also using save_post hook (and targeting "product" custom post type, to avoid this meta data to be saved for all posts types and custom post types):

add_action( 'save_post', 'action_save_product_data', 20);
function action_save_product_data( $post_id ) {
    global $typenow;

    if ( 'product' === $typenow ) {
        update_post_meta($post_id, 'test_acf_product', 'text');
    }
}

这篇关于哪个钩子可以在 WooCommerce 中保存 WooCommerce 产品元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆