in the woocommerce mini-cart I'd like to display an input to adjust the quantity of each product.
I added line in the mini-cart.php file of my Storefront child theme (using the function woocommerce_quantity_input), it displays the input, but the cart is not 'updated'.
Any idea how to do this?
See the last line of:
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<li class="woocommerce-mini-cart-item <?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
<?php
//split product name and color
$pattern = '/ [^a-zA-Z0-9 ]+| –/';
$matches = array();
$dummy = preg_match($pattern, $product_name, $matches);
$posi = strpos($product_name, $matches[0]);
$productnamename = substr($product_name, 0, $posi);
$dashAndProductcolor = substr($product_name, $posi);
?>
<?php if ( empty( $product_permalink ) ) : ?>
<?php echo $thumbnail; ?>
<?php echo '<span class="minicart-prod-name">'.$productnamename.'</span><br/>' ?>
<?php echo '<span class="minicart-prod-color">'.$dashAndProductcolor.'</span>' ?>
<?php else : ?>
<a href="<?php echo esc_url( $product_permalink ); ?>">
<?php echo $thumbnail; ?>
<?php echo '<span class="minicart-prod-name">'.$productnamename.'</span><br/>' ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); ?>
<?php echo '<span class="minicart-prod-color">'.$dashAndProductcolor.'</span>' ?>
</a>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="each-price">' . sprintf( '%s', $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
<?php endif; ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s', $cart_item['quantity'] ) . '</span>', $cart_item, $cart_item_key ); ?>
<?php
echo woocommerce_quantity_input( array('input_value' => $cart_item['quantity']),$cart_item['data'], false );
?>
Related
I would like to show a notification on the cart page, if logged in user has already purchased the product before.
On the checkout page I managed to display it
by adding this checkout/review-order.php around line no 31
if( is_user_logged_in() ) {
$user = wp_get_current_user();
}
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-name">
<?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( '× %s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php
// Here is your code -- Start
if( is_user_logged_in() && wc_customer_bought_product( $user->user_email, $user->ID, $_product->get_id() ) ) {
echo apply_filters( 'woocommerce_checkout_cart_alredy_bought', '<div class="user-bought">' . sprintf( "Hi %s you already purchased in the past.", $user->first_name ) . '</div>' );
}
// Here is your code -- End
?>
</td>
<td class="product-total">
<?php echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
</tr>
<?php
}
}
For cart page in cart/cart.php around what line number i need to add that code?
Instead of overwriting the template files, you can use the woocommerce_after_cart_item_name action hook
So you get:
function action_woocommerce_after_cart_item_name( $cart_item, $cart_item_key ) {
// Only for logged-in users
if ( ! is_user_logged_in() ) return;
// Get current user
$user = wp_get_current_user();
// Get product ID
$product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
// If true
if ( wc_customer_bought_product( $user->user_email, $user->ID, $product_id ) ) {
echo '<p class="my-class">' . sprintf( __( 'Hi %s you already purchased this product in the past.', 'woocommerce' ), $user->first_name ) . '</p>';
}
}
add_action( 'woocommerce_after_cart_item_name', 'action_woocommerce_after_cart_item_name', 10, 2 );
I'm trying to make my custom mini-cart. Cart should be updated without page reload(ajax) after item removing, but it is not happens, I have infinite load on front-end and see changes only after page reload.
My mini-cart action
function my_wc_mini_cart() {
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$count = WC()->cart->cart_contents_count;
$cart = WC()->cart->get_cart();
?>
<?php _e('Cart', 'frosted'); ?> <span class="cart__amount"><?php echo esc_html( $count ); ?></span>
<div class="sub-menu sub-menu--right sub-menu--cart">
<?php
foreach ( $cart as $cart_item_key => $cart_item ):
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
$variation_val = $cart_item['variation']['attribute_pa_size'];
$term_obj = get_term_by('slug', $variation_val, 'pa_size');
$size_name = $term_obj->name;
?>
<div class="media mini-cart__item woocommerce-mini-cart-item <?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
<?php echo $thumbnail; ?>
<div class="media-body mini-cart__item_body">
<div class="mini-cart__item__heading mt-0"><?php echo $product_name; ?></div>
<?php
echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<div class="cart__item__price">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</div>', $cart_item, $cart_item_key ); ?>
<div class="mini-cart__item__size"><?php echo $size_name; ?></div>
</div>
<div class="mini-cart__item_remove ">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $cart_item_key ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
<span class="btn__text"><?php _e('Checkout', 'frosted'); ?></span>
</div>
<?php
}
}
add_action( 'frosted_header_top', 'my_wc_mini_cart' );
Action for updating cart using ajax, maybe I'm suing wrong hook...
function my_header_add_to_cart_fragment( $fragments ) {
ob_start();
$count = WC()->cart->cart_contents_count;
$cart = WC()->cart->get_cart();
?>
<?php _e('Cart', 'frosted'); ?> <span class="cart__amount"><?php echo esc_html( $count ); ?></span>
<div class="sub-menu sub-menu--right sub-menu--cart">
<?php
foreach ( $cart as $cart_item_key => $cart_item ):
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
$variation_val = $cart_item['variation']['attribute_pa_size'];
$term_obj = get_term_by('slug', $variation_val, 'pa_size');
$size_name = $term_obj->name;
?>
<div class="media mini-cart__item woocommerce-mini-cart-item <?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
<?php echo $thumbnail; ?>
<div class="media-body mini-cart__item_body">
<div class="mini-cart__item__heading mt-0"><?php echo $product_name; ?></div>
<?php
echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<div class="cart__item__price">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</div>', $cart_item, $cart_item_key ); ?>
<div class="mini-cart__item__size"><?php echo $size_name; ?></div>
</div>
<div class="mini-cart__item_remove">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $cart_item_key ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
<span class="btn__text"><?php _e('Checkout', 'frosted'); ?></span>
</div>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
Looking for an advices and your help.
UPDATE: changed $fragments['a.cart-contents'] = ob_get_clean(); to $fragments['div.widget_shopping_cart_content'] = ob_get_clean(); to make it return right fragment(not two, like on sreenshot), but didn't help
The following will compact your code and will ajax refresh the mini-cart count + the mini-cart content in the right way:
// Utility function that outputs the mini cart content
function my_wc_mini_cart_content(){
$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item_key => $cart_item ):
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
if(isset($cart_item['variation']['attribute_pa_size'])) {
$variation_val = $cart_item['variation']['attribute_pa_size'];
$term_obj = get_term_by('slug', $variation_val, 'pa_size');
$size_name = $term_obj->name;
}
?>
<div class="media mini-cart__item woocommerce-mini-cart-item <?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
<?php echo $thumbnail; ?>
<div class="media-body mini-cart__item_body">
<div class="mini-cart__item__heading mt-0"><?php echo $product_name; ?></div>
<?php
echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<div class="cart__item__price">' .
sprintf( '%s × %s', $cart_item['quantity'], $product_price ) .
'</div>', $cart_item, $cart_item_key );
if( isset($size_name) ) { ?>
<div class="mini-cart__item__size"><?php echo $size_name; ?></div>
<?php } ?>
</div>
<div class="mini-cart__item_remove ">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $cart_item_key ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</div>
</div>
<?php
}
endforeach; ?>
<span class="btn__text"><?php _e('Checkout', 'frosted'); ?></span>
<?php
}
// Hooked: The mini cart count and the cart content
add_action( 'frosted_header_top', 'my_wc_mini_cart' );
function my_wc_mini_cart() {
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$count = WC()->cart->get_cart_contents_count();
?>
<?php _e('Cart', 'frosted'); ?> <span id="cart_count" class="cart__amount"><?php echo esc_html( $count ); ?></span>
<div id="mini-cart-content" class="sub-menu sub-menu--right sub-menu--cart">
<?php my_wc_mini_cart_content(); ?>
</div>
<?php
}
}
// Ajax refreshing mini cart count and content
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
function my_header_add_to_cart_fragment( $fragments ) {
$count = WC()->cart->get_cart_contents_count();
$fragments['#cart_count'] = '<span id="cart_count" class="cart__amount">' . esc_attr( $count ) . '</span>';
ob_start();
?>
<div id="mini-cart-content" class="sub-menu sub-menu--right sub-menu--cart">
<?php my_wc_mini_cart_content(); ?>
<div>
<?php
$fragments['#mini-cart-content'] = ob_get_clean();
return $fragments;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
After trying to use solution marked as correct answer, the remove function still not worked for me, but I found another solution here.
adding widget_shopping_cart_content classname to my mini-cart's container made it work
I'm generating my cart as such:
if( !is_wp_error( $this->getCartContentsCount() ) ) {
$cart_items = WC()->cart->get_cart();
foreach( $cart_items as $item ) { ?>
<div class="left col-md-3">
<div class="product-image"><!-- Make sure ot check if it's a gallery, if so, get its first image -->
<?php echo $item['data']->get_image(); ?>
</div>
</div>
<div class="right col-md-8">
<h4 class="product-name"><?php echo $item['data']->get_name(); ?></h4>
<div class="product-information">
<span class="product-quantity"><?php echo $item['quantity'] . 'x' ?></span>
<span class="product-price"><?php echo $item['data']->get_price_html(); ?></span>
</div>
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'X',
esc_url( wc_get_cart_remove_url( $item ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $item['product_id'] ),
esc_attr( $item['data']->get_sku() )
), $item );
?>
</div>
<?php }
}
As you can see, I'm using the basic Woocommerce implementation of the "remove item" mechanism as per cart.php, but unfortunately, it doesn't work.
This is what Woo generates:
X
I have checked every attribute and it is indeed valid!
Other symptoms:
The link, if accessed directly, takes me to the cart itself (I
believe it is due to the nonce).
Dumping the cart before and after the click still shows the same
cart with just one item in it, so the mechanism to delete doesn't
work as the cart doesn't change its state.
How come it doesn't work?
I actually went ahead and copied the cart.php implementation and customized it to my needs:
ob_start();
if( !is_wp_error( $this->getCartContentsCount() ) ) { //This checks if there are any items in the cart.
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters(
'woocommerce_cart_item_product',
$cart_item['data'], $cart_item, $cart_item_key
);
$product_id = apply_filters(
'woocommerce_cart_item_product_id',
$cart_item['product_id'], $cart_item, $cart_item_key
);
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters(
'woocommerce_cart_item_permalink',
$_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key
); ?>
<div class="cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<div class="left col-md-3">
<div class="product-image">
<?php
$thumbnail = apply_filters(
'woocommerce_cart_item_thumbnail',
$_product->get_image(), $cart_item, $cart_item_key
);
if ( !$product_permalink ) {
echo wp_kses_post( $thumbnail );
} else {
printf( '%s', esc_url( $product_permalink ), wp_kses_post( $thumbnail ) );
}
?>
</div>
</div>
<div class="right col-md-8">
<h4 class="product-name">
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters(
'woocommerce_cart_item_name',
$_product->get_name(), $cart_item, $cart_item_key ) . ' '
);
} else {
echo wp_kses_post( apply_filters(
'woocommerce_cart_item_name',
sprintf(
'%s',
esc_url( $product_permalink ), $_product->get_name()
), $cart_item, $cart_item_key
)
);
}
// Meta data.
echo wc_get_formatted_cart_item_data( $cart_item ); // PHPCS: XSS ok.
// Backorder notification.
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo wp_kses_post( apply_filters(
'woocommerce_cart_item_backorder_notification',
'<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>'
)
);
}
?>
</h4>
<div class="product-information">
<div class="product-quantity">
<?php
$product_quantity = sprintf(
'%s<input type="hidden" name="cart[%s][qty]" value="%s" />',
$cart_item['quantity'], $cart_item_key, $cart_item['quantity']
);
echo apply_filters(
'woocommerce_cart_item_quantity',
$product_quantity, $cart_item_key, $cart_item
); // PHPCS: XSS ok.
?>
</div>
<div class="product-price">
<?php
echo apply_filters(
'woocommerce_cart_item_price',
WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</div>
</div>
<div class="product-remove">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</div>
</div>
</div>
<?php
}
}
}
$markup_output = ob_get_contents();
ob_end_clean();
echo $markup_output;
Simple use this WC_Cart::remove_cart_item( $cart_item_key );
I need to sort my cart items by variation addon values.
For example I have 5 items in cart, and they have variation date:
first item date - 2016-05-05;
second 2016-05-08;
third 2016-05-20 and so on.
How can I sort them in cart ? I found that all info are in $cart_item[‘addons’] , but don’t find solution for how to sort them.
I editing my cart.php
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-name">
<div class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $_product->is_visible() )
echo $thumbnail;
else
printf( '%s', $_product->get_permalink(), $thumbnail );
?>
</div>
</td>
<td class="product-details">
<div class="cart-item-details">
<?php
if ( ! $_product->is_visible() )
echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key );
else
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', $_product->get_permalink(), $_product->get_title() ), $cart_item, $cart_item_key );
// Meta data
echo WC()->cart->get_item_data( $cart_item );
// Backorder notification
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) )
echo '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'xstore' ) . '</p>';
?>
<span class="mobile-price">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
</span>
</div>
</td>
<td class="product-price">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
</td>
<td class="product-quantity">
<?php
if ( $_product->is_sold_individually() ) {
$product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
} else {
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(),
'min_value' => '0'
), $_product, false );
}
echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key );
?>
</td>
<td class="product-subtotal">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
?>
</td>
<td class="product-remove">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf( 'X', esc_url( WC()->cart->get_remove_url( $cart_item_key ) ), __( 'Pašalinti produktą', 'xstore' ) ), $cart_item_key );
?>
</td>
</tr>
<?php
}
}
I am using woocommerce and I would like to remove the hyperlinks to single product pages as i use a quickview feature instead, making content pop up in a lightbox and making single product pages unneeded.
However, the links that are generated in the cart and mini-cart link to the default product page so I would like to remove them.
What I want to do for the cart page: Remove the hyperlink to the single product page from the product thumbnail I am not a php expert per se, but made some progress following this post. It worked on my product title but it didn't work on my product thumbnail in the cart page.
Here is the snippet from my cart.php:
<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail;
} else {
printf( '%s', $thumbnail );
}
?>
</td>
<td class="product-name" data-title="<?php _e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . ' ';
} else {
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', $_product->get_title() ) );
}
// Meta data
echo WC()->cart->get_item_data( $cart_item );
// Backorder notification
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
}
?>
</td>
What I want to do for the mini-cart: Remove the hyperlink to the single product page from the product thumbnail and product title
and the snippet from my mini-cart.php: (Full mini-cart.php code here)
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
<li class="<?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( WC()->cart->get_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
<?php if ( ! $_product->is_visible() ) : ?>
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
<?php else : ?>
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
</a>
<?php endif; ?>
<?php echo WC()->cart->get_item_data( $cart_item ); ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
</li>
<?php
}
}
?>
Both product thumbnail and product title is still hyperlinked despite removing the <a href> tag.
Please help!!
screenshot of hyperlinked product title and thumbnail
For cart.php
You can change these two lines
replace
printf( '%s', esc_url( $product_permalink ), $thumbnail );
with
printf( '%s', $thumbnail );
and
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_title() ), $cart_item, $cart_item_key );
with
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', $_product->get_title() ), $cart_item, $cart_item_key );
or you can use the filters to get the job done, if you are using a child theme in your child-theme/woocommerce/cart/cart.php make the changes .
In mini-cart.php
replace
<a href="<?php echo esc_url( $product_permalink ); ?>">
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
</a>
with
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
page should be at chil-theme/woocommerce/cart/mini-cart.php
The page link you have provided me is the order page, for which the page should be used is order-details-item.php
here you should replace
echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '%s', $product_permalink, $item['name'] ) : $item['name'], $item, $is_visible );
with
echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '%s', $item['name'] ) : $item['name'], $item, $is_visible );
let me know if you can share a screenshot of the page, that will be better if it wont work for you or you can allow me to create a fake order , so that I can check it.
if you are facing problem let me know.