【问题标题】:WooCommerce: Bookings : Trying to update dB for pricing tab in productsWooCommerce:预订:尝试更新产品中定价选项卡的 dB
【发布时间】:2021-11-25 07:38:31
【问题描述】:

当我使用后端接口将数组添加到 _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 次迭代,而不是仅仅更改值......虽然不知道如何修复我的代码。任何指针将不胜感激!

【问题讨论】:

    标签: php arrays database woocommerce-bookings


    【解决方案1】:

    好的,最终解决了这个问题。首先,我必须制作一个嵌套数组,其次我必须将键和值之间的逗号更改为=>

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

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

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 1970-01-01
      • 2021-07-24
      • 2018-01-31
      • 1970-01-01
      • 2020-10-29
      • 2021-08-01
      • 2018-05-29
      • 1970-01-01
      相关资源
      最近更新 更多