0

I have added a product in cart programatically and then want to redirect the user to checkout page and skip the product page and cart page. Product is added successfully but it does not go to checkout page and displays a blank page with no errors. This happens only when I clear my browser cashe. second time it works fine. The user comes from a custom link to product page. I want that when user is on product page, the product should automatically added and user should be redirected to checkout page. Here is my code

add_action('template_redirect','redirect_if_cart_loaded', 10, 2);
  function redirect_if_cart_loaded(){
    global $woocommerce;
  if(is_product() && sizeof( $woocommerce->cart->cart_contents ) == 0){

    echo $pooduct_id = get_the_ID();

    //check if product already in cart
    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
     $woocommerce->cart->empty_cart();
}
    // if product not found, add it

        WC()->cart->add_to_cart( $pooduct_id );
        echo sizeof( WC()->cart->get_cart() ); // this gives 1. means product is added
    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
     wp_redirect( WC()->cart->get_checkout_url() );
     exit;
}


    }
  }
Rizwi
  • 117
  • 1
  • 13
  • What version of woocommerce do you use? – Cedric Oct 09 '18 at 13:35
  • If you have a blank page, you can have a look at your apache/ngninx/php error logs, you are likely to see a php fatal error somewhere – Cedric Oct 09 '18 at 13:36
  • what is the result of echo WC()->cart->get_checkout_url() ; ? It might be an empty string; and your redirect would not even be used – Cedric Oct 09 '18 at 13:37
  • any progress in your development ? – Cedric Oct 10 '18 at 07:56
  • that functions prints checkout page url. However you solution has worked @ced – Rizwi Oct 10 '18 at 15:00
  • but its not working with wc_get_checkout_url(), it worked with get_checkout_url() – Rizwi Oct 10 '18 at 15:01
  • If you are using Woocommerce 2, use get_checkout_url. If you are using Woocommerce 3; it is advised to use wc_get_checkout_url , https://docs.woocommerce.com/wc-apidocs/annotation-group-deprecated.html – Cedric Oct 10 '18 at 15:07
  • But whatever works for you works for you :) – Cedric Oct 10 '18 at 15:07
  • thanks @Cedric. I am having another problem with woocommerce. Hope you can help me. How to get value of woocommerce stripe payment fields via jquery when I hit complete payment button? Remember woocommerce stipe payment gateway form in integrated in iframe. Should I create a new ticket for you? – Rizwi Oct 11 '18 at 15:29
  • The best way to get a quick answer is to create a new 'Question'/ticket :) – Cedric Oct 11 '18 at 15:32
  • can you check this question please? https://stackoverflow.com/questions/52764202/how-to-get-value-of-woocommerce-stripe-payment-form-that-is-integrated-in-iframe – Rizwi Oct 11 '18 at 15:56

1 Answers1

1

Use

if ( !function_exists( 'wc_get_checkout_url' ) ) { 
    require_once '/includes/wc-core-functions.php'; 
} 

$result = wc_get_checkout_url(); 

use wc_get_checkout_url instead of get_checkout_url, as get_checkout_url is deprecated

Cedric
  • 5,135
  • 11
  • 42
  • 61