在 Magento 中计算订单中每种产品的价格

发布于 2024-10-07 02:29:47 字数 257 浏览 2 评论 0原文

我想计算订单中所有产品的基本价格。每件(1 件)的价格应包含任何折扣/奖金,但不包含税费。所有产品价格的总和乘以数量+税金应该与 $order->getGrandTotal() 完全相同。

我已经设法获得包括运费在内的单独价格,只有很小的精度误差。当然,在处理货币时这是不可接受的。另外,我还没有考虑捆绑产品等。

所以我请求你帮助我,我需要进行与 Magento 完全相同的计算,并且仍然得到我需要的值(每个产品/运输有折扣但不含税)。

提前致谢

I want to calculate the base price of all products in an order. The price for each (1 quantity) should include any discount/bonuses but NOT tax. The total of all product prices times their quantity + tax should be exactly the same as $order->getGrandTotal().

I’ve managed to get the individual prices including shipping fee with only a small precision error. Of course, this is not acceptable when dealing with currency. Also I’ve not taken into account bundled products and such.

So I ask you to help me, I need to make the exact same calculation as Magento do, and still get out the values that I need (each product/shipping with discounts but without tax).

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2024-10-14 02:29:47

我已经这样做了,它为我提供了我所需要的信息。但我不确定这是否是正确的方法。另外,我的 $total 似乎与 $grand_total 不同,有时有几个小数,例如 0.005 或类似的。

$store = Mage::app()->getStore($order->getStoreId());

$customer = Mage::getModel('customer/customer')
    ->load($order->getCustomerId());

$tax_calc = Mage::getSingleton('tax/calculation');

$tax_rate_req = $tax_calc->getRateRequest(
    $order->getShippingAddress(), 
    $order->getBillingAddress(), 
    $customer->getTaxClassId(), 
    $store);

$args = array();
$total = 0;

// Calculate price of each item in the order
foreach($order->getAllVisibleItems() as $item)
{
    $product = Mage::getModel('catalog/product')
        ->load($item->getProductId());

    $children = $item->getChildrenItems();

    if(count($children) && ($product->getData('price_type') != 1))
    {
        foreach($children as $child)
        {
            $product = Mage::getModel('catalog/product')
                ->load($child->getProductId());

            /* If tax_percent is not set?
            Mage::getSingleton('tax/calculation')->getRate(
                $tax_rate_req->setProductClassId($product->getTaxClassId()))
            */
            $tax_mod = (float)$child->getData('tax_percent');
            $tax_mod /= 100;

            $qty = (float)$child->getData('qty_ordered');

            $price = (float)$child->getData('row_total_incl_tax');
            $price -= (float)$child->getData('discount_amount');

            $base_price = (($price / (1 + $tax_mod)) / $qty);
            $base_price = $store->roundPrice($base_price);

            $total += (($base_price * (1 + $tax_mod)) * $qty);

            $args[] = array
                (
                    'name'          => $product->getData('name'),
                    'sku'           => $child->getData('sku'),
                    'tax_mod'       => $tax_mod,
                    'qty'           => $qty,
                    'price'         => $price,
                    'base_price'    => $base_price
                );
        }
    }
    else
    {
        /* If tax_percent is not set?
        Mage::getSingleton('tax/calculation')->getRate(
            $tax_rate_req->setProductClassId($product->getTaxClassId()))
        */
        $tax_mod = (float)$item->getData('tax_percent');
        $tax_mod /= 100;

        $qty = (float)$item->getData('qty_ordered');

        $price = (float)$item->getData('row_total_incl_tax');
        $price -= (float)$item->getData('discount_amount');

        $base_price = (($price / (1 + $tax_mod)) / $qty);
        $base_price = $store->roundPrice($base_price);

        $total += (($base_price * (1 + $tax_mod)) * $qty);

        $args[] = array
            (
                'name'          => $product->getData('name'),
                'sku'           => $item->getData('sku'),
                'tax_mod'       => $tax_mod,
                'qty'           => $qty,
                'price'         => $price,
                'base_price'    => $base_price
            );
    }
}

// Calculate price for shipping
if(($price = (float)$order->getData('shipping_incl_tax')) > 0)
{
    $tax_mod = $tax_calc->getRate($tax_rate_req->setProductClassId(
        Mage::getStoreConfig('tax/classes/shipping_tax_class')));
    $tax_mod /= 100;

    $price -= (float)$order->getData('shipping_discount_amount');

    $base_price = ($price / (1 + $tax_mod));

    $base_price = $store->roundPrice($base_price);

    $total += ($base_price * (1 + $tax_mod));

    $args[] = array
        (
            'name'          => $order->getData('shipping_description'),
            'sku'           => $order->getData('shipping_method'),
            'tax_mod'       => $tax_mod,
            'qty'           => 1,
            'price'         => $price,
            'base_price'    => $base_price
        );
}

