添加额外信息以订购

时间:2015-08-27 22:16:35

标签: wordpress woocommerce

我正在以编程方式将产品添加到购物车。除此之外,不知怎的,我想在订单中存储一些额外的信息(数组)。当客户端完成订单时,我想通过一些WordPress操作访问该信息。将产品添加到购物车后,我必须立即执行此操作,因为如果用户没有立即完成订单,信息可能会在此之后发生变化。有没有办法,我可以做到这一点,而不是让数据库工作?

1 个答案:

答案 0 :(得分:1)

您应该使用WooCommerce Cart Item Meta APIWooCommerce Order Item Meta API

你像这样使用它们:

// Add to cart item
// This is triggered on add to cart
add_filter('woocommerce_add_cart_item_data', 'my_add_cart_item_data', 10, 2);

function my_add_cart_item_data( $cart_item_meta, $product_id ) {

    //Here we can easily filter what values should be added to what products using the $product_id 
    $cart_item_meta['my_meta_key'] = 'meta value';

    return $cart_item_meta;
}

// Add to order item when the cart is converted to an order
// This is triggered when the order is created
add_action('woocommerce_add_order_item_meta', 'my_order_item_meta'), 10, 2);

function my_order_item_meta( $item_id, $values, $cart_item_key ) {

    // The value stored in cart above is accessable in $values here
    woocommerce_add_order_item_meta( $item_id, 'meta_key', $values['my_meta_key'] );    

    //Or add what ever you want
    $meta_value = 'value';
    woocommerce_add_order_item_meta( $item_id, 'meta_key', $meta_value );   
}

我希望有所帮助。

相关问题