我正在建立一个网站,我正在使用WFCM插件将其用作市场. 我想当一个产品的价格发生变化时,我也想改变所有同名的产品. 到目前为止,我实现了改变所有产品的价格,无论名称.

我使用的最新代码是:

add_action('save_post', 'update_prices_by_product_name', 10, 2);

function update_prices_by_product_name($post_id, $post) {
    if ($post->post_type === 'product') {

        $product_name = $post->post_title;
        $new_price = get_post_meta($post_id, '_regular_price', true);
        $related_products = get_posts(array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'exclude' => $post_id,
            'post_title' => $product_name,
        ));

        foreach ($related_products as $related_product) {
            $related_product_id = $related_product->ID;

            $update_data = array(
                'ID' => $related_product_id,
                'post_title' => $product_name,
            );
            wp_update_post($update_data);
        }
    }
}

它不起作用,它会改变所有产品的价格.我知道我已经很接近了,但我需要帮助.

推荐答案

在WordPress WP_Query中,'post_title'参数不存在.

但是,从WordPress 6.2+开始,您可以对'post_title'列使用未记录的搜索参数100,如下所示:

$related_products_ids = get_posts( array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'exclude'        => $post_id,
    'search_columns' => array('post_title'), // Set search to "post_title" column
    's'              => $post->post_title, // Search string
    'fields'         => 'ids' // Return only products Ids
));

或者你也可以使用WC_Product_Query,比如:

$related_products_ids = wc_get_products( array(
    'status'   => 'publish',
    'limit'    => -1,
    'exclude'  => array( $post_id ),
    'name'     => $post->post_title, 
    'return'   => 'ids', // Return only products Ids
));

现在,您提供的代码并没有真正更新相关产品的价格but their names.

要设置为update the related product price,请改用以下代码替换:

add_action('save_post', 'update_prices_by_product_name', 10, 2);
function update_prices_by_product_name($post_id, $post) {
    if ( $post->post_type === 'product' && isset($_POST['_regular_price']) ) {

        $related_products_ids = get_posts( array(
            'post_type'      => 'product',
            'post_status'    => 'publish',
            'posts_per_page' => -1,
            'exclude'        => $post_id,
            'search_columns' => array('post_title'), // Set search to "post_title" column
            's'              => $post->post_title, // Search string
            'fields'         => 'ids' // Return only products Ids
        ));

        // Loop through related product Ids
        foreach ( $related_products_ids as $_product_id ) {
            // Update regular price
            update_post_meta( $_product_id, '_regular_price', wc_clean( wp_unslash( $_POST['_regular_price'] ) ) );
        }
    }
}

它应该能行得通

Php相关问答推荐

根据类别在WooCommerce中添加库存数量后缀

无法添加ProxyClass__setInitialized()上的忽略

我的docker—compose lamp stack在软件更新后不工作:Containerconfig错误'''

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

我如何知道BaseController中当前运行的是哪个控制器?

Laravel;Composer安装突然返回选项快捷方式不能为空.

为什么本地主机(Xampp)上的laravel运行速度比普通html站点慢?

FlySystem v3:使用本地适配器时出现问题

首先在WooCommerce我的帐户订单页面中移动操作按钮

上传WordPress模板中的翻译文件(.mo和.po)

Laravel Carbon DiffForHumans选项(年,月,天)

对 WooCommerce 购物车和 checkout 中显示的运输方式进行排序

需要以一种形式执行两个操作

WordPress 分页无法正常工作

Firefox 115 会话行为:为什么页面无法找到要加载的现有会话 ID?

WooCommerce - 调用功能ID不正确.不应直接访问订单属性

在wordpress注销后防止后退操作

服务器遇到内部错误或配置错误,无法完成您的请求

无法在 Laravel 10 中安装 jenssegers/mongodb

从 CSV 文件 seeder 数据库时的额外行