Woocommerce:收到订单的产品ID

时间:2017-08-31 11:12:00

标签: php wordpress url woocommerce orders

我想更改收到的订单(感谢页面)网址,其中包含已订购商品的ID。

这是class-wc-order.php

中的代码
/**
 * Generates a URL for the thanks page (order received).
 *
 * @return string
 */
public function get_checkout_order_received_url() {
    $order_received_url = wc_get_endpoint_url( 'order-received', $this->get_id(), wc_get_page_permalink( 'checkout' ));

    if ( 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) || is_ssl() ) {
        $order_received_url = str_replace( 'http:', 'https:', $order_received_url );
    }

    $order_received_url = add_query_arg( 'key', $this->get_order_key(), $order_received_url );

    return apply_filters( 'woocommerce_get_checkout_order_received_url', $order_received_url, $product, $this, $product->id );
}

有什么想法吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

This answer在wordpress.stackexchange.com上可以解决此问题

简单来说,只需定义以下钩子即可:

add_filter('woocommerce_get_checkout_order_received_url','override_return_url',10,2);

function override_return_url($return_url,$order){

    //create empty array to store url parameters in 
    $sku_list = array();

    // retrive 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_'.$item['product_id'] . 'sku'] = $product->get_sku();
    }
    //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;
}

然后,您可以将分析目标设置为以正则表达式指向下面的目标

\/checkout\/order-received\/\d+\/\?key=\w+&product_ID_HERE_sku=SKU_HERE


我们可以通过仅包含产品ID来使其更简单:

add_filter('woocommerce_get_checkout_order_received_url','override_return_url',0,2);

function override_return_url($return_url,$order){
  $ids = array();
  foreach($order->get_items() as $key => $item)
  {
    $ids[] = $item['product_id'];
  }
  return add_query_arg('product_ids', join(',', $ids) , $return_url);
}

在这种情况下,正则表达式为:

\/checkout\/order-received\/\d+\/\?key=\w+&product_ids=ID1,ID2,ID3

或针对单个产品ID:

\/checkout\/order-received\/\d+\/\?key=\w+&product_ids=[\w,]*ID[\w,]*