【问题标题】:Woocommence `update_post_meta` is not updating database valuesWoocommerce `update_post_meta` 未更新数据库值
【发布时间】:2021-07-21 08:26:53
【问题描述】:

我创建了一个插件,它使用update_post_meta 函数来更新产品的变化价格。

如果我有一个产品 x (id:5) 和一个变体 y (id:400) 并且我运行 update_post_meta(400,"_regular_price",13.00); 它不会更新数据库。这非常奇怪,因为当我点击Edit Product(wp-admin)时,更新后的价格13.00 出现在变体面板中,我必须点击Update 才能更新以供客户查看。这是常规行为吗?如果是,如何在 update_post_meta 函数执行后立即更新数据库?

(Image) Price after update_post_meta()摘要页面

.

(Image) Price after same update. Edit Product page

这是我进行批量更新的代码

// $attribute_value/$variation_value are set correctly!
while ($loop->have_posts() ) : $loop->the_post();
    global $product;
    $variations = new WC_Product_Variable($product->post->ID);
    $variations = $variations->get_available_variations();
    foreach ($variations as $key => $variation){
        foreach ($variation["attributes"] as $key => $attribute_value):
            if($attribute_value == $variation_value):
                update_post_meta( $variation['variation_id'], '_regular_price', $regular_price);
            endif;
        endforeach;
    }
endwhile;

我问过同样的问题,但在 Wordpress 论坛上没有回复 http://wordpress.org/support/topic/update_post_meta-is-not-updating-the-actual-values?replies=1#post-5742842

【问题讨论】:

  • 你解决了这个问题吗?我遇到了同样的问题...

标签: php wordpress woocommerce


【解决方案1】:

在 woocommerce(版本 2.1.12)中:

woocommerce/includes/wc-product-functions.php:417

$regular_price = get_post_meta( $product_id, '_regular_price', true );

update_post_meta( $product_id, '_price', $regular_price );
update_post_meta( $product_id, '_sale_price', '' );
update_post_meta( $product_id, '_sale_price_dates_from', '' );
update_post_meta( $product_id, '_sale_price_dates_to', '' );

wc_delete_product_transients( $product_id );

看来您必须更新“_price”元数据,甚至删除瞬态才能更新产品价格。

删除产品临时调用:

wc_delete_product_transients( $product_id );

【讨论】:

  • 我可以在代码中完美地更改价格,而无需删除瞬态。
  • 为什么要在这里重复别人已经说过的话?
  • 我不确定,但它不起作用。命令wc_delete_product_transients 似乎什么也没做。它显示在“编辑产品”上,但不在前端。
【解决方案2】:

在上面的代码中,$variation_value 变量值,可能没有被赋值。因此,如果不满足条件并且不执行 update_post_meta(),则有可能。

我们可以使用下面的代码来进行批量更新。

<?php

    $get_all_products_query = "SELECT `ID`
                FROM `". $wpdb->prefix . "posts`
                WHERE `post_type` LIKE 'product'
                AND `post_status` = 'publish'
                ORDER BY `ID` DESC";

     $get_all_products_result = $wpdb->get_results($get_all_products_query);

     $regular_price = '';

     if(!empty($get_all_products_result))
     {
     foreach($get_all_products_result as $single_product)
     {
         $product_parent_id = $single_product->ID;

        //Get all variations of single product

        $query = "SELECT `post_id`
              FROM `" . $wpdb->prefix . "postmeta`
              WHERE `meta_key` = 'attribute_pa_product'
              AND `post_id`
              IN (
              SELECT `ID`
              FROM `" . $wpdb->prefix . "posts`
              WHERE `post_parent` = " . $product_parent_id . "
              )";

        $variation_result = $wpdb->get_results($query);

        if(!empty($variation_result))
        {
            //As one product may have multiple variation

            foreach($variation_result as $single_variation)
            {
              $post_id = $single_variation->post_id;

                          update_post_meta( $post_id, '_regular_price', $regular_price);
            }
        }
     }
     }


?>

为常规价格变量 $regular_price 赋值。

【讨论】:

  • 最好的做法是尽可能避免运行手动 SQL 查询。首要任务应该是使用内置函数,如 get_available_variations(),第二个选择应该是 WP_query。只有在极少数情况下,当您需要进行更复杂的查询时,您才应该运行自定义 SQL。
【解决方案3】:

我认为你们俩都让这件事变得比需要的复杂得多。

首先,这个检查的目的是什么:

if($attribute_value == $variation_value)

$variation_value 变量根本没有被设置,因此测试失败并且update_post_meta() 不运行。

我将您的代码简化为这个(经过测试并且有效):

//Setup WP_Query
$args = array(
    //Set any arguments you want here to select the products
    'post_type' => 'product', //Make sure it is the correct post type
    'post__in'  => array(157, 156) //Example post IDs
);

$loop = new WP_Query($args);

//The price to set
$regular_price = 110;

//Loop through query results
while ($loop->have_posts() )
{
    //Setup post data
    $loop->the_post();

    //Instantiate the product class 
    $product = new WC_Product_Variable(get_the_ID());

    //Get all variations
    $variations = $product->get_available_variations();

    //Loop though variations and update
    foreach ($variations as $variation){
        update_post_meta( $variation['variation_id'], '_regular_price', $regular_price);
    }

}

【讨论】:

  • 我应该提到,$attribute_value$variation_value 是事先设置好的,并且工作正常,我的重点是 update_post_meta,我将尝试 wc_delete_product_transients @JOHn-e。
【解决方案4】:

这是因为update_post_meta - 更新其他数据库选项卡。它更新wp_postmeta。您需要更新wp_wc_product_meta_lookup 中的值。我为此使用了 SQL 命令。

【讨论】:

    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2017-08-06
    相关资源
    最近更新 更多