【问题标题】:How to add variables to custom thank you url in Woocommerce如何在 Woocommerce 中将变量添加到自定义谢谢 url
【发布时间】:2018-09-25 16:55:57
【问题描述】:

我能够按照重定向教程https://businessbloomer.com/resolved-woocommerce-redirect-custom-thank-page/

但似乎无法将 woocommerce 变量添加到 url。

我想要这样的东西 > http://example.com?EO_ID=M180924-678922&Product_Code=vssx2&Quantity=1&Email=billing_email@gmail.com'

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce query-string orders


    【解决方案1】:

    链接代码已过时,无法处理任何 URL 查询字符串。您应该需要进行更改并调整代码。

    注意:订单可以有许多商品,因此为了适合您的情况我们只使用第一个商品

    试试这个(你需要为$path变量定义你的URL路径):

    add_action( 'woocommerce_thankyou', 'thankyou_custom_redirect', 5, 1 );
    function thankyou_custom_redirect( $order_id ){
        // Get the WC_Order object instance
        $order         = wc_get_order( $order_id );
    
        // Order data
        $order_key     = $order->get_order_key(); // Get order key (if needed)
        $transaction_id= $order->get_transaction_id(); // Get order key (if needed)
        $billing_email = $order->get_billing_email(); // Get billing email
        $order_num     = $order->get_order_number(); // Get order number
        $order_date    = $order->get_date_created(); // Get order creation date
    
        // Order item data (first item)
        $order_items = $order->get_items(); // Get order items
        $first_item  = reset($order_items); // Keep the first Item
        $item_qty    = $first_item->get_quantity(); // Item quantity
        $product     = $first_item->get_product(); // Get the WC_Product object instance
        $sku         = $product->get_sku(); // Get the product code (SKU)
    
        // Build your query string
        $query_string  = '?EO_ID=' . $order_date->date('ymd') . -rand(pow(10, 5), pow(10, 6)-1);
        $query_string .= '&Product_Code=' . $sku;
        $query_string .= '&Quantity=' . $item_qty;
        $query_string .= '&Email=' . $billing_email;
    
        $path = '/custom-path/'; // <=== HERE define the url path (without the domain)
        $url  = home_url( $path . $query_string );
    
        // Not for failed orders
        if ( ! $order->has_status( 'failed' ) ) {
            wp_redirect( $url );
            exit();
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 所以它有 'M180924-678920' ,这只是将订单日期与 6 个随机字符一起拉出。我将如何将它注入到字符串中。非常感谢您的帮助。
    • @MatthewLoeffler 我已经更新了答案……检查一下。
    • @MatthewLoeffler 当然有一些变化......您将需要添加一个 foreach 循环,以遍历所有订单项目,而不是仅循环第一个项目。现在我不知道如何构建包含多个项目的查询字符串……
    • 这就是我卡住的地方。我知道我必须创建一个循环,只是不确定如何在 url 字符串中构建它.. 或者发布到多个字符串
    • @MatthewLoeffler 我真的不知道您对查询字符串的要求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 2021-10-16
    • 1970-01-01
    • 2017-01-21
    • 2019-01-04
    • 1970-01-01
    相关资源
    最近更新 更多