该网址目前看起来像这样,
site.com/checkout/order-received/827/?key=wc_order_AN9woFNlCBbXM&product_id=211
我知道827是订单号,但为了将数据传递到表单,我需要将它作为字符串。
我可以在URL中获得产品ID,但似乎无法添加订单号。有更好的解决方案吗?如何添加两个查询字符串?任何帮助表示赞赏。
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收到订单页面的查询字符串,请改用以下内容:
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']);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句