从结帐到我的帐户再返回到Woocommerce的自定义重定向

时间:2018-09-02 08:41:46

标签: php wordpress redirect woocommerce checkout

在WordPress和WooCommerce上有一个网站。目前,当用户购买产品时,我已经具有多种条件的功能:

  1. 如果用户未注册,并且购买了除订购以外的任何产品,那么他会冷静地草拟订单,然后系统会自动为他创建一个帐户。 (完成)

  2. 如果用户已注册但忘记了使用其登录名的输入,那么他也只需在结帐页面上以其登录名登录即可安静地起草订单。 (完成)

  3. 如果用户尚未注册并想购买订阅,则系统会将其重定向到注册页面,然后从该页面转到编辑帐户页面,然后他返回结帐页面。 (完成)

  4. 如果用户已注册,但忘记使用登录名登录,则他想购买订阅。用户重定向到注册页面。他输入登录名和密码。系统将他重定向回结帐页面。 (任务)

我创建的前两个条件是标准的WooCommerce设置。对于第三个条件,我有可以看到的代码:

add_action('template_redirect', 'woo_custom_redirect');

function woo_custom_redirect($redirect) {
// HERE set your product category (can be term IDs, slugs or names)
$category = 'subscriptions';

$found = false;

// CHECK CART ITEMS: search for items from our product category
foreach(WC()->cart->get_cart() as $cart_item) {
        if (has_term($category, 'product_cat', $cart_item['product_id'])) {
                $found = true;
                break;
        }
}

if (!is_user_logged_in() && is_checkout() && $found) {
        wp_redirect('/my-account/edit-account/');                
        exit();
}

if (is_user_logged_in() && !WC()->cart->is_empty() && is_account_page()) {
    wp_redirect( get_permalink( get_option('woocommerce_checkout_page_id') ) );
}

}

对于第四个条件,我添加了代码:

if (is_user_logged_in() &&! WC()->cart->is_empty() && is_account_page ()) {
    wp_redirect (get_permalink (get_option ('woocomm erce_checkout_page_id'))));
}

但是它不能正常工作。第三个条件由于此代码而停止工作。

如何解决?谢谢!


更新:我将其保存在此处。这可以帮助其他用户。该代码可以正常工作。

add_action( 'template_redirect', 'custom_redirections' );
function custom_redirections( $redirect ) {
// HERE set your product category (can be term IDs, slugs or names)
$category = 'subscriptions';

$found = false;

// CHECK CART ITEMS: search for items from our product category
foreach( WC()->cart->get_cart() as $cart_item ) {
    if (has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
        $found = true;
        break;
    }
}

if ( ! is_user_logged_in() && is_checkout() && $found ) {
    wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
    exit();
} elseif ( is_user_logged_in() && is_account_page() && $found  ) {
    wp_redirect( get_permalink( get_option('woocommerce_checkout_page_id') ) );
    exit();
}
}

0 个答案:

没有答案