Ket*_*tan 6 wordpress hook product woocommerce
我需要Woocommerce产品发布,更新和删除钩子,如果知道的话请告知我.
我发现这个钩子:
add_action('transition_post_status', 'wpse_110037_new_posts', 10, 3);
function wpse_110037_new_posts($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'publish'
&& !empty($post->ID)
&& in_array( $post->post_type,
array( 'product')
)
) {
//add some cde here
}
}
Run Code Online (Sandbox Code Playgroud)
但它只显示产品ID,标题,发布状态等....但我想要产品价格,类别,标签,品牌和库存状态.
如果有人知道,请重播我.
谢谢,Ketan.
Wis*_*abs 12
Woocommerce产品基本上是wordpress帖子.你可以使用wordpress钩子
add_action( 'before_delete_post', 'wpse_110037_new_posts' );
add_action( 'save_post', 'wpse_110037_new_posts' );
function wpse_110037_new_posts($post_id){
$WC_Product = wc_get_product( $post_id);
}
Run Code Online (Sandbox Code Playgroud)
wc_get_product()将返回WC_Product对象,您可以从中获取产品详细信息.
小智 8
和挂钩在更新之前运行save_post,并且由于大多数 WooCommerce 产品数据存储为,因此使用它们可能会导致问题。save_post_productpost_metapost_meta
值得庆幸的是,从 v3 开始,有特定的 WooCommerce 挂钩在产品更新 ( woocommerce_update_product ) 和创建产品 ( woocommerce_new_product ) 后运行。
add_action( 'woocommerce_new_product', 'on_product_save', 10, 1 );
add_action( 'woocommerce_update_product', 'on_product_save', 10, 1 );
function on_product_save( $product_id ) {
$product = wc_get_product( $product_id );
// do something with this product
}
Run Code Online (Sandbox Code Playgroud)
我更愿意检查状态是否为草稿。您也可以使用第三个参数update来检查它是否是更新
add_action( 'save_post', array($this, 'wpse1511_create_or_update_product' ), 10, 3);
function wpse1511_create_or_update_product($post_id, $post, $update){
if ($post->post_status != 'publish' || $post->post_type != 'product') {
return;
}
if (!$product = wc_get_product( $post )) {
return;
}
// Make something with $product
// You can also check $update
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8635 次 |
| 最近记录: |