I'm in the process of moving a site over from Infusionsoft's singular order form pages (no cart, you buy one product at a time) to a WooCommerce powered site with a shopping cart. That part is going well.
However, there's certain circumstances where my client wants the site to behave like the Infusionsoft checkout. Basically... user goes to a URL that is nothing but a checkout page with a specified product, the only thing you can purchase. This would be for if they were doing a class and offering a special offer to attendees only (via a custom URL).
I've tried WooCommerce Single Product Checkout, and while it gets close it doesn't do exactly what I'd want. The two big issues are 1) It still requires the user to add the item to the cart and 2) The cart is still site wide, meaning if the user had previously added another product to the cart now the checkout on the single product page wants to check both products out.
Any suggestions? Thanks
Ended up writing my own plugin. Here's the code
function add_and_forward($atts, $content=null) {
global $woocommerce;
$ids = explode(",", $atts['product_ids']);
$empty_cart = $atts['empty_cart'];
$checkout_url = $woocommerce->cart->get_checkout_url();
//if empty cart
if($empty_cart === "1")
$woocommerce->cart->empty_cart();
foreach($ids as $id)
$woocommerce->cart->add_to_cart($id);
wp_redirect( $checkout_url );
} add_shortcode('add_and_forward', 'add_and_forward');
Only downside is that it still is a site wide cart, but the client said it was ok, at least this gives them the option to empty the users cart or not.
Run it with the following shortcode
[add_and_forward product_ids="1748" empty_cart="1"]
Related
Was wondering if someone could point me in the right direction.
My current Woocommerce Permalink settings are as follows:
SHOP
www.mywebsite.com/shop
SHOP WITH SUB CATEGORY
www.mywebsite.com/seasons/winter
Unfortunately, the above SHOP WITH SUB CATEGORY link also works without the product category - i.e. www.mywebsite.com/winter - for some reason.
Which is annoying as I also have a main page in my website called Winter. (i.e. mywebsite.com/winter).
I have tried various versions of the COMMON, OPTIONAL and PRODUCT permalinks, but no matter what I type, www.mywebsite.com/winter always points to the Woocommerce Shop page and not the normal website page.
Any ideas?
Huge thank you in advance :)
Steven
What happens when you use this? This code stops WordPress from guessing where to go.
function no_redirect_guess_404_permalink( $header ){
global $wp_query;
if( is_404() )
unset( $wp_query->query_vars['name'] );
return $header;
}
add_filter( 'status_header', 'no_redirect_guess_404_permalink' );
Original answer
I have a woocommerce shop page which shows me all of the products perfectly. This page is also set as the "Shop" page in the settings. Now I want a second page called Home, which should use the same template as the shop page. (basically a second shop page without categories and some news)
home-template.php
<?php /* Template Name: Homepage */
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_get_template( 'archive-product.php' );
?>
I'm using the standard archive-product template from woocommerce's github.
I can see the template being loaded, like the header, but certain woocommerce functions don't return the expected value. woocommerce_before_shop_loop doesn't return anything, even though it should display the orderby dropdown. woocommerce_product_loop() returns true, but wc_get_loop_prop('total') returns zero.
I've also tried renaming the file to front-page.php but that didn't help. The home page is set correctly in the reading settings.
Am i missing a call to a query, or is it something else? Thank you in advance for your help!
When WooCommerce creates the builtin shop page it saves the id of the created page in the wp_options database table with option_name 'woocommerce_shop_page_id'. When WordPress loads a page WooCommerce checks if the page id of the page being loaded is equal to the option_value of option_name 'woocommerce_shop_page_id' and then executes code to generate the HTML of the shop page. If you have looked at the shop page in the page editor you will notice that the content is empty. The magic is being done with hard coded routines that execute for this special id.
Since, your custom page has a different page id none of the custom code for generating the shop page will be execute. So, you need to execute this magic code for your custom page. It can be done but you need a good understanding of WooCommerce.
I suggest you reconsider your design and instead of a new page just customize the existing shop page using actions and filters.
I’ve added the following filter (see below) for the scenario that a customer adds another (individually sold, digital) product to the cart that is already in the cart.
It was a nice way to disable the kind of ugly “you cannot add another product” message in a case that happened and just show the cart (as a reminder that the product is already in the cart)
With the new 3.7 woocommerce update, the appearing circle on top of the ajax "buy button" is stuck in an endlessly spinning loop.
In the version before the circle started spinning, it stopped, the cart appeared to show the customer that the product is already in the cart.
Did woocommerce change the variables that are involved in the filter below or is there another easy fix that I'm not aware of?
// if product is already in cart, just go to cart and don’t show ugly “you cannot add another product” message
add_filter( ‘woocommerce_add_to_cart_sold_individually_found_in_cart’, ‘spark_redirect_to_cart’ );
function spark_redirect_to_cart( $found_in_cart ) {
if ( $found_in_cart ) {
wp_safe_redirect( wc_get_page_permalink( ‘cart’ ) );
exit;
}
return $found_in_cart;
}
Help is very much appreciated, thanks.
Or is this bug related to the flatsome theme I'm using.
So basically, what i want, to show product added in cart also on archive page. I did this functionality, you can refer with image (1 item- already in cart) but my code work on refresh the page. I want to show the same with ajax. As soon as "add to cart" clicked, the data added in mini cart, at same time data will show on product listing page. enter image description here
Go to
Woocommerce > Settings > Products > Enable AJAX add to cart buttons on archives
add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
global $woocommerce;
$return_to = get_permalink(woocommerce_get_page_id('shop'));
$message = sprintf('%s %s', $return_to, __('Continue Shopping', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
return $message;
}
If you want to add your product with quantity using Ajax then definitely WooCommerce default checkbox Enable AJAX to add to cart buttons on archives not works for quantity purpose.
There are many solutions to add this feature. I have implemented it earlier. You just need to add some classes, functions, and script to achieve this.
This is the link which is really helpful for me and you can go through it or you can also check this one. It's not complicated.
If you want to add this in your headers like a mini cart or something. So for that, this one is the best solution for you.
Hope this will helpful for you. Thanks.
I have a custom template file, rendering some products and their Add To Cart buttons, that I am trying to implement manually. When Add to Cart is pressed, the page is reloaded and a $_POST variable containing some data adds the new products. 'cart_contents_count' also reflects the added item. However, when I go to the cart page, it's empty. Please see the following code.
global $woocommerce;
if ( isset( $_POST['AddedToCart'] ) ) {
$woocommerce->cart->add_to_cart($_POST['event'], $_POST['qty']);
}
$cart_total = $woocommerce->cart->cart_contents_count;
When I however go to the normal default shop page ( /shop/ ) and add a product from there, my cart page indicates that this product has been added. When I NOW go to my custom page and add products from that Add To Cart button, it works perfectly.
It seems to me that, before I run the above-mentioned code, I must check if a Cart Session has been initialized, and if not, initialize it. Could someone please confirm for me that I understand it right and show me how to initialize the cart?
Here is a solution if your custom form is on a page template. This code goes in your functions.php file. Be sure to change yourtheme_template to something more unique. Also, change the items in the $session_templates array to the template filenames where you want this filter to be used. It uses the template_include filter, which isn't an easy filter to track down, let alone $woocommerce->session->set_customer_session_cookie(true) - Special thanks to #vrazer (on twitter) for the help.
function yourtheme_template($template) {
//List of template file names that require WooCommerce session creation
$session_templates = array(
'page-template-file-name.php', 'another-page-template-filename.php'
);
//Split up the template path into parts so the template file name can be retrieved
$parts = explode('/', $template);
//Check the template file name against the session_templates list and instantiate the session if the
//template is in the list and the user is not already logged in. If the session already exists from
//having created a cart, WooCommerce will not destroy the active session
if (in_array($parts[count($parts) - 1], $session_templates) && !is_user_logged_in()) {
global $woocommerce;
$woocommerce->session->set_customer_session_cookie(true);
}
return $template;
}
//Filter to run the WooCommerce conditional session instantiation code
add_filter('template_include', 'yourtheme_template');
I resolved this problem by making sure the $woocommerce->cart->add_to_cart() line is positioned before any headers are sent. I.E, before get_header() is called on my custom template.
In the WooCommerce version 2.5 they change the way the sessions works. https://woocommerce.wordpress.com/2015/10/07/new-session-handler-in-2-5/
What i did was install this plugin https://github.com/kloon/woocommerce-large-sessions then my cart is not empty any more with guess users.
I hope it helps someone else.