WooCommerce Booking & Appointment Plugin: wrong price on cart and checkout page - wordpress

The site I am developing is using the WooCommerce Booking & Appointment Plugin (http://www.tychesoftwares.com/store/premium-plugins/woocommerce-booking-plugin), I create a custom form to add mutiple products to cart at one time. Here is the code:
function booking_add_to_cart() {
if ($_POST && is_post_type_archive('product')) {
global $woocommerce;
if(is_array($_POST['qty']) && !empty($_POST['qty'])) {
$woocommerce->cart->empty_cart(); //Clear the cart
foreach ($_POST['qty'] as $product_id => $qty) {
$qty = intval($qty);
$product_id = intval($product_id);
if ($qty > 0 && $product_id > 0) {
$woocommerce->cart->add_to_cart($product_id, $qty); // Add product to cart individually.
}
}
$checkout_url = $woocommerce->cart->get_checkout_url();
wp_redirect( $checkout_url ); //Redirect to checkout page
}
}
}
add_action( 'template_redirect', 'booking_add_to_cart');
But there are something wrong with the prices on checkout page. Some of them are correct and some aren't.
The plugin is using three filters to modify the prices:
add_filter('woocommerce_add_cart_item_data', array('bkap_cart', 'bkap_add_cart_item_data'), 25, 2);
add_filter('woocommerce_get_cart_item_from_session', array('bkap_cart', 'bkap_get_cart_item_from_session'), 25, 2);
add_filter( 'woocommerce_get_item_data', array('bkap_cart', 'bkap_get_item_data_booking'), 25, 2 );
However it's not working correctly in the custom form I created. Can anyone help me with this? Thanks in advance.

Related

Add a custom "Buy now" button on WooCommerce single product page which clears the cart, add a product and redirect to checkout

I'm using this simple code to add a 'buy now' button on a single product page.
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
function add_content_after_addtocart() {
// get the current post/product ID
$current_product_id = get_the_ID();
// get the product based on the ID
$product = wc_get_product( $current_product_id );
// get the "Checkout Page" URL
$checkout_url = WC()->cart->get_checkout_url();
// run only on simple products
if( $product->is_type( 'simple' ) ) {
echo 'Buy Now';
}
}
This code effectively redirects to the checkout page and add the product to the cart, but I want to add two little features to it:
After clicking on the button, I want it to clear the cart before taking the action.
After adding the product to the cart, I want it to redirect users to '/checkout' page. Right now it sends users on 'checkout/?add-to-cart=3122', which means that any refresh on the checkout page adds 1 product on the cart automatically.
Any advice?
Instead of using the add-to-cart param in your url, which will cause the product to be added (but also perform other actions in WooCommerce), you can use a custom url for your button and the template_redirect action hook
That way you get rid of the built-in functionality in WooCommerce and you can perform your own custom actions based on the GET parameters
So you get:
// Add new/extra button
function action_woocommerce_after_add_to_cart_button() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Run only on simple products
if ( $product->is_type( 'simple' ) ) {
// Get product ID
$product_id = $product->get_id();
// Get permalink
$permalink = $product->get_permalink();
// Output url
echo ''. __ ( 'Buy Now', 'woocommerce' ) . '';
}
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button', 10 );
// Redirect
function action_template_redirect() {
// Determines whether the current request is for an administrative interface page
if ( is_admin() ) return;
// Returns true when viewing a single product
if ( ! is_product() ) return;
// Get params
if ( isset( $_GET['product_id'] ) && isset( $_GET['redirect_checkout'] ) ) {
// Get param 1
$product_id = $_GET['product_id'];
// Get param 2
$boolean = $_GET['redirect_checkout'];
// WC Cart
if ( WC()->cart ) {
// 1. Empty cart
WC()->cart->empty_cart();
// 2. Add to cart
WC()->cart->add_to_cart( $product_id );
// 3. Redirect
// When true
if ( $boolean ) {
// Gets the url to the checkout page
$checkout_url = wc_get_checkout_url();
// Performs a safe (local) redirect
wp_safe_redirect( $checkout_url );
exit;
}
}
}
}
add_action( 'template_redirect', 'action_template_redirect' );
Here is code for clear cart before add item
add_filter( 'woocommerce_add_to_cart_validation', 'ji_remove_cart_item_before_add_to_cart', 20, 3 );
function ji_remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
if( ! WC()->cart->is_empty() )
WC()->cart->empty_cart();
return $passed;}
After adding the product to the cart, I want it to redirect users to '/checkout' page. Right now it sends users on 'checkout/?add-to-cart=3122', which means that any refresh on the checkout page adds 1 product on the cart automatically.
add_filter( 'woocommerce_add_to_cart_redirect', 'ji_redirect_checkout_after_add_to_cart' );
function ji_redirect_checkout_after_add_to_cart() {
return wc_get_checkout_url();
}

I want to show a new table on checkout page to show how much user save on a product ACF Wordpress WooCommerce

