I use cart Widget as minicart details in top menu. In Mini cart details page display wrong cart subtotal (Product Amount without Discount). but checkout page display correct total amount how to fix it.
if ( WC()->cart->get_cart_contents_count() > 0 ) { echo '<span class="circle" id="mini-cart-details">' . WC()->cart->get_cart_contents_count() . ' items - '.WC()->cart->get_cart_total().'</span>'; } else { echo '<span class="circle" id="mini-cart-details"> items - '.WC()->cart->get_cart_total().'</span>'; }
I have the same issue with taxes. $order->get_formatted_order_total() returns incorrect and different value than $order->get_total().
Related
On my WooCommerce checkout page, I am trying to show a notification before the order review section when the total of the cart equals €0.01
So far, I have placed the following in the functions.php file of my child theme:
add_action('woocommerce_checkout_before_order_review', 'test_funtion');
function test_funtion(){
?>
<p>Notice goes here</p>
<?php
}
How can I modify these lines such that this notice is only shown if the order total equals € 0.01?
You can use WC_Cart::get_cart_contents_total(); - Gets cart total. This is the total of items in the cart, but after discounts. Subtotal is before discounts.
So you get:
function action_woocommerce_checkout_before_order_review () {
// Get cart total
$cart_total = WC()->cart->get_cart_contents_total();
// Compare
if ( $cart_total == 0.01 ) {
echo '<p>' . __( 'My message', 'woocommerce' ) . '</p>';
}
}
add_action( 'woocommerce_checkout_before_order_review', 'action_woocommerce_checkout_before_order_review', 10, 0 );
I am using the cart widget from WooCommerce -> mini-cart.php
I am adding products to it via AJAX and added some elements already to it myself. Standard it shows the subtotal price in the cart but I want to show the shipping costs after it and after that the total price (subtotal + shipping).
So the cart will look like this:
- Products in cart
- Subtotal
- Shipping costs
- Total price (shipping + subtotal)
I have added the total price with this code:
<p><?php echo WC()->cart->get_cart_total(); ?></p>
But this echo's the same price as the subtotal because the shipping isn't added to it. Can someone help me to add the shipping costs to it. Some hook or code.
To get and display the chosen shipping method label (and other related data, if needed) in cart page (or in checkout page):
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
if( WC()->session->get('chosen_shipping_methods')[0] == $method_id ){
$rate_label = $rate->label; // The shipping method label name
$rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
// The taxes cost
$rate_taxes = 0;
foreach ($rate->taxes as $rate_tax)
$rate_taxes += floatval($rate_tax);
// The cost including tax
$rate_cost_incl_tax = $rate_cost_excl_tax + $rate_taxes;
echo '<p class="shipping-total">
<strong class="label">'.$rate_label.': </strong>
<span class="totals">'. WC()->cart->get_cart_shipping_total() .'</span>
</p>';
break;
}
}
In woocommerce, is it possible to display the sum of total stock of variations in variable product? I've the code from here Get the total stock of all variations from a variable product In Woocommerce
and its working but it displays two stocks infos. for example, I leave the stock in variable product as blank (because I wanted it to be auto generated as the sum of variation.) The default stock display say its out of stock, while the other one which is in the code displays the accurate one(one that highlighted in the picture). please help
I've used this code in the red text stocks.
function show_stock() {global $product;if ( $product->stock ) { // if manage stock is enabled if ( ! $product->managing_stock() && ! $product->is_in_stock() ) echo '';}if ( number_format($product->stock,0,'','') > 0 ) { // if stock is lowecho '<div class="remainingpc" style="text-align:center;"><font color="red"> ' . number_format($product->stock,0,'','') . ' Pcs Left</font></div>';} else {echo '<div class="remaining" style="text-align:center;"><font color="red">Out of Stock</font></div>'; }} add_action('woocommerce_after_shop_loop_item','show_stock', 10);
Try replacing the provided code with:
add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );
function display_variable_product_stock_quantity(){
wc_get_variable_product_stock_quantity( 'echo_html' );
}
I'm creating a webshop with WooCommerce in WordPress, using swedish krona (SEK) as currency. It should be displayed after the price, i.e. "2999 kr", and not "kr2999", but I can't get it to work on the cart and the checkout page, and not cart in the header.
I've found this filter for the cart total price, which is working but only on the total price in checkout:
functions.php
add_filter('woocommerce_cart_totals_order_total_html', 'total_price');
function total_price() {
$price_html = substr(WC()->cart->get_total(),33);
$replace_price = substr_replace($price_html, ' kr', 49);
$value = '<strong>' . $replace_price . '</strong> ';
return $value;
}
How do I do this for every price display in cart and checkout and cart in header? It should be similar to the code above?
I have a pretty weird issue, I hope someone can help me with this.
Here are the major config settings that influence my problem:
Catalog prices in admin panel are shown including tax
Catalog prices in frontend are shown including tax
Items in shopping cart are shown excluding tax (so it's displayed separately near the subtotal).
Everything is working fine so far. The problem comes in a custom ajax mini cart module. I grab the collection of items from the cart, but, since I'm getting the price from the shopping cart item, I get it without tax.
Here is some code to exemplify what I mean. I will assume a 20% tax and a product that has the admin price (including tax) set to 120$, an option that costs 60$ (also including tax). Excluding tax these would be 100$ and 50$. I want to get the price + option + tax => 180$
$quote = Mage::getSingleton('checkout/session')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
echo $item->getPrice(); // 150$ - price excluding tax
echo $item->getPriceInclTax(); // 150$ - price excluding tax
echo $item->getProduct()->getPrice(); // 120$ price including tax, BUT without the customer selected options.
}
PS: The custom option I am talking about is user selected, for example an install checkbox that adds +50$ to the price of the product.
- Get products id, name, price, quantity, etc. present in your cart.
- Get number of items in cart and total quantity in cart.
- Get base total price and grand total price of items in cart.
Get all items information in cart
// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
echo 'ID: '.$item->getProductId().'<br />';
echo 'Name: '.$item->getName().'<br />';
echo 'Sku: '.$item->getSku().'<br />';
echo 'Quantity: '.$item->getQty().'<br />';
echo 'Price: '.$item->getPrice().'<br />';
echo "<br />";
}
Get total items and total quantity in cart
$totalItems = Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
$totalQuantity = Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
Get subtotal and grand total price of cart
$subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
$grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
Have you tried:
$product->getFinalPrice();
// or this?
$product->getPriceModel()->getFinalPrice($qty, $product);
what is the ouput of $item->getOptions()?
Have you tried $item->getData('price')?
How do you apply your custom options? What is the ouput of $item->debug()? Maybe you can find what you need there.
Regards
Simon
I didn't find a solution to my exact problem, but I changed the settings to mimic this exact functionality, and the problem I encountered was no longer there.
First of all, I removed all the taxes on the site, and told magento all the prices are excluding tax (even though they are including tax).
The tax reduction is now made through a promotion applied on a custom group, so for
$tax = 20; // percent
I add a reduction of
(1 - (1 / ($tax / 100 + 1)))*100
// for 20% tax => 16.6667% reduction
// for 24% tax => 19.3548% reduction
with 4 decimals (that's as much as magento accepts). It may have an error of 1 cent from time to time - so if this is not an issue, go for it!
Now the prices all over the website will be shown exactly for the product (because the promotion is applied per cart, not per product).
You can try This :
$grandTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
echo $text .= $this->__(' Total: %s', $this->helper('core')->formatPrice($grandTotal, false));
show the quantity of a cart in my header
if ($parentBlock = $this->getParentBlock()) {
$count = $this->helper('checkout/cart')->getSummaryCount();
if( $count == 1 ) {
echo $text = $this->__('My Cart (%s item)', $count);
} elseif( $count > 0 ) {
echo $text = $this->__('My Cart (%s items)', $count);
} else {
echo $text = $this->__('My Cart (0 items)');
}
}
show the total price of a cart in my header
$grandTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
echo $text .= $this->__(' Total: %s', $this->helper('core')->formatPrice($grandTotal, false));