I am trying to programmatically add a booking to the shopping cart. I have been looking into the code for this and have found and tried the following:
Method 1: $woocommerce->cart->add_to_cart( $product_id);
Method 2: WC()->cart->add_to_cart( $product_id );
Neither of which are working for me with the latest release of woocommerce bookings.
The code works for simple products in woocommerce without issue. Any help would be greatly appreciated
Related
I've created a custom field using ACF and I want to show it in my WooCommerce single product page. How can I do that?
You need to find out the hook that represents the place where the ACF field should be displayed, e.g. by using the WooCommerce Visual Hook Guide for the single product page.
example
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
if (function_exists('the_field')){
the_field('field_name');
}
}
This is adapted from https://support.advancedcustomfields.com/forums/topic/acf-with-woocommerce/.
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.
This question already has answers here:
Only one currently added product into Woocommerce cart?
(2 answers)
WooCommerce - Skip cart page redirecting to checkout page
(1 answer)
Closed 4 years ago.
I am trying to remove the full cart feature from WooCommerce. I want when some one click on the buy now button they should go directly to the checkout page.
** I have tried few plugins but those don't remove the full cart feature. They just redirect to the checkout page.
** So with the plugins my problem was if someone leave from the checkout page and try to buy the same product or an another product the previous product was in the cart. and in the checkout page there was 2 products.
Can someone tell me how can i remove the full cart feature from the woo-commerce? Is it possible?
So if you want to always have only one item in your cart you add this to your functions.php
add_filter( 'woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 20, 3 );
function remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
if( ! WC()->cart->is_empty())
WC()->cart->empty_cart();
return $passed;
}
It will empty your cart before adding the new item to it. And you can keep your plugin that is redirecting you to checkout or code it in php probably with something like that :
add_action( 'template_redirect', 'redirect_cart_to_checkout' );
function redirect_cart_to_checkout() {
if( is_cart()){
wp_redirect(wc_get_checkout_url());
die;
}
}
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 am very new to this community. kindly help me to get resolve form wordpress related query.
I am working on woocommerce website and need to add a service for ex:"electricity bill payment" . So, user has to dynamically add the amount and it should add to the card and reduced from his main wallet.
Is there any plugin or extension for this dynamically price added by this enduser ?
Plz suggest me and sorry for my bad english.
Many thanks,
sai
function woo_add_cart_fee() {
global $woocommerce;
$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
Would you please add above code in your active theme functions.php ? but it will be applied on over all cart.