[英]How to display the total amount of products and the total amount of configurable products in Magento?
[英]Echo Tax Amount for all products in magento
在magento中,我想以這種方式回應每種產品在特定類別中的稅率:
([{"id":"1","name":"Shirt","price":"45.00","tax":null}]);
我使用以下代碼:
public function ws_products($store_id, $service, $categoryid){
$res=array();
Mage::app()->setCurrentStore($store_id);
$c_id = $categoryid;
$category = new Mage_Catalog_Model_Category();
$category->load($c_id);
$collection = $category->getProductCollection()->addStoreFilter($store_id);
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) {
$res[] = array(
"id" => $_product->getId(),
"name" => $_product->getName(),
"price" => number_format($_product->getPrice(),2),
"tax" => Mage::helper('checkout')->getQuote()->getShippingAddress()->getData('tax_amount'),
);
}
return($res);
}
當我使用時
"tax" => Mage::helper('checkout')->getQuote()->getShippingAddress()->getData('tax_amount'),
我正在將稅項設為NULL。
請幫助我。
您必須使用產品模型來納稅-
$product = Mage::getModel('catalog/product')->load($_product->getId());
$taxClassId = $product->getData("tax_class_id");
$taxClasses = Mage::helper("core")->jsonDecode(Mage::helper("tax")->getAllRatesByProductClass());
$taxRate = $taxClasses["value_".$taxClassId];
現在使用$taxRate
作為數組中“ tax”的值。
"tax" => $taxRate,
這是工作代碼,可能對其他人有用。
public function ws_products($store_id, $service, $categoryid)
{
$res=array();
Mage::app()->setCurrentStore($store_id);
$c_id = $categoryid;
$category = new Mage_Catalog_Model_Category();
$category->load($c_id);
$collection = $category->getProductCollection()->addStoreFilter($store_id);
$collection->addAttributeToSelect('*');
foreach ($collection as $_product)
{
$product = Mage::getModel('catalog/product')->load($_product->getId());
$taxClassId = $product->getData("tax_class_id");
$taxClasses = Mage::helper("core")->jsonDecode(Mage::helper("tax"->getAllRatesByProductClass());
$taxRate = $taxClasses["value_".$taxClassId];
$a = (($taxRate)/100) * ($_product->getPrice());
$res[] = array(
"id" => $_product->getId(),
"name" => $_product->getName(),
"imageurl" => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'/media/catalog/product'.$_product->getImage(),
"sku" => $_product->getSku(),
"spclprice" => number_format($_product->getSpecialprice(),2),
"price" => number_format($_product->getPrice(),2),
"tax" => number_format($a,2),
"created_date" => $_product->getCreatedAt(),
"is_in_stock" => $_product->getStockItem()->getIsInStock(),
"stock_quantity" => Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId())->getQty()
);
}
return($res);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.