有没有人能帮我写一下下面的代码?仅当产品名称包含字符串"Fiat"时,我才try 向新创建的产品添加自定义采购备注.

add_action( 'transition_post_status', 'bbloomer_add_custom_meta_on_publish_product', 9999, 3 );

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

$product = wc_get_product( id );
$prod_name = $product->get_title();
$string_to_find = 'Fiat';

if ( 'product' === $post->post_type && 'publish' === $new_status && 'publish' !== $old_status && strpos( strtolower($product_name), $string_to_find ) === true ) {
  update_post_meta( $post->ID, '_purchase_note', 'This is my Fiat text' );
}}

推荐答案

add_action('woocommerce_before_product_object_save', 'save_product_before_action', 10, 2);

function save_product_before_action($product, $data_store) {


    if ($product->get_id()) {

        $prod_name = $product->get_name();
        $string_to_find = 'Fiat';

        if (strpos($prod_name, $string_to_find) !== false) {
            $product->set_purchase_note('This is my Fiat text');
        }
    }
}

Php相关问答推荐

为什么这个方法调用用引号和花括号包裹?

如何使用属性#[Embedded]嵌入带有规则的对象集合?

在WooCommerce产品档案页面中显示产品销售百分比

不包括WooCommerce中单独销售的产品

如何确保所有语言文件都使用Laravel中的新密钥进行更新?

将一个按钮添加到WooCommerce产品的管理视图中,该按钮链接到一个自定义的WebHook,并在末尾附加产品ID

如何在Livewire 3 Form类中做依赖注入?

哪里的表现能更好?

shell_exec运行大型数据处理

显示自定义 Laravel 9 公共属性的错误

如何使用 AJAX、PHP 和 MYSQL 在 Select 项目时在文本框中填充数据

如何从 URL 核心 PHP 中的 API 获取 JSON?

如何在 Phpunit 的静态提供程序中创建测试双打?

htaccess Rewriterule 无法以任何方式工作

Composer自动加载器重复了子类的类文件路径

symfony/notifier + twig/inky-extra 在 dunglas/symfony-docker 中缺少 ext-xsl.

Eloquent 的模型更新了它应该 laravel php 的更多行

在全局安装 Composer 包后运行命令

Google Drive - 仅使用 FileID 获取文件的 FileSize

Laravel 自定义中间件:ERR_TOO_MANY_REDIRECTS