WooCommerce get order quantity in thank you page and redirect

社会主义新天地 提交于 2019-12-08 03:18:27
add_action('template_redirect', 'wc_custom_redirect_after_purchase');

function wc_custom_redirect_after_purchase() {
    global $wp;

    if (is_checkout() && !empty($wp->query_vars['order-received'])) {

        $order = new WC_Order($wp->query_vars['order-received']);

        $quantity = 0;
        if (count($order->get_items()) > 0) {
            foreach ($order->get_items() as $item) {

                if (!empty($item)) {
                    $quantity+= $item['qty'];
                }
            }
        }

        switch ($quantity) {
            case 1:
                wp_redirect('http://www.facebook.com/'); // exampe site
                break;
            case 2:
                wp_redirect('http://www.youtube.com/');  // exampe site
                break;
            default:

                break;
        }
        exit();
    }
}

Try this code snippet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!