使用 Woocommerce 3 中的 GET 方法使用来自 url 的自定义数据添加到购物车
Add to cart with custom data from an url using GET method in Woocommerce 3
在 Woocommerce 中,我使用 URL 中的 GET 方法将产品添加到购物车,例如:
http://example.com/cart/?add-to-cart=10
现在我希望能够同时添加一些自定义数据作为产品说明,例如:
http://example.com/cart/?add-to-cart=10¬e=hinote
然后将该 "hinote" 值保存为购物车项目数据。下订单后,我想将 "hinote" 保存在订单项目数据中,并将其作为自定义订单项目数据显示在各处。
这可能吗?
感谢任何帮助。
是的,这是可能的,而且很简单……试试下面的代码:
// Add custom note as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'get_custom_product_note', 30, 2 );
function get_custom_product_note( $cart_item_data, $product_id ){
if ( isset($_GET['note']) && ! empty($_GET['note']) ) {
$cart_item_data['custom_note'] = sanitize_text_field( $_GET['note'] );
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
// Display note in cart and checkout pages as cart item data - Optional
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
if ( isset( $cart_item['custom_note'] ) ){
$cart_item_data[] = array(
'name' => "Note",
'value' => $cart_item['custom_note'],
);
}
return $cart_item_data;
}
// Save and display product note in orders and email notifications (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_note_order_item_meta', 20, 4 );
function add_custom_note_order_item_meta( $item, $cart_item_key, $values, $order ) {
if ( isset( $values['custom_note'] ) ){
$item->update_meta_data( 'Note', $values['custom_note'] );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
在 Woocommerce 中,我使用 URL 中的 GET 方法将产品添加到购物车,例如:
http://example.com/cart/?add-to-cart=10
现在我希望能够同时添加一些自定义数据作为产品说明,例如:
http://example.com/cart/?add-to-cart=10¬e=hinote
然后将该 "hinote" 值保存为购物车项目数据。下订单后,我想将 "hinote" 保存在订单项目数据中,并将其作为自定义订单项目数据显示在各处。
这可能吗?
感谢任何帮助。
是的,这是可能的,而且很简单……试试下面的代码:
// Add custom note as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'get_custom_product_note', 30, 2 );
function get_custom_product_note( $cart_item_data, $product_id ){
if ( isset($_GET['note']) && ! empty($_GET['note']) ) {
$cart_item_data['custom_note'] = sanitize_text_field( $_GET['note'] );
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
// Display note in cart and checkout pages as cart item data - Optional
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
if ( isset( $cart_item['custom_note'] ) ){
$cart_item_data[] = array(
'name' => "Note",
'value' => $cart_item['custom_note'],
);
}
return $cart_item_data;
}
// Save and display product note in orders and email notifications (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_note_order_item_meta', 20, 4 );
function add_custom_note_order_item_meta( $item, $cart_item_key, $values, $order ) {
if ( isset( $values['custom_note'] ) ){
$item->update_meta_data( 'Note', $values['custom_note'] );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。