Make WC_Cart add_to_cart method working for guests in Woocommerce












1















Im trying to get my code to work. Searching for hours now.
I found similar questions like this one. But unfortunately noone seems to find a solution.



In my custom plugin I want to add a specific item to the WC cart and redirect the user directly to the checkout. As a logged in user it works like a charm but for guests it shows a blank page on checkout (woocommerce_checkout shortcode seems to return nothing in this case).
So I came up with a check if the cart iss till empty. Apparently it is because the "cart-page" shows "There are no items in the cart yet".



In code i check if the cart is still empty but it tells me its not!



Here is my code:



if(!$wooID = $wpdb->get_var("SELECT wooID FROM ".$wpdb->prefix."ceb_events WHERE id = $event")) die("ERROR GETTING WOOID");

WC()->cart->empty_cart();

if(!WC()->cart->add_to_cart( $wooID, 1 )) die("CART GOT NOT UPDATED. THERE IS AN ERROR 1.");

if(WC()->cart->get_cart_contents_count() == 0) die("CART GOT NOT UPDATED. THERE IS AN ERROR 2.");

//Here follows the redirect to checkout page


The code runs without errors. And it works 100% as a logged in user / admin. Just not as guest, even tho I allowed the guest checkout in the woocommerce settings.










share|improve this question

























  • Heyja, There is probably nothing wrong with the code. It works fine as logged in user. Its just the function add_to_cart from WooCommerce thats seems to create a problem after adding an item to cart when there is no logged in user.

    – xDrago
    Nov 21 '18 at 22:38











  • Wow yea, it works! Thank you very much, youre my man! Can you explain whats going on?

    – xDrago
    Nov 21 '18 at 23:34













  • Oh i Figured that my admin area doesnt work anymore when i add your code. HTTP 500 Error.

    – xDrago
    Nov 21 '18 at 23:44
















1















Im trying to get my code to work. Searching for hours now.
I found similar questions like this one. But unfortunately noone seems to find a solution.



In my custom plugin I want to add a specific item to the WC cart and redirect the user directly to the checkout. As a logged in user it works like a charm but for guests it shows a blank page on checkout (woocommerce_checkout shortcode seems to return nothing in this case).
So I came up with a check if the cart iss till empty. Apparently it is because the "cart-page" shows "There are no items in the cart yet".



In code i check if the cart is still empty but it tells me its not!



Here is my code:



if(!$wooID = $wpdb->get_var("SELECT wooID FROM ".$wpdb->prefix."ceb_events WHERE id = $event")) die("ERROR GETTING WOOID");

WC()->cart->empty_cart();

if(!WC()->cart->add_to_cart( $wooID, 1 )) die("CART GOT NOT UPDATED. THERE IS AN ERROR 1.");

if(WC()->cart->get_cart_contents_count() == 0) die("CART GOT NOT UPDATED. THERE IS AN ERROR 2.");

//Here follows the redirect to checkout page


The code runs without errors. And it works 100% as a logged in user / admin. Just not as guest, even tho I allowed the guest checkout in the woocommerce settings.










share|improve this question

























  • Heyja, There is probably nothing wrong with the code. It works fine as logged in user. Its just the function add_to_cart from WooCommerce thats seems to create a problem after adding an item to cart when there is no logged in user.

    – xDrago
    Nov 21 '18 at 22:38











  • Wow yea, it works! Thank you very much, youre my man! Can you explain whats going on?

    – xDrago
    Nov 21 '18 at 23:34













  • Oh i Figured that my admin area doesnt work anymore when i add your code. HTTP 500 Error.

    – xDrago
    Nov 21 '18 at 23:44














1












1








1








Im trying to get my code to work. Searching for hours now.
I found similar questions like this one. But unfortunately noone seems to find a solution.



In my custom plugin I want to add a specific item to the WC cart and redirect the user directly to the checkout. As a logged in user it works like a charm but for guests it shows a blank page on checkout (woocommerce_checkout shortcode seems to return nothing in this case).
So I came up with a check if the cart iss till empty. Apparently it is because the "cart-page" shows "There are no items in the cart yet".



In code i check if the cart is still empty but it tells me its not!



Here is my code:



if(!$wooID = $wpdb->get_var("SELECT wooID FROM ".$wpdb->prefix."ceb_events WHERE id = $event")) die("ERROR GETTING WOOID");

