WooCommerce在感谢页面获取订单数量并重定向

问题描述 投票:1回答:1

我所做的是我在购买后重定向页面,但现在我想得到订购数量的值,以便我可以出于某种原因将它用于我的switch语句。如何获得物品的订购数量的价值?

function wc_custom_redirect_after_purchase() {
global $wp;

   if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
            //wp_redirect( 'http://grimmindustries.com.au/matchmaker/' );

    $quantity =  ; // ordered quantity that I need to use to switch statement to redirect

    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();
        }


}
php wordpress woocommerce orders
1个回答
1
投票
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();
    }
}

试试这个code snippet

© www.soinside.com 2019 - 2024. All rights reserved.