magento中所有产品的回声税金额

时间:2014-08-06 05:25:44

标签: php 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'),

我的税收为空。

请帮助我。

2 个答案:

答案 0 :(得分:0)

您必须使用产品型号获得税 -

$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,

答案 1 :(得分:0)

这是工作代码,可能对其他人有帮助。

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