【问题标题】:Get ordered product details (ID, quantity) in functions.php file of theme - WooCommerce在主题的functions.php文件中获取订购的产品详细信息(ID,数量) - WooCommerce
【发布时间】:2018-05-19 08:13:19
【问题描述】:

我正在使用 WordPress 和 WooCommerce 作为全球支付解决方案来处理我的订单。

付款后我将客户重定向到自定义感谢页面...我通过在当前活动主题的“functions.php”文件中添加此代码来重定向他们:

add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
    global $wp;
    if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
        wp_redirect( 'http://example.com/success.php' );
        exit;
    }
}

我正在尝试将“ORDERED”产品 ID 添加到 URL。

所以我的代码现在是这样的:

add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
    global $wp;
    global $product;
    if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
        $id = $product->get_id();
        wp_redirect( 'http://example.com/success.php?pid='.$id );
        exit;
    }
}

下单成功后重定向很好,但$id的值为空。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    试试这个代码;

    add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
    
    function woo_custom_redirect_after_purchase() {
        global $wp;
        global $product;
        if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
            $order_id = isset( $wp->query_vars['order-received'] ) ? intval( $wp->query_vars['order-received'] ) : 0;
            $order = new WC_Order( $order_id );
            $items = $order->get_items();
            foreach ( $items as $item ) {
                $product_id = $item['product_id'];
            }
    
            wp_redirect( 'http://example.com/success.php?pid='.$product_id );
            exit;
        }
    }
    

    【讨论】:

    • 效果很好!非常感谢你。我也使用$product_quantity = $item['qty']; 来获取订购数量:)
    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 2015-04-19
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多