【问题标题】:WooCommerce Custom Field does not save to DB ... sometimesWooCommerce 自定义字段不保存到数据库...有时
【发布时间】:2017-02-15 10:56:00
【问题描述】:

我在 Woo-commerce 网站上收到了大约 10,000 个订单。大约 0.4% 没有保存所需的自定义字段“custom_location”。我不知道这是怎么可能的,也找不到重现的方法。

另外,我使用字段的选定值将辅助值保存到数据库。即使数据库中的“custom_location”为空,辅助值也会正确保存。显然, $_POST['custom_location'] 包含有效数据,但没有保存......为什么?

// Here I create the custom select field
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields');
function custom_override_checkout_fields( $fields ) {
    // <select> options
    $args = array('post_type' => 'MY_CUSTOM_POST_TYPE',
                'posts_per_page' => -1,
                'order' => 'ASC',
                'post_status' => 'publish',
                'suppress_filters' => true);
    $locations = get_posts($args);
    $ops = array('default' => 'Select a location');
    foreach($locations as $l) {
        $ops[$l->ID] =  __($l->post_title, 'woocommerce' );
    }
    // Add 'custom_location' field to 'shipping'
    $fields['shipping'] = array( 'custom_location' => array(
        'label'       => __('Location', 'woocommerce'),
        'placeholder' => _x('', 'empty', 'woocommerce'),
        'required'    => true,
        'clear'       => false,
        'type'        => 'select',
        'options'     => $ops
        ))
        + $fields['shipping'];

     return $fields;
}
// Save related data
add_action( 'woocommerce_checkout_update_order_meta', 'save_extra_data' );
function save_extra_data( $order_id ) {
    $loc = $_POST['custom_location'];

    // This was saved correctly to DB even when 'custom_location' is null in DB!
    $meta = get_post_meta($loc, 'extra-shipping-data', true);
    update_post_meta( $order_id, '_shipping_extra', $meta ); 
}

【问题讨论】:

  • foreach 循环中使用的$locations 变量在哪里定义?
  • 我在代码中添加了 $locations 变量。这是我通过“get_posts()”获得的一系列帖子

标签: wordpress woocommerce


【解决方案1】:

如果客户帐单和送货地址相同,您的位置将被保存为空。在您的代码中,缺少 locations 的声明。稍作改动,您就可以最大限度地减少代码和工作量。确保将 ops 替换为您的选项。

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields');
function custom_override_checkout_fields( $fields ) {
    // <select> options
    $ops = array('default' => 'Select a location', 'location1' => 'Location 1');

    // Add 'custom_location' field to 'shipping'
    $fields['shipping'] = array( 'shipping_custom_location' => array(
        'label'       => __('Location', 'woocommerce'),
        'placeholder' => _x('', 'empty', 'woocommerce'),
        'required'    => true,
        'clear'       => false,
        'type'        => 'select',
        'options'     => $ops
     ))
    + $fields['shipping'];

    return $fields;
}

【讨论】:

  • 在这个应用程序中,送货地址总是与帐单不同,所以我们不允许它是可选的。我不久前添加了位置声明,很抱歉你错过了。 (顺便说一句,$ops 确实需要管理员可以编辑,这就是 get_posts 代码存在的原因)
  • 您是否尝试将 id 从custom_location 更改为shipping_custom_location?它应该可以工作。
  • @LearnWoo 你的评论真的很有帮助,我解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
相关资源
最近更新 更多