php - 提交订单请求后为产品添加购物车元值

标签 php wordpress woocommerce custom-fields orders

我有一个多供应商餐厅 WooCommerce 网站。一个人最多可以从一家餐厅点餐。在一家餐厅,当我将产品添加到购物车时,我添加了一个额外的元字段 restaurant_id,这是该餐厅的 ID。在购物车中,所有产品都具有相同的 restaurant_id

即:我在购物车中有两个具有 product_id 的项目:1213 但它们具有相同的 restaurant_id : 366

我需要将此 restaurant_id 添加为 order-completeorder-process 操作中的元字段。因为我需要显示餐厅客户帐户页面上的名称。

或者有什么简单的方法吗?

我试过下面的代码作为测试

add_action( 'woocommerce_checkout_update_order_meta', 'add_field_to_order' );
function add_field_to_order( $order_id ) {
    update_post_meta( $order_id, 'new_field', 'new_value' );
}

但是它没有添加任何meta-fieldmeta-value 到order

最佳答案

—更新—

据我所知,这个自定义字段已经存在,并且您在购物车中获得了这个 restaurant_id 值。所以你想在你的查看订单中显示它(谢谢你和我的帐户页面)并且可能在电子邮件中......

这是代码:

//
// ADD HIDDEN IMPUT FIELDS TO THE CHECKOUT
//
add_action( 'woocommerce_after_order_notes', 'checkout_custom_hidden_imput_field' );
function checkout_custom_hidden_imput_field( $checkout ) {
    foreach(WC()->cart->get_cart() as $item){
        $restaurant_id = $item['restaurant_id'];
        break;
    }
    echo '<div id="custom_checkout_fields" class="custom-hidden-checkout-field">
        <input type="hidden" id="restaurant_id" name="restaurant_id" value="'.$restaurant_id.'" />
    </div>';
}

//
// SAVE THE ORDER META WITH FIELD VALUE
//
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );

function custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['restaurant_id'] ) ) {
        add_post_meta( $order_id, '_restaurant_id', $_POST['restaurant_id'] );
    }
}

//
// DISPLAY FIELD VALUE ON THE ORDER EDIT PAGE (NOT IN CUSTOM FIELDS METABOX)
//
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta($order){
    $restaurant_id = get_post_meta( $order->id, '_restaurant_id', true );
    if ( ! empty( $restaurant_id ) ) {
        echo '<p><strong>'. __("Restaurant ID", "woocommerce").':</strong> ' . $restaurant_id . '</p>';
    }
}

//
// ADD THE INFORMATION AS META DATA SO THAT IT CAN BE SEEN AS PART OF THE ORDER
//
add_action('woocommerce_add_order_item_meta','custom_add_values_to_order_item_meta', 1, 3 );
function custom_add_values_to_order_item_meta( $item_id, $values, $cart_item_key ) {
    $restaurant_id = get_post_meta( $order->id, '_restaurant_id', true );
    // lets add the meta data to the order!
    wc_add_order_item_meta($item_id, '_restaurant_id', $restaurant_id, true);
}

代码进入您的事件子主题(或主题)的任何 php 文件或任何插件 php 文件。

代码已经过测试并且可以工作。

关于php - 提交订单请求后为产品添加购物车元值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40838049/

相关文章:

php - Mod_rewrite 和不同顺序的多个变量

javascript - 从地址变量到谷歌地图

wordpress - htaccess 条件特定页面

html - 如何将自定义字段添加到 Woocommerce 中的类别?

php - 向 Woocommerce 3.0 添加股票期权

woocommerce - 错误: An error occurred in the request and at the time were unable to send the consumer data

php - Twig :无法覆盖包含文件中的 block

php - 类作用域和对象作用域之间的区别

html - ie7 错误 - div 在某些 wordpress 页面中滑出屏幕

javascript - css 链接到 .html 文件