如何在Magento中获得捆绑产品价格?

时间:2012-07-03 02:51:58

标签: magento

我有一个像这样的数组的捆绑产品:(在将产品添加到购物车时从params获取)

Array
(
    [product] => 165
    [bundle_option] => Array
    (
        [17] => 47
        [22] => 60
        [16] => 46
        [15] => 42
        [14] => Array
            (
                [0] => 39
            )
    )
)

我怎么能得到这个捆绑产品的价格?

8 个答案:

答案 0 :(得分:6)

这样的事情也应该有效:

public function getDisplayPrice($product) {
    if($product->getFinalPrice()) {
        return $product->getFormatedPrice();
    } else if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
        $optionCol= $product->getTypeInstance(true)
                            ->getOptionsCollection($product);
        $selectionCol= $product->getTypeInstance(true)
                               ->getSelectionsCollection(
            $product->getTypeInstance(true)->getOptionsIds($product),
            $product
        );
        $optionCol->appendSelections($selectionCol);
        $price = $product->getPrice();

        foreach ($optionCol as $option) {
            if($option->required) {
                $selections = $option->getSelections();
                $minPrice = min(array_map(function ($s) {
                                return $s->price;
                            }, $selections));
                if($product->getSpecialPrice() > 0) {
                    $minPrice *= $product->getSpecialPrice()/100;
                }

                $price += round($minPrice,2);
            }  
        }
        return Mage::app()->getStore()->formatPrice($price);
    } else {
        return "";
    }
}

答案 1 :(得分:2)

您可以使用Mage_Bundle_Model_Product_Price的内置静态方法calculatePrice,如下所示:

if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE){

        $pricemodel = Mage::getModel('bundle/product_price');

        $price = $pricemodel::calculatePrice(
            $product->getData('price'),
            $product->getSpecialPrice(),
            $product->getSpecialPriceFrom(),
            $product->getSpecialPriceTo(),
            false,
            Mage::app()->getStore()->getWebsite()->getId(),
            Mage::getSingleton('customer/session')->getCustomer()->getGroupId(),
            $product->getId()
        );

        $finalprice = $this->helper('core')->currencyByStore(Mage::helper('tax')->getPrice($product, $price));

    }

答案 2 :(得分:1)

// load product
$product = new Mage_Catalog_Model_Product();
$product->load(165);
$priceModel = $product->getPriceModel();

// get options
$block = Mage::getSingleton('core/layout')->createBlock('bundle/catalog_product_view_type_bundle');
$options = $block->setProduct($product)->getOptions();

$price = 0;
foreach ($options as $option) {
  $selection = $option->getDefaultSelection();

  if ($selection === null) {
    continue;
  }

  $price += $priceModel->getSelectionPreFinalPrice($product, $selection, $selection->getSelectionQty());
}

或者,您可以使用我的magento模块:使用magento 1.7测试https://github.com/head82/KH_ExtendedBundlePrice

答案 3 :(得分:1)

您可以在Mage_Bundle_Product_Price类中使用getMinimalPrice()和getMaximalPrice()函数,这些函数是专门为捆绑产品编写的。

答案 4 :(得分:1)

@Kevin,干得好。我个人需要稍作修改。在Magento 1.7.0.2中,函数getSelectionPreFinalPrice()实际调用getSelectionPrice()调用getSelectionFinalTotalPrice() - 但在最后一部分中,价格是用最终价格计算的(所以包括层次定价和特殊价格)而不是原价。

我应用以下代码段来获取原始价格(没有等级定价且没有特价):

$_normalPrice = 0;
$_options = $_priceModel->getOptions($_product);
foreach($_options as $_option) {
    $_selection = $_option->getDefaultSelection();
    if ($_selection === null) continue;
    $_normalPrice = $_normalPrice + $_selection->getPrice();
}

答案 5 :(得分:1)

以下是magento方式:

$_product     = $this->getProduct();
$_priceModel  = $_product->getPriceModel();

list($_minimalPriceTax, $_maximalPriceTax) = $_priceModel->getTotalPrices($_product, null, null, false);
list($_minimalPriceInclTax, $_maximalPriceInclTax) = $_priceModel->getTotalPrices($_product, null, true, false);

假设$this->getProduct()返回Catalog/Product模型。 兼具固定和动态价格类型,甚至兼容可配置捆绑包扩展。取自design / base / default / template / bundle / catalog / product / price.phtml

答案 6 :(得分:0)

虽然我确定你几年前已经弄清楚了你需要什么,但我不太确定接受答案的地方你会把你的参数放在哪里。

我发现您可以像使用任何其他产品一样使用getFinalPrice()获取捆绑产品的价格,但您可以使用目录/产品帮助程序设置所选选项。

    $_product = Mage::getModel('catalog/product')->load($this->getRequest()->getPost()['product'];
    $productHelper = $this->helper('catalog/product');
    //getpost() contains the array you mentioned when you click add to cart
    $_configuredProducts = $_product->getTypeInstance(true)->processConfiguration(new Varien_Object($this->getRequest()->getPost()), $_product,Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL ); 
    echo $_product->getFinalPrice();

答案 7 :(得分:0)

我用自己的方法解决了它,不确定它是否可以接受。发布帖子值和产品ID,模型会返回价格。

$bundle_option = Mage::app ()->getRequest ()->getParam('bundle_option');
$bundle_option_array = call_user_func_array('array_merge', $bundle_option);
$price =  Mage::Helper('airhotels/bundle')->getBundlePrice($productid,$bundle_option_array);

我的帮助文件是

public function getBundlePrice($productId,$bundle_option_array) {
$product = new Mage_Catalog_Model_Product();
$product->load($productId);
$price=0;
$selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection($product->getTypeInstance(true)->getOptionsIds($product), $product);
foreach($selectionCollection as $option) 
{
if (in_array($option->getSelectionId(), $bundle_option_array)){
    $price += $option->price; 
}
}
return $price;
}

概念:我从二维数组(问题)构建了一个一维数组。从帮助函数中我们可以得到捆绑产品的所有选择ID。通过匹配(使用in_array),我们可以计算出我们自定义产品的价格。

相关问题