Price and VAT in opencart 2.x - php

I m using opencart 2.x for development and i am really stuck in cart and invoices. My scenario is that,
I want to show VAT only while sending invoice to the customer and VAT shall not be visible in Cart but total in cart shall be including VAT.
What I want is to not display the tax in cart and if I disable it in the admin screen it does not count up correctly and is not appearinf in Invoice as well. So it must be hidden in the code.
Presentation in Cart shall be
Subtotal // This subtotal shall inclusive of VAT here
Shipping
Total // This total shall inclusive of VAT here
Presentation in Invoice shall be
Subtotal // This subtotal shall exclusive of VAT here
Shipping
VAT(1%) // VAT is shown here
Total // This total shall inclusive of VAT here
Currently, i have presenation in cart and invoice same as of invoice shown above.
I have enabled Taxes in Extensions > Order Totals but i do not know how to modify the total array in opencart.

You need to replace code :--
catalog/controller/checkout/cart.php
Find Below code :--
foreach ($results as $result)
{
if ($this->config->get($result['code'] . '_status'))
{
$this->load->model('total/' . $result['code']);
$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
}
}
Replace with This Code :--
foreach ($results as $result)
{
if ($this->config->get($result['code'] . '_status'))
{
$this->load->model('total/' . $result['code']);
if($result['code'] != "tax")
{
$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
}
}
}

Related

Get separate Order's tax totals amounts with tax class

I'm making personal a PDF invoice plugin for woocommerce.
Some of our products has tax with Reduced rate (%8) or Standart rate (%18). For example, Product A = Reduced rate (%8), Product B = Standart rate (%18). I can get total of tax amount easily but I want to print with sperate tax total amounts with tax class.
How to get total Reduced rate tax amount of an order? Also Standart rate.
How can I echo them separately?
The Tax class is not registered anywhere in the order data… It's registered in each product and optionally for shipping.
You can simply use the dedicated WC_Abstract_Order method get_tax_totals() (as Woocommerce uses for separated tax rows) and you will have the tax label percentage that is set in each tax line settings.
The Rate code $rate_code is made up of COUNTRY-STATE-NAME-Priority. For example: GB-VAT-1 or US-AL-TAX-1.
The code to display separated tax rows:
// Get the WC_Order instance Object from the Order ID (if needed)
$order = wc_get_order($order_id);
// Output the tax rows in a table
echo '<table>';
foreach ( $order->get_tax_totals() as $rate_code => $tax ) {
$tax_rate_id = $tax->rate_id;
$tax_label = $tax->label;
$tax_amount = $tax->amount;
$tax_f_amount = $tax->formatted_amount;
$compound = $tax->is_compound;
echo '<tr><td>' . $tax_label . ': </td><td>' . $tax_f_amount . '</td></tr>';
}
echo '</table>';
If you want to display something like Reduced rate (%8) or Standart rate (%18), you will have to customize the "Tax name" in the Tax settings for each tax line in each Tax rate (But it will be displayed everywhere and not only in your custom PDF plugin).
Additionally, the Tax class is just for settings purpose and for admin view.

Woocommerce - Change price display in cart and checkout page

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?

Building a custom module using PHP

I am currently building a module for an ecommerce website (Lemonstand). My shop sells both normal books and ebooks, and charge different rates of for shipping based on the subtotal of all items in the cart ($7 shipping cost for subtotal of less than $20, $14 shipping cost for between $20 and $50..etc). Currently the website just uses the subtotal of all items in the cart to determine which rate should be applied. The problem is, I want only the subtotal of normal books to be used for calculating shipping, because obviously ebooks don't need shipping.
Lemonstand platform has some built_in functions that I'm using to help with this. update_shipping_quote function is called just before shipping cost is calculated. It will be used to change the subtotal of cart items so that shipping cost can be calculated using the subtotal of non-ebooks instead.
Here is the API documentation for the function: https://v1.lemonstand.com/api/event/shop:onupdateshippingquote/
Here is the bit of code that's giving me trouble. I want to know if the value I get at the end ($non_ebook_subtotal) actually contains the value it's supposed to.
If anyone can come up with a better method for doing what I'm trying to do, please share.
//$params is an array containing things like individual item price
//Here I get the cart items and put them into var
public function
update_shipping_quote($shipping_option, $params) {
$cart_items = $params['cart_items'];
//find all products that are ebooks
foreach ($cart_items-> items as $item)
{
$product = $item->product;
if($product->product_type->code == 'ebook') {
$isEbook = true;
}
}
//add price of all ebooks into $ebook_subtotal
foreach ($cart_items as $item) {
$product = $item -> product;
if ($isEbook == true) {
$ebook_subtotal = $ebook_subtotal + total_price($product);
}
}
//Calculating the subtotal of only the non-ebook products
$non_ebook_subtotal = $params['total_price'] - $ebook_subtotal;
//This returns the non_ebook_subtotal to be used for calculating shipping cost
return array('total_price' => $non_ebook_subtotal);
}
Thanks
// get all variables needed
$totalprice = $params['total_price'];
$items = $params['cart_items'];
foreach ($items AS $item) {
// if is ebook
if ($item->product->product_type->code == 'ebook') {
// minus price of the item from total
$totalprice -= total_price($item->product);
}
}
return $totalprice;

Magento get cart single item price incl. tax

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));

in Magento, how can i put shipping cost on the product page for a configurable product?

I have this code on my products view.phtml file: I am getting a shipping estimate and basing the lowest cost to a UK address and rendering this on the products page. Rather than having a full blown shipping estimator on the pages.
<?php
if($_product->isSaleable())
{
$quote = Mage::getModel('sales/quote');
$quote->getShippingAddress()->setCountryId('GB');
$quote->addProduct($_product);
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$rates = $quote->getShippingAddress()->getShippingRatesCollection();
$minPrice = PHP_INT_MAX;
foreach ($rates as $rate) {
$minPrice = min($minPrice, $rate->getPrice());
}
if ($minPrice == 0) {
echo ('This item qualifies for FREE shipping');
}
elseif ($minPrice < PHP_INT_MAX) {
echo ('Shipping to UK from £' . money_format('%i', $rate->getPrice()) . "\n");
}
}
?>
Which works fine for simple products. however on configurable products, it always displays free shipping! How can get around this?
This is just a guess, but a configurable product is just a container to organize a bunch of simple products. When you add a product to the cart (for real), there is a simple product in the cart in addition to the configurable one. I'm guessing that this second product provides the weight and information for shipping, etc.
That would imply that, to get your shipping estimate, you'll need to specify one of the simple products underneath the configurable (either directly, or by passing some options and pretending to have "selected" a product) in order to see your shipping estimates.

Categories