WooCommerce 教程:产品发布后挂钩(transition_post_status)

WooCommerce 教程:产品发布后挂钩(transition_post_status)

客户需要在产品发布时添加 “某物”。如您所知,例如,创建产品后,会自动添加名为 “total_sales” 的后期元信息。所以,当我们发布新产品时,我们来看看怎么做 “东西”。

示例:当创建 WooCommerce 产品时,我们来添加 add_post_meta

PHP 解决方案:创建产品时做 “某事”

function add_this_to_new_products( $new_status, $old_status, $post ) {

    global $post;

    if ( 'publish' !== $new_status or 'publish' === $old_status ) return;

    add_post_meta( $post->ID, 'total_amount', '0', true ); // This is the action to take

}

add_action( 'transition_post_status', 'add_this_to_new_products', 10, 3 );

如何添加此代码?

1、您可以将 PHP 代码片段放置在主题或子主题的 functions.php 文件的底部(如果是 CSS 代码,请添加到子主题的 style.css 文件底部)修改之前建议先备份原始文件,若出现错误请先删除此代码。

2、WordPress 4.9 后改进了主题编辑器,对于 CSS 代码也可打开网站前台编辑器的【自定义】,复制代码添加到自定义 css 中。

此代码是否可用?

如需帮助或是您有更好的方案想分享?请到薇晓朵 WooCommerce 中文论坛留言告知,我们希望可以帮到更多国内的 WooCommerce 用户也希望您的参与。

文章没看懂?代码不会用?需要帮助您可以去论坛提问自助服务台

作者小新

大象、大象 ,你的鼻子怎么那么长 ,妈妈说鼻子长才是漂亮 ......