Make WC_Cart add_to_cart method working for guests in Woocommerce
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
add a comment |
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
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
add a comment |
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
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
php wordpress session woocommerce cart
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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