Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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 Image */
$cqoc_image_size = array(25, 25);
$thumbnail = $_product->get_image( $cqoc_image_size );
if ( ! $product_permalink ) {
$return_value .= $thumbnail;
} else {
$return_value .= sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $thumbnail );
}
/* Step 3 : Add product name */
$return_value .= '&nbsp; <span class = "product_name" >' . $product_title . '</span>' ;
/* Step 4 : 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
You can’t perform that action at this time.