php - 在产品创建中添加购买备注元 - WooCommerce

标签 php wordpress woocommerce product hook-woocommerce

谁能帮我写下面的代码?仅当产品名称包含字符串“Fiat”时,我才尝试向新创建的产品添加自定义购买备注。

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 - 在产品创建中添加购买备注元 - WooCommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74188322/

相关文章:

php - 如何在 WooCommerce 的订单支付页面上检索订单 ID

PHP 已弃用代码?

html - wordpress 插件和主题 css 发生冲突

wordpress - WooCommerce 模板过时通知不会在 "System Status"页面上消失

php - 隐藏运输标签 woocommerce

javascript - 通过比较一些数据使用wpdb获取数据

php - 如何根据唯一且预定的顺序而不是 asc 或 desc 对 mysql 数据库中的数据进行排序

php - Adsense Onclick(php, javascript)

php - 使用 PHP 更改服务器日期

php - 根据订单详情页面中的产品标题替换产品 SKU