我试图用计算值更新产品元信息。
背后的故事是,我们正在为一个机场出售停车位,我们现有的系统是,一个停车产品有31种变体,用户会输入进出的日期和时间,它会根据停车的天数等来计算价格。
目前,我们必须在设置页面上进行计算,然后手动将这些值复制到产品变体中,如果我们想要快速更改价格,这将是一件乏味的事情。
因此,我在google上找到了get\\u post\\u meta(),并能够获取有关31种变体的所有信息。
$product_attr = get_post_meta( 148 , \'redq_day_ranges_cost\');
print_r($product_attr[0]);
结果如下:
Array
(
[0] => Array
(
[0] => Array
(
[min_days] => 1
[max_days] => 1
[range_cost] => 29.5
)
[1] => Array
(
[min_days] => 2
[max_days] => 2
[range_cost] => 17.25
)
[2] => Array
(
[min_days] => 3
[max_days] => 3
[range_cost] => 13.1667
)
[3] => Array
(
[min_days] => 4
[max_days] => 4
[range_cost] => 11.125
)
[4] => Array
(
[min_days] => 5
[max_days] => 5
[range_cost] => 9.9
)
[5] => Array
(
[min_days] => 6
[max_days] => 6
[range_cost] => 9.0833
)
[6] => Array
(
[min_days] => 7
[max_days] => 7
[range_cost] => 8.5
)
[7] => Array
(
[min_days] => 8
[max_days] => 8
[range_cost] => 8.0625
)
[8] => Array
(
[min_days] => 9
[max_days] => 9
[range_cost] => 7.7222
)
[9] => Array
(
[min_days] => 10
[max_days] => 10
[range_cost] => 7.45
)
[10] => Array
(
[min_days] => 11
[max_days] => 11
[range_cost] => 7.2273
)
[11] => Array
(
[min_days] => 12
[max_days] => 12
[range_cost] => 7.0417
)
[12] => Array
(
[min_days] => 13
[max_days] => 13
[range_cost] => 6.8846
)
[13] => Array
(
[min_days] => 14
[max_days] => 14
[range_cost] => 6.75
)
[14] => Array
(
[min_days] => 15
[max_days] => 15
[range_cost] => 6.6333
)
[15] => Array
(
[min_days] => 16
[max_days] => 16
[range_cost] => 6.5313
)
[16] => Array
(
[min_days] => 17
[max_days] => 17
[range_cost] => 6.4412
)
[17] => Array
(
[min_days] => 18
[max_days] => 18
[range_cost] => 6.3611
)
[18] => Array
(
[min_days] => 19
[max_days] => 19
[range_cost] => 6.2895
)
[19] => Array
(
[min_days] => 20
[max_days] => 20
[range_cost] => 6.225
)
[20] => Array
(
[min_days] => 21
[max_days] => 21
[range_cost] => 6.1667
)
[21] => Array
(
[min_days] => 22
[max_days] => 22
[range_cost] => 6.1136
)
[22] => Array
(
[min_days] => 23
[max_days] => 23
[range_cost] => 6.0652
)
[23] => Array
(
[min_days] => 24
[max_days] => 24
[range_cost] => 6.0208
)
[24] => Array
(
[min_days] => 25
[max_days] => 25
[range_cost] => 5.98
)
[25] => Array
(
[min_days] => 26
[max_days] => 26
[range_cost] => 5.9423
)
[26] => Array
(
[min_days] => 27
[max_days] => 27
[range_cost] => 5.9074
)
[27] => Array
(
[min_days] => 28
[max_days] => 28
[range_cost] => 5.875
)
[28] => Array
(
[min_days] => 29
[max_days] => 29
[range_cost] => 5.8448
)
[29] => Array
(
[min_days] => 30
[max_days] => 30
[range_cost] => 5.8167
)
[30] => Array
(
[min_days] => 31
[max_days] => 31
[range_cost] => 5.7903
)
)
)
但是现在我该如何更新每一个的range\\u成本。
我猜大概是
$newcosts = array(
[0][0][range_cost] => 30,
[0][1][range_cost] => 29,
);
update_post_meta(148, \'redq_day_ranges_cost\', $newcosts);
但我不确定是否会非常感谢任何帮助:)
干杯
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
这有点离题,但无论如何。。。
假设您希望将产品148的第一个range\\u成本更改为5.789,第三个更改为6.890。代码如下:
$product_attr = get_post_meta( 148 , \'redq_day_ranges_cost\');
$product_attr[0][0][\'range_cost\'] = 5.789;
$product_attr[0][2][\'range_cost\'] = 6.890;
update_post_meta(148, \'redq_day_ranges_cost\', $product_attr);