Get a dynamic calculated fee based on postcode input in Woocommerce - php

I trying to pass a variable in woocommerce_cart_calculate_fees hook, but this code doesn't work. When I try to pass static value within a variable it works.
Here is my code:
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees', 20, 1);
function add_custom_fees( WC_Cart $cart){
$final_discount; // storing ajax value
$static_value = 5; // static value
if ($final_discount) {
/**
* custom discount for the cart in woocommerce
*/
// Hook before calculate fees
add_action('woocommerce_cart_calculate_fees', 'add_custom_fees');
/**
* Add custom fee if more than three article
* #param WC_Cart $cart
*/
function add_custom_fees(WC_Cart $cart) {
// Calculate the amount to reduce
global $final_discount;
$discount = $cart->subtotal * $final_discount / 100;
$cart->add_fee('10% discount has been added.', -$discount);
}
}
}
EDIT:
Now I am to pass a WC_Session value in woocommerce_cart_calculate_fees hook, the code works but session variable does not update before page refreshing.
The session variable is storing from onchange ajax value of billing_postcode field of woocommerce checkout page.
My jQuery code (Ajax):
jQuery(document).ready(function () {
jQuery('#billing_postcode').on('change', function (event) {
//alert( this.value );
event.preventDefault();
var billing_postcode = jQuery('#billing_postcode').val();
console.log(billing_postcode);
var data = {
action: 'woocommerce_apply_state',
security: wc_checkout_params.apply_state_nonce,
billing_postcode: billing_postcode
};
jQuery.ajax({
type: "POST",
data: data,
url: wc_checkout_params.ajax_url,
success: function (code) {
console.log(code);
if (code === '0') {
jQuery('body').trigger('update_checkout');
}
},
dataType: 'html'
});
});
});
My PHP code in functions.php file of my theme:
wp_enqueue_script('zip_code', get_template_directory_uri() . '/assets/js/zipcode.js', array('jquery'));
wp_localize_script('zip_code', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));
add_action('wp_ajax_woocommerce_apply_state', 'discount', 10);
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'discount', 10);
function discount() {
if(isset($_POST['billing_postcode'])){
$billing_postcode = isset($_POST['billing_postcode'])?$_POST['billing_postcode']:'not yet';
global $wpdb;
$zipcodes = $wpdb->get_results(
$wpdb->prepare( "
SELECT * FROM wp_zip_codes_value
WHERE zip_code_value = %d",
$billing_postcode
)
);
$zip_for_discount = array();
foreach ( $zipcodes as $zipcode ){
$zip_for_discount = $zipcode->zip_code_id;
}
$find_discount = $wpdb->get_results(
$wpdb->prepare( "
SELECT * FROM wp_zip_codes
WHERE zip_code = %d",
$zip_for_discount
)
);
$final_discount = array();
if($find_discount){
foreach ( $find_discount as $discount ){
$final_discount[] = $discount->discount;
}
}
$final_discount[0];
WC()->session->set( 'final_discount', $final_discount[0] );
}
}
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees', 20, 1);
function add_custom_fees( WC_Cart $cart){
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$percent = WC()->session->get( 'final_discount' );
if( $percent > 0 ){
$discount = $cart->subtotal * $percent / 100;
$cart->add_fee( __('Zip Code Discount', 'woocommerce' ) . " ($percent%)", -$discount);
}
}