I working on a WordPress website where users can purchase a membership and I want to Show that if a user is a member how much he can save. I'm using ACF.
My Question is how can I show ACF field on a checkout page next to product???
WordPress in general and WooCommerce have the thing called "Hook" to help us add our custom data.
This article is an introduction and example about WooCommerce Hooks
This is a virtual version of hooks on the WooCommerce checkout page.
So as your needs, you may use woocommerce_review_order_before_cart_contents hook, add this code to functions.php file in your theme:
Add to the checkout page in general
add_action( 'woocommerce_review_order_before_cart_contents', 'add_member_info' );
function add_member_info() {
echo '<div class=”member-message”>Become a member will save $100</div>';
}
Get data from a custom field in the product
I. To show in the single product
function cw_change_product_price_display( $price, $product ) {
$my_field = get_field('member_save', $product->ID);
$price .= " | Member save $$my_field";
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display', 10, 2);
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display', 10, 2);
II. To show in the checkout page
Add custom data with the product to Card
add_filter( 'woocommerce_add_cart_item_data', function ( $cartItemData, $productId, $variationId ) {
$member_saved = get_field('member_save', $productId);
$cartItemData['myCustomData'] = $member_saved;
return $cartItemData;
}, 10, 3 );
add_filter( 'woocommerce_get_cart_item_from_session', function ( $cartItemData, $cartItemSessionData, $cartItemKey ) {
if ( isset( $cartItemSessionData['myCustomData'] ) ) {
$cartItemData['myCustomData'] = $cartItemSessionData['myCustomData'];
}
return $cartItemData;
}, 10, 3 );
Show the data in the cart and checkout page
add_filter( 'woocommerce_get_item_data', function ( $data, $cartItem ) {
if ( isset( $cartItem['myCustomData'] ) ) {
$data[] = array(
'name' => 'Member saved',
'value' => $cartItem['myCustomData']
);
}
return $data;
}, 10, 2 );

URL to product page with quantity inputed - WooCommerce

I am trying to find a way to edit a product url in WooCommerce so that when visited a quantity is pre-selected for the quantity input, e.g. 2.
I know there is an add to cart url for it:
http://yourdomain.com/?add-to-cart=47&quantity=2
But was wondering if it was possible to just input the quantity on the product page with a similar URL.
Thanks for any ideas on the matter.
you can do it like this:
add_filter( 'woocommerce_quantity_input_args', 'custom_woocommerce_quantity_input_args' ); // Simple products
add_filter( 'woocommerce_available_variation', 'custom_woocommerce_quantity_input_args' ); // Variations
function custom_woocommerce_quantity_input_args( $args ) {
if ( isset( $_GET['qty'] ) && is_numeric($_GET['qty']) ) {
$args['input_value'] = $_GET['qty'];
}
return $args;
}
with that you can then do http://yourdomain.com/product/ship-your-idea/?qty=10.

Woocommerce add to cart with custom price

I've seen many examples of adding an item to the WC cart with a customer price, but none dynamically. I am trying to do in a shortcode function that receives a POST variables....
if (isset($_POST['wmnf_add_donation'])) {
global $woocommerce;
$cart_object = $woocommerce->cart;
$custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
$target_product_id = 65986;
$cart_object->add_to_cart($target_product_id, "1");
foreach ( $cart_object->cart_contents as $key => $value ) {
if ( $value['product_id'] == $target_product_id ) {
$value['data']->price = $custom_price;
}
}
}
This adds the item to the cart of course, but the price is zero and I realize I need to somehow save this array back to the WC cart data. Is this method even possible or can it only be done via a filter or action hook? If so, how can I save the changed array back to the cart contents or make it work to add the one item with its posted price? Any guidance greatly appreciated.
Thanks for that answer doublesharp, I was not able to get it to work as described because the form was posting to the page with my shortcode, which has my form, instead of posting directly to the cart. The $_POST was not being seen and the product ended up zero. I did find another approach, but having a problem using wp_redirect. I changed the above shortcode to this:
if (isset($_POST['wmnf_add_donation'])) {
global $woocommerce;
$custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
$target_product_id = 65986;
$_SESSION['donation_amount'] = $custom_price;
$woocommerce->cart->add_to_cart($target_product_id, "1");
wp_redirect( site_url() . '/gifts/swag-bag/');
}
Then I added the following filters to functions.php:
add_filter('woocommerce_get_price','donation_price', 10, 2);
add_filter('woocommerce_get_regular_price','donation_price', 10, 2);
add_filter('woocommerce_get_sale_price','donation_price', 10, 2);
function donation_price($price, $productd){
if($productd->id == '65986'){
$price = $_SESSION['donation_amount'];
}
return $price;
}
This does not work except when wp_redirect is commented out, hence, not redirecting. The above redirects to the cart, but its empty. If I comment out the wp_redirect line and then manually go to the cart, the product is there with my custom price. Actually, I would like to apply a custom price and redirect directly to the checkout page instead of the cart, if possible?
You can use the woocommerce_before_calculate_totals action hook to modify the contents of the cart, including the product prices.
add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals' );
function before_calculate_totals( $_cart ){
// loop through the cart_contents
foreach ( $_cart->cart_contents as $cart_item_key => &$item ) {
// you will need to determine the product id you want to modify, only when the "donation_amount" is passed
if ( $item['id'] == 65986 && isset( $_POST['donation_amount'] ) ){
// custom price from POST
$custom_price = $_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0;
// save to the cart data
$item['data']->price = $custom_price;
// new versions of WooCommerce may require (instead of line above)...
// $item['data']->set_price($custom_price);
}
}
}

how to disable cart functionality from woocommerce?

How to disable cart functionality from woocommerce store. I want user can only see products available.customer can not purchase from store.
the easiest way is make the products not purchasable..
add_filter( 'woocommerce_is_purchasable','__return_false',10,2);
To fully disable woocommerce purchase functionality:
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
More option you can find here: https://react2wp.com/remove-hide-add-to-cart-button-in-woocommerce-while-disabling-keeping-purchase-functionality/
If you need conditions, you can use the following code:
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
// Conditions here.
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );
For example, if you need to check users:
// Disable purchase for non-logged-in users.
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
if ( ! is_user_logged_in() ) {
return false;
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );

Resources