$total = $store->roundPrice($total);

echo('<pre>');
print_r($args);
//print_r($order->getData());
echo('</pre>');

$grand_total = (float)$order->getData('grand_total');
//$grand_total = $store->roundPrice($grand_total);

echo('<p><strong>My total</strong>: ' . $total . '</p>');
echo('<p><strong>Grand total</strong>: ' . $grand_total . '</p>');

exit;

I've done this, and it gives me exactly the information I need. But I'm not sure if this is the right way to do it. Also my $total seem to be different from $grand_total with a few decimals such as 0.005 or similar sometimes.

$store = Mage::app()->getStore($order->getStoreId());

$customer = Mage::getModel('customer/customer')
    ->load($order->getCustomerId());

$tax_calc = Mage::getSingleton('tax/calculation');

$tax_rate_req = $tax_calc->getRateRequest(
    $order->getShippingAddress(), 
    $order->getBillingAddress(), 
    $customer->getTaxClassId(), 
    $store);

$args = array();
$total = 0;

// Calculate price of each item in the order
foreach($order->getAllVisibleItems() as $item)
{
    $product = Mage::getModel('catalog/product')
        ->load($item->getProductId());

    $children = $item->getChildrenItems();

    if(count($children) && ($product->getData('price_type') != 1))
    {
        foreach($children as $child)
        {
            $product = Mage::getModel('catalog/product')
                ->load($child->getProductId());

            /* If tax_percent is not set?
            Mage::getSingleton('tax/calculation')->getRate(
                $tax_rate_req->setProductClassId($product->getTaxClassId()))
            */
            $tax_mod = (float)$child->getData('tax_percent');
            $tax_mod /= 100;

            $qty = (float)$child->getData('qty_ordered');

            $price = (float)$child->getData('row_total_incl_tax');
            $price -= (float)$child->getData('discount_amount');

            $base_price = (($price / (1 + $tax_mod)) / $qty);
            $base_price = $store->roundPrice($base_price);

            $total += (($base_price * (1 + $tax_mod)) * $qty);

            $args[] = array
                (
                    'name'          => $product->getData('name'),
                    'sku'           => $child->getData('sku'),
                    'tax_mod'       => $tax_mod,
                    'qty'           => $qty,
                    'price'         => $price,
                    'base_price'    => $base_price
                );
        }
    }
    else
    {
        /* If tax_percent is not set?
        Mage::getSingleton('tax/calculation')->getRate(
            $tax_rate_req->setProductClassId($product->getTaxClassId()))
        */
        $tax_mod = (float)$item->getData('tax_percent');
        $tax_mod /= 100;

        $qty = (float)$item->getData('qty_ordered');

        $price = (float)$item->getData('row_total_incl_tax');
        $price -= (float)$item->getData('discount_amount');

        $base_price = (($price / (1 + $tax_mod)) / $qty);
        $base_price = $store->roundPrice($base_price);

        $total += (($base_price * (1 + $tax_mod)) * $qty);

        $args[] = array
            (
                'name'          => $product->getData('name'),
                'sku'           => $item->getData('sku'),
                'tax_mod'       => $tax_mod,
                'qty'           => $qty,
                'price'         => $price,
                'base_price'    => $base_price
            );
    }
}

// Calculate price for shipping
if(($price = (float)$order->getData('shipping_incl_tax')) > 0)
{
    $tax_mod = $tax_calc->getRate($tax_rate_req->setProductClassId(
        Mage::getStoreConfig('tax/classes/shipping_tax_class')));
    $tax_mod /= 100;

    $price -= (float)$order->getData('shipping_discount_amount');

    $base_price = ($price / (1 + $tax_mod));

    $base_price = $store->roundPrice($base_price);

    $total += ($base_price * (1 + $tax_mod));

    $args[] = array
        (
            'name'          => $order->getData('shipping_description'),
            'sku'           => $order->getData('shipping_method'),
            'tax_mod'       => $tax_mod,
            'qty'           => 1,
            'price'         => $price,
            'base_price'    => $base_price
        );
}

$total = $store->roundPrice($total);

echo('<pre>');
print_r($args);
//print_r($order->getData());
echo('</pre>');

$grand_total = (float)$order->getData('grand_total');
//$grand_total = $store->roundPrice($grand_total);

echo('<p><strong>My total</strong>: ' . $total . '</p>');
echo('<p><strong>Grand total</strong>: ' . $grand_total . '</p>');

exit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文