我能够完成,下面是我使用的代码:
// To create a new product meta data with the page from which the product is added:
function custom_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$page_url = wp_get_referer();
$page_id = url_to_postid( $page_url );
$cart_item_data[\'page-of-addition\'] = $page_id;
return $cart_item_data;}
add_filter( \'woocommerce_add_cart_item_data\', \'custom_to_cart_item\', 10, 3 );
如果要在订单页上显示该值:
function add_addition_page_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( empty( $values[\'page-of-addition\'] ) ) {
return;
}
$item->add_meta_data( __( \'Page of addition\', \'wpi18n\' ), $values[\'page-of-addition\'] );
}
add_action( \'woocommerce_checkout_create_order_line_item\', \'add_addition_page_to_order_items\', 10, 4 );
非常感谢米兰Hirpara。我还用一些共享的内容补充了代码
iconic