The best way to get that working is to set your $final_discount variable using WC_Sessions in another function before, this way:
WC()->session->set( 'final_discount', $final_discount );
Now you can get this value anywhere on front end hooks or code using:
$final_discount = WC()->session->get( 'final_discount' );
So your final discount (negative fee) code will be something like:
add_action('woocommerce_cart_calculate_fees', 'add_custom_fee', 20, 1 );
function add_custom_fee( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Get the data from WC_Sessions
$percent = WC()->session->get( 'final_discount' );
if( $percent > 0 ){
$discount = $cart->subtotal * $percent / 100;
$cart->add_fee( __('Discount', 'woocommerce' ) . " ($percent%)" . $percent, -$discount);
}
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
Update regarding your updated question:
Here is a working Ajax example code similar to yours but simplified (as I don't have similar database custom tables to get the percentage for the discount).
The variable discount percentage is based on the imputed zip-code value…
The PHP code:
add_action( 'wp_enqueue_scripts', 'wc_zipcode_enqueue_scripts' );
function wc_zipcode_enqueue_scripts() {
// Only on front-end and checkout page
if( is_admin() || ! is_checkout() ) return;
// (For child themes use get_stylesheet_directory_uri() instead)
wp_enqueue_script('zip_code', get_template_directory_uri() . '/assets/js/zipcode.js', array('jquery'));
wp_localize_script('zip_code', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));
}
add_action('wp_ajax_woocommerce_apply_state', 'woocommerce_apply_state', 10 );
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'woocommerce_apply_state', 10 );
function woocommerce_apply_state() {
global $wpdb;
if( isset($_POST['billing_postcode']) ){
$billing_postcode = $_POST['billing_postcode'];
if( empty($billing_postcode) || $billing_postcode == 0 ) die();
if( $billing_postcode < 30000 )
$final_discount = 10;
elseif( $billing_postcode >= 30000 && $billing_postcode < 60000 )
$final_discount = 15;
else
$final_discount = 20;
WC()->session->set( 'final_discount', $final_discount );
echo json_encode( WC()->session->get('final_discount' ) );
}
die(); // Alway at the end (to avoid server error 500)
}
add_action('woocommerce_cart_calculate_fees' , 'add_custom_discount', 20, 1);
function add_custom_discount( WC_Cart $cart){
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Get the data from WC_Sessions
$percent = WC()->session->get( 'final_discount' );
// If the billing postcode is not set we exit
$billing_postcode = WC()->session->get('customer')['postcode'];
if( empty($billing_postcode) ) return;
if( $percent > 0 ){
$discount = $cart->subtotal * $percent / 100;
$cart->add_fee( __('Zip Code Discount', 'woocommerce' ) . " ($percent%)", -$discount);
}
}
Code goes in function.php file of your active child theme (active theme).
The javascript code:
jQuery(document).ready(function($) {
function postcodeAjax(){
$.ajax({
type: 'POST',
data: {
action: 'woocommerce_apply_state',
billing_postcode: $('#billing_postcode').val(),
security: wc_checkout_params.apply_state_nonce
},
url: wc_checkout_params.ajax_url,
success: function (response) {
$('body').trigger('update_checkout');
console.log('updating checkout…');
}
});
}
if( $('#billing_postcode').val() > 0 )
postcodeAjax();
$('#billing_postcode').on('change blur', function(e){
e.preventDefault();
postcodeAjax();
$('body').trigger('update_checkout');
});
});
To be saved in a file named zipcode.js located in your theme folder /assets/js/zipcode.js
Tested and perfectly works

Related

Add discount to woocomerce cart page INCL. tax

