Php 在感谢页面上添加产品ID和订单号作为查询字符串
url当前看起来像这样 site.com/checkout/order received/827/?key=wc\u order\an9wofnlcbxm&product\u id=211 我知道827是订单号,但我需要它作为字符串,以便将数据传递给表单 我可以在URL中获取产品id,但似乎无法添加订单号。有更好的解决办法吗?如何添加这两个查询字符串?感谢您的帮助Php 在感谢页面上添加产品ID和订单号作为查询字符串,php,wordpress,woocommerce,query-string,orders,Php,Wordpress,Woocommerce,Query String,Orders,url当前看起来像这样 site.com/checkout/order received/827/?key=wc\u order\an9wofnlcbxm&product\u id=211 我知道827是订单号,但我需要它作为字符串,以便将数据传递给表单 我可以在URL中获取产品id,但似乎无法添加订单号。有更好的解决办法吗?如何添加这两个查询字符串?感谢您的帮助 add_filter( 'woocommerce_get_checkout_order_received_url', 'custom
add_filter( 'woocommerce_get_checkout_order_received_url', 'custom_add_product_id_in_order_url', 10, 2 );
function custom_add_product_id_in_order_url( $return_url, $order ) {
// Create empty array to store url parameters in
$sku_list = array();
// Retrieve products in order
foreach($order->get_items() as $key => $item){
$product = wc_get_product($item['product_id']);
//get sku of each product and insert it in array
$sku_list['product_id'] = $product->get_id();
}
// Build query strings out of the SKU array
$url_extension = http_build_query($sku_list);
// Append our strings to original url
$modified_url = $return_url.'&'.$url_extension;
return $modified_url;
}
你的代码在WooCommerce3之后有点过时了。要将产品ID和订单号添加为WooCommerce order received页面的查询字符串,请改用以下命令:
add_filter( 'woocommerce_get_checkout_order_received_url', 'add_product_ids_in_order_received_url', 10, 2 );
function add_product_ids_in_order_received_url( $return_url, $order ) {
$product_ids = array(); // Initializing
// Retrieve products in order
foreach( $order->get_items() as $item ){
$product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
}
return $return_url . '&number=' . $order->get_order_number() . '&ids=' . implode( ',', $product_ids );
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作
现在,要从URL获取产品ID和订单号,您将使用以下内容:
// Get product Ids in an array
if( isset($_GET['ids']) && ! empty($_GET['ids']) ){
$product_ids_array = explode( ',', esc_attr($_GET['ids']) );
}
// Get order number
if( isset($_GET['number']) && ! empty($_GET['number']) ){
$order_number = esc_attr($_GET['number']);
}
非常感谢你,Loic,你的代码总是很有魅力