我创建了一个使用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
答案 0 :(得分:1)
在woocommerce(版本2.1.12)中:
woocommerce /包括/ WC-产品的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" meta甚至删除瞬态以更新产品价格。
删除产品临时呼叫:
wc_delete_product_transients( $product_id );
答案 1 :(得分:0)
在上面的代码中,可能不会分配$ 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。
指定值答案 2 :(得分:0)
我认为你们两个都比现在要复杂得多。
首先,这项检查的目的是什么:
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);
}
}
答案 3 :(得分:0)
这是因为 update_post_meta
- 更新其他数据库选项卡。它更新 wp_postmeta
。您需要更新 wp_wc_product_meta_lookup
中的值。我为此使用了 SQL 命令。