Having a problem getting the Woocommerce cart subtotal price to show a crossed-out regular price and a sale price.
I have tried working with
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
and
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
However, I've had no luck. Any advice?
<?php
if($cart_item[ 'data' ]->is_on_sale()){
echo '<del aria-hidden="true"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span>'.$cart_item[ 'data' ]->get_regular_price() * $cart_item['quantity'].'</bdi></span></del> <ins><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span>'.$cart_item[ 'data' ]->get_price() * $cart_item['quantity'].'</bdi></span></ins>'; // PHPCS: XSS ok.
}else{
echo '<span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span>'.$cart_item[ 'data' ]->get_price() * $cart_item['quantity'].'</bdi></span>'; // PHPCS: XSS ok.
}
?>
Related
I would like to change the order of items in the checkout order area.
I want to show the product quantity before the product name.
So instead of:
Air flow meter oxygen × 1
I would like to show:
1 x Air flow meter oxygen
I've managed to find the review-order.php and added it to my child theme so it's update compatible.
I changed the order like this:
<?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( $cart_item['quantity'], '× %s' ) . '</strong>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) ) . ' '; ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
It now shows:
x 1
Air flow meter oxygen
I can't seem to figure out how:
I can remove the break after the quantity so it is set on one line.
And how to change the order of the number and the x
I already switched the $cart_item['quantity'] and the '× %s'. But I guess that does nothing. Any advice?
To change the order of items in the checkout order area, in /checkout/review-order.php #version 5.2.0
Replace:
<td class="product-name">
<?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) ) . ' '; ?>
<?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 ?>
</td>
With:
<td class="product-name">
<?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 wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) ) . ' '; ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
This template can be overridden by copying it to yourtheme/woocommerce/checkout/review-order.php.
Result:
We created an additional page on our webshop for some kind of 'quotation'. On that page we'd like to load the cart. The contents of the 'quotation' is basically the same as the cart content.
We tried copying all of the WooCommerce code from the template file cart.php.
Everything is showing correctly, except for the prices. Our quotation cart is showing the basic prices that are set up in the WooCommerce back-end.
But here's our problem: When adding a product to the cart, there are additional price settings which we custom coded by using some hooks, like cart item data that's being added.
All of our custom code works perfectly fine when opening the regular WooCommerce cart, but when showing that exact Cart on another page, prices are 'reset' to the regular starting prices.
The reason we copied the code from the cart.php template file is the ability to change the structure with additional css/html.
Here you can find the shortcode & complete function of our 'quotation' cart:
<?php function toon_winkelmand_offerte() {
do_action( 'woocommerce_before_cart_table' );?>
<form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<div class="offerte_wrapper">
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
?>
<div class="offerte_item">
<?php
$_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 );
$product_qty = $cart_item['quantity'];
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="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
?>
</div>
<div class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' ' );
?>
<span class="offerte_qty"><?php echo ' x ';
echo $product_qty;
?>
</span>
<?php
} 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 ) );
?>
<span class="offerte_qty"><?php echo ' x ';
echo $product_qty;
?>
</span>
<?php
}
do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );
echo wc_get_formatted_cart_item_data( $cart_item );
?>
<div class="product_prijs">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
<span class="prijs_per_stuk"><?php echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
echo ' ';
printf(esc_html__('per stuk', 'astra-child'));
// print_r($_product);
?>
</span>
</div>
</div>
<div class="product-remove">
<?php
echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'woocommerce_cart_item_remove_link',
sprintf(
'<img src="https://kreatixlabs.be/rivanco/wp-content/uploads/2021/08/trash.png">',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
esc_html__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
),
$cart_item_key
);
?>
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
</form>
<?php
}
add_shortcode('toon_winkelmand_offerte', 'toon_winkelmand_offerte');?>
Is it possible to move the "remove product" link from the first <td> in the cart under the quantity selector? And delete the first <td> with it?
I found the funvtion in the template cart/cart.php.
Unfortunately I couldn't find any hooks to change the position or delete the <td>.
Is there really no way to change that without touching/copy the template file?
This ist the <td> with the quantity field:
<td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
<?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 ) );
}
do_action( 'woocommerce_after_cart_item_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>', $product_id ) );
}
?>
</td>
I don't want to touch the template because it's the only detail that I want to change and couldn't do at the moment. And for that "little" detail I don't want to chage such a big and crucial template file. I don't have to worry about future updates an newer versions of the file.
Please paste the below code in your current theme functions.php file.
add_filter('woocommerce_cart_item_remove_link','remove_cart_icon',10,2);
function remove_cart_icon($plink,$link){
return '';
}
add_filter('woocommerce_cart_item_quantity','change_cart_remove_link',10,3);
function change_cart_remove_link($product_quantity, $cart_item_key, $cart_item){
//create new product object
$_product = new WC_Product($cart_item['product_id']);//product_id
$cart_remove_link = sprintf(
'<span class="button_icon">×</span>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $_product->get_id() ),
esc_attr( $_product->get_sku() )
);
$product_quantity .= $cart_remove_link;
return $product_quantity;
}
I hope it will help you.
you can use jquery to do that
here it is
jQuery(document).ready(function(){
jQuery(".remove").appendTo(".quantity");
});
I am trying to display the count of the items in the cart at the bottom or top of the Woocommerce cart widget. It seems that by default behaviour the cart widget does not do this when in the sidebar.
I have researched SO and found the following answer, but it has a slightly different goal of adding individual subtotals. I have tried modifying it, by passing in WC()->cart->get_cart_contents_count() instead of WC()->cart->get_cart() to see if I can get the cart item count, but it's not displaying...any suggestions?
Example of what I am trying to do:
Code I have modified from the approved answer:
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>
<?php do_action( 'woocommerce_before_mini_cart' ); ?>
<ul class="cart_list product_list_widget <?php echo $args['list_class']; ?>">
<?php if ( WC()->cart->get_cart_contents_count() ) > 0 ) : ?>
<?php
foreach ( WC()->cart->get_cart_contents_count() 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 );
echo "<p>".$product_price."</p>";
?>
<li>
<?php if ( ! $_product->is_visible() ) { ?>
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name; ?>
<?php } else { ?>
<a href="<?php echo get_permalink( $product_id ); ?>">
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name; ?>
</a>
<?php } ?>
<?php echo WC()->cart->get_item_data( $cart_item ); ?>
<?php $new_product_price_array = explode ( get_woocommerce_currency_symbol(), $product_price);
$new_product_price = number_format((float)$new_product_price_array[1] * $cart_item['quantity'], 2, '.', '');
?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s=%s%s', $cart_item['quantity'], $product_price,get_woocommerce_currency_symbol(), $new_product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
</li>
<?php
}
}
?>
<?php else : ?>
<li class="empty"><?php _e( 'No products in the cart.', 'woocommerce' ); ?></li>
<?php endif; ?>
</ul><!-- end product list -->
<?php if ( WC()->cart->get_cart_contents_count() ) > 0 ) : ?>
<p class="total"><strong><?php _e( 'Subtotal', 'woocommerce' ); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>
<?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>
<p class="buttons">
<?php _e( 'View Cart', 'woocommerce' ); ?>
<?php _e( 'Checkout', 'woocommerce' ); ?>
</p>
<?php endif; ?>
<?php do_action( 'woocommerce_after_mini_cart' ); ?>
If you want to display the cart item count in the mini cart widget you can use hooks:
1) On top before minicart content:
add_action( 'woocommerce_before_mini_cart', 'minicart_count_after_content' );
function minicart_count_after_content() {
$items_count = WC()->cart->get_cart_contents_count();
$text_label = _n( 'Item', 'Items', $items_count, 'woocommerce' );
?>
<p class="total item-count"><strong><?php echo $text_label; ?>:</strong> <?php echo $items_count; ?></p>
<?php
}
2) On the bottom before the buttons:
add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'minicart_count_before_content' );
function minicart_count_before_content() {
$items_count = WC()->cart->get_cart_contents_count();
$text_label = _n( 'Item', 'Items', $items_count, 'woocommerce' );
?>
<p class="total item-count"><strong><?php echo $text_label; ?>:</strong> <?php echo $items_count; ?></p>
<?php
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
WC()->cart->get_cart_contents_count(); will give you the total items count including quantities.
If you want to get the number of items in cart you will use instead:
$items_count = sizeof( WC()->cart->get_cart() );
You aren't echoing WC()->cart->get_cart_contents_count() anywhere, so no, it won't display.
Wherever in the code you need to display the count, you'll need to use
echo WC()->cart->get_cart_contents_count();
I am using WooCommerce mini cart widget and I want to limit the number of products shown in the mini cart. If a user adds 10 products to the cart, the mini cart widget shows all 10.
I would like to limit the number lets say to 5 products.
Is there a way to achieve this?
Thanks.
You can achieve this overriding cart/mini-cart.php WooCommerce template via your active theme, ( see this related docs: Template Structure + Overriding Templates via a Theme ).
Briefly (if not done yet), you will need to copy from woocommerce plugin folder, a subfolder named templates to your active child theme (or theme) and to rename it woocommerce..
After that you find inside that new woocommerce folder in cart subfolder a template named mini-cart.php.
Open/edit mini-cart.php template and replace the code by this to limit cart to 5 items:
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget.
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php do_action( 'woocommerce_before_mini_cart' ); ?>
<ul class="cart_list product_list_widget <?php echo $args['list_class']; ?>">
<?php if ( ! WC()->cart->is_empty() ) : ?>
<?php
// Define HERE the number of items
$number_of_items = 5;
$loop_count = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Limiting number of items displayed in mini cart
if($loop_count < $number_of_items) {
// The counter
$loop_count++;
$_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 );
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $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 : ?>
<a href="<?php echo esc_url( $product_permalink ); ?>">
<?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
}
}
}
?>
<?php else : ?>
<li class="empty"><?php _e( 'No products in the cart.', 'woocommerce' ); ?></li>
<?php endif; ?>
</ul><!-- end product list -->
<?php if ( ! WC()->cart->is_empty() ) : ?>
<p class="total"><strong><?php _e( 'Subtotal', 'woocommerce' ); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>
<?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>
<p class="buttons">
<?php _e( 'View Cart', 'woocommerce' ); ?>
<?php _e( 'Checkout', 'woocommerce' ); ?>
</p>
<?php endif; ?>
<?php do_action( 'woocommerce_after_mini_cart' ); ?>
Here we count the number of items in the foreach loop and we limit them to a specified number in variable $number_of_items.