WC()->cart->empty_cart();

if(!WC()->cart->add_to_cart( $wooID, 1 )) die("CART GOT NOT UPDATED. THERE IS AN ERROR 1.");

if(WC()->cart->get_cart_contents_count() == 0) die("CART GOT NOT UPDATED. THERE IS AN ERROR 2.");

//Here follows the redirect to checkout page


The code runs without errors. And it works 100% as a logged in user / admin. Just not as guest, even tho I allowed the guest checkout in the woocommerce settings.










share|improve this question
















Im trying to get my code to work. Searching for hours now.
I found similar questions like this one. But unfortunately noone seems to find a solution.



In my custom plugin I want to add a specific item to the WC cart and redirect the user directly to the checkout. As a logged in user it works like a charm but for guests it shows a blank page on checkout (woocommerce_checkout shortcode seems to return nothing in this case).
So I came up with a check if the cart iss till empty. Apparently it is because the "cart-page" shows "There are no items in the cart yet".



In code i check if the cart is still empty but it tells me its not!



Here is my code:



if(!$wooID = $wpdb->get_var("SELECT wooID FROM ".$wpdb->prefix."ceb_events WHERE id = $event")) die("ERROR GETTING WOOID");

WC()->cart->empty_cart();

if(!WC()->cart->add_to_cart( $wooID, 1 )) die("CART GOT NOT UPDATED. THERE IS AN ERROR 1.");

if(WC()->cart->get_cart_contents_count() == 0) die("CART GOT NOT UPDATED. THERE IS AN ERROR 2.");

//Here follows the redirect to checkout page


The code runs without errors. And it works 100% as a logged in user / admin. Just not as guest, even tho I allowed the guest checkout in the woocommerce settings.







php wordpress session woocommerce cart






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 0:00









LoicTheAztec

88.3k1365102




88.3k1365102










asked Nov 21 '18 at 22:21









xDragoxDrago

6218




6218













  • Heyja, There is probably nothing wrong with the code. It works fine as logged in user. Its just the function add_to_cart from WooCommerce thats seems to create a problem after adding an item to cart when there is no logged in user.

    – xDrago
    Nov 21 '18 at 22:38











  • Wow yea, it works! Thank you very much, youre my man! Can you explain whats going on?

    – xDrago
    Nov 21 '18 at 23:34













  • Oh i Figured that my admin area doesnt work anymore when i add your code. HTTP 500 Error.

    – xDrago
    Nov 21 '18 at 23:44



















  • Heyja, There is probably nothing wrong with the code. It works fine as logged in user. Its just the function add_to_cart from WooCommerce thats seems to create a problem after adding an item to cart when there is no logged in user.

    – xDrago
    Nov 21 '18 at 22:38











  • Wow yea, it works! Thank you very much, youre my man! Can you explain whats going on?

    – xDrago
    Nov 21 '18 at 23:34













  • Oh i Figured that my admin area doesnt work anymore when i add your code. HTTP 500 Error.

    – xDrago
    Nov 21 '18 at 23:44

















Heyja, There is probably nothing wrong with the code. It works fine as logged in user. Its just the function add_to_cart from WooCommerce thats seems to create a problem after adding an item to cart when there is no logged in user.

– xDrago
Nov 21 '18 at 22:38





Heyja, There is probably nothing wrong with the code. It works fine as logged in user. Its just the function add_to_cart from WooCommerce thats seems to create a problem after adding an item to cart when there is no logged in user.

– xDrago
Nov 21 '18 at 22:38













Wow yea, it works! Thank you very much, youre my man! Can you explain whats going on?

– xDrago
Nov 21 '18 at 23:34







Wow yea, it works! Thank you very much, youre my man! Can you explain whats going on?

– xDrago
Nov 21 '18 at 23:34















Oh i Figured that my admin area doesnt work anymore when i add your code. HTTP 500 Error.

– xDrago
Nov 21 '18 at 23:44





Oh i Figured that my admin area doesnt work anymore when i add your code. HTTP 500 Error.

– xDrago
Nov 21 '18 at 23:44












1 Answer
1






active

oldest

votes


















2














You need to initiate Woocommerce User session when it's not logged in. So you will use the following:



add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
function force_non_logged_user_wc_session(){
if( is_user_logged_in() || is_admin() )
return;

if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );
}


Code goes in function.php file of your active child theme (or active theme). It should works.






share|improve this answer



















  • 1





    YES! Thank you very much. Your reply will probably help a few people since i saw a lot of similar questions in the net. It also fixed the http 500 error. awesome

    – xDrago
    Nov 21 '18 at 23:56






  • 1





    @xDrago I have marked as duplicate the linked thread in your question … This will help people to find out this thread easily.

    – LoicTheAztec
    Nov 22 '18 at 0:05













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421253%2fmake-wc-cart-add-to-cart-method-working-for-guests-in-woocommerce%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You need to initiate Woocommerce User session when it's not logged in. So you will use the following:



add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
function force_non_logged_user_wc_session(){
if( is_user_logged_in() || is_admin() )
return;

if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );
}


Code goes in function.php file of your active child theme (or active theme). It should works.






share|improve this answer



















  • 1





    YES! Thank you very much. Your reply will probably help a few people since i saw a lot of similar questions in the net. It also fixed the http 500 error. awesome

    – xDrago
    Nov 21 '18 at 23:56






  • 1





    @xDrago I have marked as duplicate the linked thread in your question … This will help people to find out this thread easily.

    – LoicTheAztec
    Nov 22 '18 at 0:05


















2














You need to initiate Woocommerce User session when it's not logged in. So you will use the following:



add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
function force_non_logged_user_wc_session(){
if( is_user_logged_in() || is_admin() )
return;

if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );
}


Code goes in function.php file of your active child theme (or active theme). It should works.






share|improve this answer



















  • 1





    YES! Thank you very much. Your reply will probably help a few people since i saw a lot of similar questions in the net. It also fixed the http 500 error. awesome

    – xDrago
    Nov 21 '18 at 23:56






  • 1





    @xDrago I have marked as duplicate the linked thread in your question … This will help people to find out this thread easily.

    – LoicTheAztec
    Nov 22 '18 at 0:05
















2












2








2







You need to initiate Woocommerce User session when it's not logged in. So you will use the following:



add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
function force_non_logged_user_wc_session(){
if( is_user_logged_in() || is_admin() )
return;

if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );
}


Code goes in function.php file of your active child theme (or active theme). It should works.






share|improve this answer













You need to initiate Woocommerce User session when it's not logged in. So you will use the following:



add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
function force_non_logged_user_wc_session(){
if( is_user_logged_in() || is_admin() )
return;

if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );
}


Code goes in function.php file of your active child theme (or active theme). It should works.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 23:51









LoicTheAztecLoicTheAztec

88.3k1365102




88.3k1365102








  • 1





    YES! Thank you very much. Your reply will probably help a few people since i saw a lot of similar questions in the net. It also fixed the http 500 error. awesome

    – xDrago
    Nov 21 '18 at 23:56






  • 1





    @xDrago I have marked as duplicate the linked thread in your question … This will help people to find out this thread easily.

    – LoicTheAztec
    Nov 22 '18 at 0:05
















  • 1





    YES! Thank you very much. Your reply will probably help a few people since i saw a lot of similar questions in the net. It also fixed the http 500 error. awesome

    – xDrago
    Nov 21 '18 at 23:56






  • 1





    @xDrago I have marked as duplicate the linked thread in your question … This will help people to find out this thread easily.

    – LoicTheAztec
    Nov 22 '18 at 0:05










1




1





YES! Thank you very much. Your reply will probably help a few people since i saw a lot of similar questions in the net. It also fixed the http 500 error. awesome

– xDrago
Nov 21 '18 at 23:56





YES! Thank you very much. Your reply will probably help a few people since i saw a lot of similar questions in the net. It also fixed the http 500 error. awesome

– xDrago
Nov 21 '18 at 23:56




1




1





@xDrago I have marked as duplicate the linked thread in your question … This will help people to find out this thread easily.

– LoicTheAztec
Nov 22 '18 at 0:05







@xDrago I have marked as duplicate the linked thread in your question … This will help people to find out this thread easily.

– LoicTheAztec
Nov 22 '18 at 0:05






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421253%2fmake-wc-cart-add-to-cart-method-working-for-guests-in-woocommerce%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?