php Magento 获得购物车单品价格包括。税
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15139697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento get cart single item price incl. tax
提问by Vlad Preda
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.
到目前为止一切正常。问题出在自定义 ajax 迷你购物车模块中。我从购物车中获取了一系列商品,但是,由于我是从购物车商品中获取价格的,因此我是免税的。
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$
这是一些代码来举例说明我的意思。我将假设一个20% 的税和一个将管理价格(含税)设置为120 美元的产品,一个成本为60 美元(也含税)的选项。不含税这些将是100$和50$。我想得到价格 + 选项 + 税 => 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.
PS:我所说的自定义选项是用户选择的,例如一个安装复选框,它会为产品的价格增加 50 美元。
采纳答案by Vlad Preda
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).
首先,我删除了网站上的所有税费,并告诉 magento 所有的价格都是不含税的(即使它们是含税的)。
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!
有 4 位小数(这与 magento 接受的一样多)。它可能不时有 1 美分的错误 - 所以如果这不是问题,那就去吧!
Now the prices all over the website will be shown exactly for the product (because the promotion is applied per cart, not per product).
现在网站上的价格将完全显示为产品(因为促销适用于每个购物车,而不是每个产品)。
回答by Gaurang
- 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();
回答by Andrew
Have you tried:
你有没有尝试过:
$product->getFinalPrice();
// or this?
$product->getPriceModel()->getFinalPrice($qty, $product);
回答by simonthesorcerer
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.
的输出是$item->getOptions()什么?你试过$item->getData('price')吗?您如何应用自定义选项?的输出是$item->debug()什么?也许你可以在那里找到你需要的东西。
Regards Simon
问候西蒙
回答by punit
You can try This :
你可以试试这个:
$grandTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
echo $text .= $this->__(' Total: %s', $this->helper('core')->formatPrice($grandTotal, false));
回答by Gaurang
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));

