Created
July 30, 2020 16:49
-
-
Save SamuelHadsall/bebb98baaf08cc3c3925932d3440c2d4 to your computer and use it in GitHub Desktop.
Updates Quantity on Checkout Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_quantity( $product_title, $cart_item, $cart_item_key ) { | |
/* Checkout page check */ | |
if ( is_checkout() ) { | |
/* Get Cart of the user */ | |
$cart = WC()->cart->get_cart(); | |
foreach ( $cart as $cart_key => $cart_value ){ | |
if ( $cart_key == $cart_item_key ){ | |
$product_id = $cart_item['product_id']; | |
$_product = $cart_item['data'] ; | |
/* Add delete icon */ | |
$return_value = sprintf( | |
'<a href="%s" class="remove" title="%s" data-product_id="%s" data-product_sku="%s">×</a>', | |
esc_url( WC()->cart->get_remove_url( $cart_key ) ), | |
__( 'Remove this item', 'woocommerce' ), | |
esc_attr( $product_id ), | |
esc_attr( $_product->get_sku() ) | |
); | |
/* Add product name */ | |
$return_value .= '<span class = "product_name" >' . $product_title . '</span>' ; | |
/* Add quantity selector */ | |
if ( $_product->is_sold_individually() ) { | |
$return_value .= sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_key ); | |
} else { | |
$return_value .= woocommerce_quantity_input( array( | |
'input_name' => "cart[{$cart_key}][qty]", | |
'input_value' => $cart_item['quantity'], | |
'max_value' => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(), | |
'min_value' => '1' | |
), $_product, false ); | |
} | |
return $return_value; | |
} | |
} | |
}else{ | |
/* | |
* This will return the product name on the cart page. | |
* As the filter used on checkout and cart are same. | |
*/ | |
$_product = $cart_item['data'] ; | |
$product_permalink = $_product->is_visible() ? $_product->get_permalink( $cart_item ) : ''; | |
if ( ! $product_permalink ) { | |
$return_value = $_product->get_title(); | |
} else { | |
$return_value = sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_title()); | |
} | |
return $return_value; | |
} | |
} | |
/* Adds js at the footer to update Cart via ajax */ | |
function add_quanity_js(){ | |
if ( is_checkout() ) { | |
wp_enqueue_script( 'checkout_script', get_stylesheet_directory_uri() . "/js/update-checkout.js", '', '', false ); | |
$localize_script = array( | |
'ajax_url' => admin_url( 'admin-ajax.php' ) | |
); | |
wp_localize_script( 'checkout_script', 'add_quantity', $localize_script ); | |
} | |
} | |
add_action( 'wp_footer', 'add_quanity_js', 10 ); | |
//This loads values from ajax call | |
//This is also used in our script.js file for our ajax call | |
function load_ajax() { | |
if ( !is_user_logged_in() ){ | |
add_action( 'wp_ajax_nopriv_update_order_review', 'update_order_review' ); | |
} else{ | |
add_action( 'wp_ajax_update_order_review', 'update_order_review' ); | |
} | |
} | |
add_action( 'init', 'load_ajax' ); | |
//This updates Order Review | |
function update_order_review() { | |
$values = array(); | |
parse_str($_POST['post_data'], $values); | |
$cart = $values['cart']; | |
foreach ( $cart as $cart_key => $cart_value ){ | |
WC()->cart->set_quantity( $cart_key, $cart_value['qty'], false ); | |
WC()->cart->calculate_totals(); | |
woocommerce_cart_totals(); | |
} | |
wp_die(); | |
} | |
/* Javascript */ | |
(function ($) { | |
$(function () { | |
var $checkout = $('form.woocommerce-checkout'); | |
//Update Cart | |
$checkout.on('click', '.minus-btn, .plus-btn', function (e) { | |
var $qty = $(this).closest('.quantity').find('.qty'); | |
var data = { | |
//Make sure the action here is the same as our load_ajax function | |
//This is found in our function.php file | |
action: 'update_order_review', | |
security: wc_checkout_params.update_order_review_nonce, | |
post_data: $('form.checkout').serialize() | |
}; | |
$.ajax({ | |
type: 'POST', | |
url: add_quantity.ajax_url, | |
data: data, | |
success: function (data) { | |
$qty.trigger('update_checkout'); | |
} | |
}); | |
}); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment