Ahm*_*ati 2 php wordpress cart orders woocommerce
在 Woocommerce 中,我使用以下 URL 的 GET 方法将产品添加到购物车:
http://example.com/cart/?add-to-cart=10
Run Code Online (Sandbox Code Playgroud)
现在我希望能够同时添加一些自定义数据作为产品注释,例如:
http://example.com/cart/?add-to-cart=10¬e=hinote
Run Code Online (Sandbox Code Playgroud)
然后将该“hinote”值保存为购物车商品数据。下订单后,我想将“hinote”保存在订单项数据中,并将其显示为自定义订单项数据。
那可能吗?
任何帮助表示赞赏。
是的,这是可能的,而且安静简单\xe2\x80\xa6 尝试以下代码:
\n\n// Add custom note as custom cart item data\nadd_filter( \'woocommerce_add_cart_item_data\', \'get_custom_product_note\', 30, 2 );\nfunction get_custom_product_note( $cart_item_data, $product_id ){\n if ( isset($_GET[\'note\']) && ! empty($_GET[\'note\']) ) {\n $cart_item_data[\'custom_note\'] = sanitize_text_field( $_GET[\'note\'] );\n $cart_item_data[\'unique_key\'] = md5( microtime().rand() );\n }\n return $cart_item_data;\n}\n\n\n// Display note in cart and checkout pages as cart item data - Optional\nadd_filter( \'woocommerce_get_item_data\', \'display_custom_item_data\', 10, 2 );\nfunction display_custom_item_data( $cart_item_data, $cart_item ) {\n if ( isset( $cart_item[\'custom_note\'] ) ){\n $cart_item_data[] = array(\n \'name\' => "Note",\n \'value\' => $cart_item[\'custom_note\'],\n );\n }\n return $cart_item_data;\n}\n\n// Save and display product note in orders and email notifications (everywhere)\nadd_action( \'woocommerce_checkout_create_order_line_item\', \'add_custom_note_order_item_meta\', 20, 4 );\nfunction add_custom_note_order_item_meta( $item, $cart_item_key, $values, $order ) {\n if ( isset( $values[\'custom_note\'] ) ){\n $item->update_meta_data( \'Note\', $values[\'custom_note\'] );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
\n\n\n| 归档时间: |
|
| 查看次数: |
2963 次 |
| 最近记录: |