Skip to content

Instantly share code, notes, and snippets.

@bhavik-kiri
Last active June 21, 2017 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhavik-kiri/e6b06b076ae2d3c164ba4174cf9b76f8 to your computer and use it in GitHub Desktop.
Save bhavik-kiri/e6b06b076ae2d3c164ba4174cf9b76f8 to your computer and use it in GitHub Desktop.
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();
foreach ( $cart as $cart_key => $cart_value ){
if ( $cart_key == $cart_item_key ){
$product_id = $cart_item['product_id'];
$_product = $cart_item['data'] ;
/* Step 1 : Add delete icon */
$return_value = sprintf(
'<a href="%s" class="remove" title="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
esc_url( WC()->cart->get_remove_url( $cart_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
);
/* Step 2 : Add product name */
$return_value .= '&nbsp; <span class = "product_name" >' . $product_title . '</span>' ;
/* Step 3 : 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{
/*
* It 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() . '&nbsp;';
} else {
$return_value = sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_title());
}
return $return_value;
}
}
add_filter ('woocommerce_cart_item_name', 'add_quantity' , 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment