Skip to content

Instantly share code, notes, and snippets.

@bhavik-kiri
bhavik-kiri / add_quantity.css
Last active June 21, 2017 11:25
We will add the CSS for the delete icon.
#content table.shop_table a.remove {
color: red;
display: block;
font-size: 20px;
font-weight: 700;
height: 20px;
line-height: 20px;
padding: 0;
text-align: center;
text-decoration: none;
@bhavik-kiri
bhavik-kiri / enqueue_css.php
Last active June 21, 2017 11:26
It will enqueue the CSS file in the footer of the store
<?php
function add_css(){
if ( is_checkout() ) {
wp_enqueue_style( 'checkout_style', plugins_url( '/assets/css/change-quantity-on-checkout.css', __FILE__ ), '', '', false );
}
}
add_action( 'wp_footer', 'add_css' ), 10 );
@bhavik-kiri
bhavik-kiri / add_quantity.php
Last active June 21, 2017 11:27
We will add Delete icon and Quantity selector along with Product name
<?php
/*
* It will add Delete button, Quanitity field on the checkout page Your Order Table.
*/
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();
@bhavik-kiri
bhavik-kiri / load_ajax_function.php
Last active June 21, 2017 11:28
It will add the ajax action
<?php
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' );
@bhavik-kiri
bhavik-kiri / ajax_function.php
Last active June 21, 2017 11:29
It will modify the quantity & calculate the total of the cart
<?php
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();
}
@bhavik-kiri
bhavik-kiri / add_quantity.js
Last active June 21, 2017 12:20
It will call the ajax when customer click on the quantity button
jQuery(function( $ ) {
$( "form.checkout" ).on( "click", "input.qty", function( e ) {
var data = {
action: 'update_order_review',
security: wc_checkout_params.update_order_review_nonce,
post_data: $( 'form.checkout' ).serialize()
};
jQuery.post( add_quantity.ajax_url, data, function( response )
{
/**
* Save the persistent cart when the cart is updated.
*/
public function persistent_cart_update() {
update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart',
array( 'cart' => WC()->session->get( 'cart' ),
) );
}
/**
* Get the cart data from the PHP session and store it in class variables.
*/
public function get_cart_from_session() {
// Load cart session data from session
foreach ( $this->cart_session_data as $key => $default ) {
$this->$key = WC()->session->get( $key, $default );
}
$update_cart_session = false;
/**
* Save the persistent cart when the cart is updated.
*/
public function persistent_cart_update() {
update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(),
array( 'cart' => WC()->session->get( 'cart' ),
) );
}
<?php
add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
function add_fields_before_add_to_cart( ) {
?>
<table>
<tr>
<td>
<?php _e( "Name:", "aoim"); ?>
</td>