I am trying to modify the code based on this question:
// Display the checkout field in cart page totals section
add_action( 'woocommerce_cart_totals_before_order_total', 'display_priority_fee_checkbox_field', 20 );
function display_priority_fee_checkbox_field(){
echo '<tr class="installment-section">
<th>'.__("Priority Dispatch").'</th><td>';
woocommerce_form_field( 'priority_fee', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __(' $20.00'),
), WC()->session->get('priority_fee') ? '1' : '' );
echo '<div class="tooltip">?
<span class="tooltiptext">'.__("By selecting this option... ").'</span>
</div></td>';
}
// Remove "(optional)" text from the field
add_filter( 'woocommerce_form_field' , 'remove_optional_txt_from_priority_fee_checkbox', 10, 4 );
function remove_optional_txt_from_priority_fee_checkbox( $field, $key, $args, $value ) {
// Only on checkout page for Order notes field
if( 'priority_fee' === $key ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// jQuery :: Ajax script
add_action( 'wp_footer', 'priority_fee_js_script' );
function priority_fee_js_script() {
// On Order received page, remove the wc session variable if it exist
if ( is_wc_endpoint_url('order-received')
&& WC()->session->__isset('priority_fee') ) :
WC()->session->__unset('priority_fee');
// On Cart page: jQuert script
elseif ( is_cart() ) :
?>
<script type="text/javascript">
jQuery( function($){
if (typeof woocommerce_params === 'undefined')
return false;
var c = 'input[name=priority_fee]';
$(document.body).on( 'click change', c, function(){
console.log('click');
var fee = $(c).is(':checked') ? '1' : '';
$.ajax({
type: 'POST',
url: woocommerce_params.ajax_url,
data: {
'action': 'priority_fee',
'priority_fee': fee,
},
success: function (response) {
setTimeout(function(){
$(document.body).trigger('added_to_cart');
}, 500);
},
});
});
});
</script>
<?php
endif;
}
// Get Ajax request and saving to WC session
add_action( 'wp_ajax_priority_fee', 'priority_fee_ajax_receiver' );
add_action( 'wp_ajax_nopriv_priority_fee', 'priority_fee_ajax_receiver' );
function priority_fee_ajax_receiver() {
if ( isset($_POST['priority_fee']) ) {
$priority_fee = $_POST['priority_fee'] ? true : false;
// Set to a WC Session variable
WC()->session->set('priority_fee', $priority_fee );
echo $priority_fee ? '1' : '0';
die();
}
}
// Add a custom calculated fee conditionally
add_action( 'woocommerce_cart_calculate_fees', 'set_priority_fee' );
function set_priority_fee( $cart ){
if ( is_admin() && ! defined('DOING_AJAX') )
return;
if ( WC()->session->get('priority_fee') ) {
$item_count = $cart->get_cart_contents_count();
$fee_label = 'Discount';
$fee_amount = -2;
$cart->add_fee( $fee_label, $fee_amount );
}
}
But the discount is not showing up. Also, I nneed the discount to be applied to the SUBTOTALS so that tax is recalculated as well (which the code does not do, it applies the discount after taxes).
How can I set a negative fee(discount) when checkbox is checked and also have the discount applied before tax calculation?
EDIT: Okay, I got it to work with the taxes by changing the last part:
// Set the discount
add_action( 'woocommerce_cart_calculate_fees', 'checkout_set_discount', 20, 1 );
function checkout_set_discount( $cart ) {
if ( ( is_admin() && ! defined('DOING_AJAX') ) )
return;
$subtotal = WC()->cart->get_subtotal();
$percentage = 1;
$discount = $subtotal * $percentage / 100;
if( WC()->session->get('priority_fee') ) {
$cart->add_fee( sprintf( __( 'Discount', 'woocommerce') ), -$discount );
}
}
but this only works with a percentage based discount. How can I instead use a fixed discount?

WooCommerce: Avoid checkout If all cart item custom fields are not filled in

I wrote a function that disables the ordering button when additional fields (with display in cart) in each of the products have not been completed (fields appear only if there are more than 2 items in the cart).
function disable_checkout_button_no_shipping() {
global $woocommerce;
$quantity = $woocommerce->cart->cart_contents_count;
if( $quantity > 1 ){
foreach ( WC()->cart->get_cart() as $cart_item ) {
$item_name = $cart_item['data']->get_title();
$notes = $cart_item['notes'];
printf("[%s]\n", $notes);
if ( empty( $notes ) ) {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
printf(
'<div class="wc-cart-note-warning"><span>Przed złożeniem zamówienia dodaj informację dla kogo jest kartka!</span></div>',
);
}
}
}
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );
But it only works when the page is refreshed in the browser.
How can I make the function refresh every time something is entered in the text field assigned to the product and the section with the order button will be refreshed with ajax?
Below is the code that is responsible for adding notes:
Javascrit code saved under update-cart-item-ajax.js file name (custom file):
(function($) {
// $(document).ready(function(){
$('.prefix-cart-notes').on('change keyup paste', function() {
$('.cart_totals').block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var cart_id = $(this).data('cart-id');
$.ajax({
type: 'POST',
url: prefix_vars.ajaxurl,
data: {
action: 'prefix_update_cart_notes',
security: $('#woocommerce-cart-nonce').val(),
notes: $('#cart_notes_' + cart_id).val(),
cart_id: cart_id
},
success: function(response) {
$('.cart_totals').unblock();
}
})
});
});
PHP code saved in active theme functions.php file:
/**
* Add a text field to each cart item
*/
function prefix_after_cart_item_name( $cart_item, $cart_item_key ) {
$notes = isset( $cart_item['notes'] ) ? $cart_item['notes'] : '';
global $woocommerce;
$quantity = $woocommerce->cart->cart_contents_count;
if( $quantity > 1 ){
printf(
'<div><textarea rows="1" placeholder="Dla kogo jest kartka?" maxlength="30" class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$notes
);
}
}
add_action( 'woocommerce_after_cart_item_name', 'prefix_after_cart_item_name', 10, 2 );
/**
* Enqueue our JS file
*/
function prefix_enqueue_scripts() {
//trailingslashit( plugin_dir_url( __FILE__ ) )
wp_register_script( 'prefix-script', trailingslashit( get_home_url() ) . '/wp-content/themes/boomb_theme/assets/update-cart-item-ajax.js', array( 'jquery-blockui' ), time(), true );
wp_localize_script(
'prefix-script',
'prefix_vars',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
)
);
wp_enqueue_script( 'prefix-script' );
}
add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );
/**
* Update cart item notes
*/
function prefix_update_cart_notes() {
// Do a nonce check
if( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'woocommerce-cart' ) ) {
wp_send_json( array( 'nonce_fail' => 1 ) );
exit;
}
// Save the notes to the cart meta
$cart = WC()->cart->cart_contents;
$cart_id = $_POST['cart_id'];
$notes = $_POST['notes'];
$cart_item = $cart[$cart_id];
$cart_item['notes'] = $notes;
WC()->cart->cart_contents[$cart_id] = $cart_item;
WC()->cart->set_session();
wp_send_json( array( 'success' => 1 ) );
exit;
}
add_action( 'wp_ajax_prefix_update_cart_notes', 'prefix_update_cart_notes' );
add_action( 'wp_ajax_nopriv_prefix_update_cart_notes', 'prefix_update_cart_notes' );
function prefix_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if( isset( $values['notes'] ) ) {
$item->add_meta_data( 'notes', $values['notes'], true );
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'prefix_checkout_create_order_line_item', 10, 4 );
In your case, to avoid checkout, use instead woocommerce_checkout_process dedicated hook like:
add_action( 'woocommerce_checkout_process', 'check_cart_notes_to_disable_checkout' );
function check_cart_notes_to_disable_checkout() {
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( ! isset($cart_item['notes']) || empty($cart_item['notes']) ) {
$button = ''. __("Back to cart", "woocommerce") .'';
wc_add_notice( $button . __("Przed złożeniem zamówienia dodaj informację dla kogo jest kartka!", "woocommerce"), 'error' );
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Update totals after applying or removing a coupon programmatically In WooCommerce

I have created a checkbox (It does not look like a checkbox anymore) that apply/remove a coupong on change. This works good. But the total does not update on the apply, the page has to be refreshed. I have build this function with some cut and paste from other functions, it was once a radio field, and it might not be the best practise. The coupong ads a discount for 500 SEK.
But how to do I recalculate the total after the coupong is applied?
As you can see in the end, I have tried WC()->cart->calculate_totals();.
This is the site and checkout: https://www.klubbtryck.se/nif/kassa/
This is my code:
// Add a custom checkout field
add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_form_delivery_addition_nifny', 20 );
function checkout_shipping_form_delivery_addition_nifny(){
$domain = 'wocommerce';
if ( WC()->session->get( 'chosen_shipping_methods' )[0] == 'local_pickup:3' ) :
echo '<tr class="delivery-radio"><th>' . __('Gift Card', $domain) . '</th><td>';
$chosen = WC()->session->get('chosen_delivery');
$chosen = empty($chosen) ? WC()->checkout->get_value('delivery') : $chosen;
$chosen = empty($chosen) ? 0 : $chosen;
if( $chosen == 1){ $chosen = true; } else { $chosen = false; }
// Add a custom checkbox field
woocommerce_form_field( 'radio_delivery', array(
'type' => 'checkbox',
'label' => '<label for="radio_delivery" class="checkbox-label"><span class="presentkortbesk">I have a gift card</span><span class="priset">-500kr</span></label>',
'class' => array( 'form-row-wide' ),
'required' => false,
//'default' => false,
), $chosen );
echo '</td></tr>';
endif;
}
// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_delivery_script_nifny' );
function checkout_delivery_script_nifny() {
// Only checkout page
if ( ! is_checkout() ) return;
?>
<script type="text/javascript">
jQuery( function($){
if (typeof wc_checkout_params === 'undefined')
return false;
$('form.checkout').on('change', 'input[name=radio_delivery]', function(e){
e.preventDefault();
var d = $(this).prop('checked') === true ? 1 : 0;
//var d = $(this).val();
//alert('value: '+d);
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'delivery',
'delivery': d,
},
success: function (result) {
$('body').trigger('update_checkout');
//console.log(result); // just for testing | TO BE REMOVED
},
error: function(error){
//console.log(error); // just for testing | TO BE REMOVED
}
});
});
});
</script>
<?php
}
// Get Ajax request and saving to WC session
add_action( 'wp_ajax_delivery', 'wc_get_delivery_ajax_data_nifny' );
add_action( 'wp_ajax_nopriv_delivery', 'wc_get_delivery_ajax_data_nifny' );
function wc_get_delivery_ajax_data_nifny() {
if ( isset($_POST['delivery']) ){
WC()->session->set('chosen_delivery', sanitize_key( $_POST['delivery'] ) );
echo json_encode( $delivery ); // Return the value to jQuery
}
die();
}
// Add a custom dynamic delivery fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee_nifny', 20, 1 );
function add_packaging_fee_nifny( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for targeted shipping method
if ( WC()->session->get( 'chosen_shipping_methods' )[0] != 'local_pickup:3' )
return;
if( WC()->session->get( 'chosen_delivery' ) == 1 ){
if (!in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
WC()->cart->apply_coupon('card2020');
//WC()->cart->calculate_totals();
}
} else {
if (in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
WC()->cart->remove_coupon('card2020');
}
}
}
You should replace woocommerce_cart_calculate_fees hook that is only made for Fees with similar woocommerce_before_calculate_totals more appropriated hook as follows:
// Add a custom dynamic delivery fee
add_action( 'woocommerce_before_calculate_totals', 'add_packaging_fee_nifny' );
function add_packaging_fee_nifny( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for targeted shipping method
if ( WC()->session->get( 'chosen_shipping_methods' )[0] != 'local_pickup:3' )
return;
if( WC()->session->get( 'chosen_delivery' ) == 1 ){
if (!in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
WC()->cart->apply_coupon('card2020');
//WC()->cart->calculate_totals();
}
} else {
if (in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
WC()->cart->remove_coupon('card2020');
}
}
}
Code goes in functions.php file of the active child theme (or active theme). It should better works.

How to change product (or quantity) on woocommerce checkout page

looking for some help here
so my plan is to have the option to change the product in the checkout page with some radio boxes.
something that looks like this
I've been trying to change the code bellow that is original to add fees.
My goal would be to have the different products on the radio box or at least that it change the quantity.
/**
* #snippet Dynamic Radio Buttons Fees - WooCommerce Checkout
* #how-to Watch tutorial # https://businessbloomer.com/?p=19055
* #sourcecode https://businessbloomer.com/?p=79316
* #author Rodolfo Melogli
* #compatible WooCommerce 3.5.1
* #donate $9 https://businessbloomer.com/bloomer-armada/
*/
// Part 1
// Display Radio Buttons
// Uses woocommerce_form_field()
add_action( 'woocommerce_review_order_before_payment', 'bbloomer_checkout_radio_choice' );
function bbloomer_checkout_radio_choice() {
$chosen = WC()->session->get('radio_chosen');
$chosen = empty( $chosen ) ? WC()->checkout->get_value('radio_choice') : $chosen;
$chosen = empty( $chosen ) ? 'no_option' : $chosen;
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide' ),
'options' => array(
'no_option' => 'No Option',
'option_1' => 'Option 1 ($10)',
'option_2' => 'Option 2 ($30)',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<h3>Customize Your Order!</h3>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
}
// Part 2
// Add Fee and Calculate Total
// Based on session's "radio_chosen"
#2 Calculate New Total
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_checkout_radio_choice_fee', 20, 1 );
function bbloomer_checkout_radio_choice_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$radio = WC()->session->get( 'radio_chosen' );
if ( "option_1" == $radio ) {
$fee = 10;
} elseif ( "option_2" == $radio ) {
$fee = 30;
}
$cart->add_fee( __('Option Fee', 'woocommerce'), $fee );
}
// Part 3
// Refresh Checkout if Radio Changes
// Uses jQuery
add_action( 'wp_footer', 'bbloomer_checkout_radio_choice_refresh' );
function bbloomer_checkout_radio_choice_refresh() {
if ( ! is_checkout() ) return;
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name=radio_choice]', function(e){
e.preventDefault();
var p = $(this).val();
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'woo_get_ajax_data',
'radio': p,
},
success: function (result) {
$('body').trigger('update_checkout');
}
});
});
});
</script>
<?php
}
// Part 4
// Add Radio Choice to Session
// Uses Ajax
add_action( 'wp_ajax_woo_get_ajax_data', 'bbloomer_checkout_radio_choice_set_session' );
add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'bbloomer_checkout_radio_choice_set_session' );
function bbloomer_checkout_radio_choice_set_session() {
if ( isset($_POST['radio']) ){
$radio = sanitize_key( $_POST['radio'] );
WC()->session->set('radio_chosen', $radio );
echo json_encode( $radio );
}
die();
}
code source
You'll not use woocommerce_cart_calculate_fees in this case since you're not adding any fees but rather changing the quantity of the products available in user cart.
So assuming no_option means (1:default), option_1 will change quantity to 10, and option_2 will change quantity to 20, you'd change the function bbloomer_checkout_radio_choice_set_session() to look something like this:
function bbloomer_checkout_radio_choice_set_session() {
if ( isset($_POST['radio']) ){
$radio = sanitize_key( $_POST['radio'] );
$cart = WC()->cart;
$cart_content = $cart->cart_contents;
foreach ( $cart_content as $cart_item_key => $cart_item ) {
if( 'no_option' === $radio ){
$cart->set_quantity($cart_item_key, 1);
}elseif( 'option_1' === $radio ){
$cart->set_quantity($cart_item_key, 10);
}elseif( 'option_2' === $radio ){
$cart->set_quantity($cart_item_key, 20);
}
}
}
echo json_encode( $_POST['radio'] ) ;
die();
}
The above is just an example of how to manipulate the cart products quantity, you'd need change the options and the function to match your exact needs.

Custom add to cart button to add multiple product into cart with quantity :woocommerce

I want to create custom add to cart button to add my 3 product into cart with 2 quantity for each..
For add three product into cart I have done using like this:
<a id="buy" class="single_add_to_cart_button shop-skin-btn shop-flat-btn alt" href="#">ADD MY PRODUCT</a>
$p_id = my product id eg: 45,99,152
<script>
jQuery('#buy').click(function(e) {
e.preventDefault();
var myStringArray = [<?php echo $p_id; ?>];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
addToCart(myStringArray[i]);
}
return true;
});
function addToCart(p_id) {
$.get('/glassful/?post_type=product&add-to-cart=' + p_id, function() {
$(".show_success").show();
});
}
</script>
It will add my resulted product into cart but with only 1 quantity
Please let me know how I can add quantity? I want to add 2 quantity of each product.
Mean when click on add to cart three product will added to cart with 2 quantity each.
Thanks for help in advance..
You need to pass quantity in query string like:
?post_type=product&add-to-cart=100&quantity=2
I have modify you code
<script>
jQuery('#buy').click(function(e) {
e.preventDefault();
var myStringArray = [<?php echo $p_id; ?>];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
addToCart(myStringArray[i],2);
}
return true;
//window.location.href = "http://seoexpertiser.ca/glassful/cart/";
});
function addToCart(p_id,qu) {
$.get('/glassful/?post_type=product&add-to-cart=' + p_id +'&quantity='+qu, function() {
// success
$(".show_success").show();
});
}
</script>
I think this will solve your problem.
Please check this link , This is works for me :)
WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string
functions.php
function woocommerce_maybe_add_multiple_products_to_cart() {
// Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least one comma.
if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
return;
}
// Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
$product_ids = explode( ',', $_REQUEST['add-to-cart'] );
$count = count( $product_ids );
$number = 0;
foreach ( $product_ids as $product_id ) {
if ( ++$number === $count ) {
// Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.
$_REQUEST['add-to-cart'] = $product_id;
return WC_Form_Handler::add_to_cart_action();
}
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
continue;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );
/*
* Sorry.. if you want non-simple products, you're on your own.
*
* Related: WooCommerce has set the following methods as private:
* WC_Form_Handler::add_to_cart_handler_variable(),
* WC_Form_Handler::add_to_cart_handler_grouped(),
* WC_Form_Handler::add_to_cart_handler_simple()
*
* Why you gotta be like that WooCommerce?
*/
if ( 'simple' !== $add_to_cart_handler ) {
continue;
}
// For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
}
}
}
// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );
and you can use for your link.
$product_ids = implode( ',', array( 1, 2, 55 ) );
$url = esc_url_raw( add_query_arg( 'add-to-cart', $product_ids, wc_get_checkout_url() ) );
Thanks !
Save products id in array and make ajax request
jQuery("#addSelected").click(function() { var arrayLength =
arrayOfAddedProducts.length; jQuery.ajax({
type: "POST",
url: ajaxurl,
data: {action : 'add_item_from_cart','getIDtoAdd' : arrayOfAddedProducts},
success: function (res) {
wc_load_all_products();
}
}); });
function.php
function add_item_from_cart() {
foreach ($_POST['getIDtoAdd'] as $productId) {
WC()->cart->add_to_cart( intval($productId), 1, 0, array(), array() );
// WC()->cart->add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() );
}
}
add_action('wp_ajax_add_item_from_cart', 'add_item_from_cart');
add_action('wp_ajax_nopriv_add_item_from_cart', 'add_item_from_cart');

Categories