首页 > 解决方案 > WooCommerce:预订:尝试更新产品中定价选项卡的 dB

问题描述

当我使用后端接口向其中添加一个数组时,_wc_bookings_pricing meta_key我可以看到它被保存如下:

a:1:{i:0;a:7:{s:4:"type";s:6:"custom";s:4:"cost";s:1:"5";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:10:"2021-10-26";s:2:"to";s:10:"2021-10-27";}}

我已经编写了一些代码来更改从前端更新该值,但我很难让它正确格式化。

这是我的代码(不是表单,只是表单中的值和用于更新该值的 php)

$type = isset($_POST['input_1']) ? $_POST['input_1'] : null;
$cost = isset($_POST['input_25']) ? $_POST['input_25'] : null;
$modifyer = isset($_POST['input_20']) ? $_POST['input_20'] : null;
$base_cost = isset($_POST['input_24']) ? $_POST['input_24'] : null;
$base_modifyer = isset($_POST['input_20']) ? $_POST['input_20'] : null;
$date_from = isset($_POST['input_2']) ? $_POST['input_2'] : null;
$date_to = isset($_POST['input_3']) ? $_POST['input_3'] : null;
                        
$response = array('type', $type, 'cost', $cost, 'modifyer', $modifyer, 'base_cost', $base_cost, 'base_modifyer', $base_modifyer, 'from', $date_from, 'to', $date_to);
update_field('_wc_booking_pricing', $response, 379);

我正在更新正确的单元格,但它正在为每个参数创建一个新的迭代。这是我使用前端表单时得到的:

a:14:{i:0;s:4:"type";i:1;s:6:"custom";i:2;s:4:"cost";i:3;s:1:"5";i:4;s:8:"modifyer";i:5;s:1:"0";i:6;s:9:"base_cost";i:7;s:1:"0";i:8;s:13:"base_modifyer";i:9;s:1:"0";i:10;s:4:"from";i:11;s:10:"2021-10-26";i:12;s:2:"to";i:13;s:10:"2021-10-27";}

所以我可以看到我实际上是在创建 14 次迭代,而不是仅仅更改值......虽然不知道如何修复我的代码。任何指针将不胜感激!

标签: phparraysdatabasewoocommerce-bookings

解决方案


好的,最终弄清楚了这一点。首先,我必须创建一个嵌套数组,其次我必须将键和值之间的逗号更改为=>

所以正确的$response数组应该是:

array (
    array ('type' => $type, 'cost' => $cost, 'modifyer' => $modifyer, 'base_cost' => $base_cost, 'base_modifyer' => $base_modifyer, 'from' => $date_from, 'to' => $date_to)
);

推荐阅读