获取可配置产品选项的价格


9

我需要以Magento 1.7的价格出口所有产品。

对于简单产品,这没问题,但是对于可配置产品,我有以下问题:导出的价格是为相关简单产品设置的价格!如您所知,Magento会忽略此价格,而是使用可配置产品的价格以及所选选项的调整项。

我可以获得母产品的价格,但是如何根据所选的选项计算差异?

我的代码如下所示:

foreach($products as $p)
   {
    $price = $p->getPrice();
            // I save it somewhere

    // check if the item is sold in second shop
    if (in_array($otherShopId, $p->getStoreIds()))
     {
      $otherConfProd = Mage::getModel('catalog/product')->setStoreId($otherShopId)->load($p->getId());
      $otherPrice = $b2cConfProd->getPrice();
      // I save it somewhere
      unset($otherPrice);
     }

    if ($p->getTypeId() == "configurable"):
      $_associatedProducts = $p->getTypeInstance()->getUsedProducts();
      if (count($_associatedProducts))
       {
        foreach($_associatedProducts as $prod)
         {
                            $p->getPrice(); //WRONG PRICE!!
                            // I save it somewhere
                        $size $prod->getAttributeText('size');
                        // I save it somewhere

          if (in_array($otherShopId, $prod->getStoreIds()))
           {
            $otherProd = Mage::getModel('catalog/product')->setStoreId($otherShopId)->load($prod->getId());

            $otherPrice = $otherProd->getPrice(); //WRONG PRICE!!
                            // I save it somewhere
            unset($otherPrice);
            $otherProd->clearInstance();
            unset($otherProd);
           }
         }
                     if(isset($otherConfProd)) {
                         $otherConfProd->clearInstance();
                            unset($otherConfProd);
                        }
       }

      unset($_associatedProducts);
    endif;
  }

Answers:


13

这是如何获得简单产品的价格。该示例是针对单个可配置产品的,但您可以将其集成到循环中。
性能可能存在问题,因为存在很多foreach循环,但至少您有一个起点。您可以稍后进行优化。

//the configurable product id
$productId = 126; 
//load the product - this may not be needed if you get the product from a collection with the prices loaded.
$product = Mage::getModel('catalog/product')->load($productId); 
//get all configurable attributes
$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
//array to keep the price differences for each attribute value
$pricesByAttributeValues = array();
//base price of the configurable product 
$basePrice = $product->getFinalPrice();
//loop through the attributes and get the price adjustments specified in the configurable product admin page
foreach ($attributes as $attribute){
    $prices = $attribute->getPrices();
    foreach ($prices as $price){
        if ($price['is_percent']){ //if the price is specified in percents
            $pricesByAttributeValues[$price['value_index']] = (float)$price['pricing_value'] * $basePrice / 100;
        }
        else { //if the price is absolute value
            $pricesByAttributeValues[$price['value_index']] = (float)$price['pricing_value'];
        }
    }
}

//get all simple products
$simple = $product->getTypeInstance()->getUsedProducts();
//loop through the products
foreach ($simple as $sProduct){
    $totalPrice = $basePrice;
    //loop through the configurable attributes
    foreach ($attributes as $attribute){
        //get the value for a specific attribute for a simple product
        $value = $sProduct->getData($attribute->getProductAttribute()->getAttributeCode());
        //add the price adjustment to the total price of the simple product
        if (isset($pricesByAttributeValues[$value])){
            $totalPrice += $pricesByAttributeValues[$value];
        }
    }
    //in $totalPrice you should have now the price of the simple product
    //do what you want/need with it
}

上面的代码在CE-1.7.0.2上使用Magento样本数据进行了1.6.0.0的测试。
我在产品Zolof The Rock and Roll Destroyer:LOL Cat T恤上进行了测试,并且可以正常工作。在通过Size和配置产品后,我得到的结果与前端中看到的价格相同。Color


3

是否可能需要在下面的代码中更改$p$prod

 foreach($_associatedProducts as $prod)
         {
                            $p->getPrice(); //WRONG PRICE!!

2

这是我的方法:

$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('catalog/product_view_type_configurable');
$pricesConfig = Mage::helper('core')->jsonDecode($block->getJsonConfig());

此外,您可以将其转换为Varien_Object:

$pricesConfigVarien = new Varien_Object($pricesConfig);

因此,基本上我使用的是用于计算magento核心中可配置产品页面价格的方法。


0

不知道这是否有帮助,但是如果您将此代码添加到configurable.phtml页面,它应该吐出带有每个选项及其标签价格的可配置产品超级属性。

   $json =  json_decode($this->getJsonConfig() ,true);


    foreach ($json as $js){
        foreach($js as $j){

      echo "<br>";     print_r($j['label']); echo '<br/>';

            foreach($j['options'] as $k){
                echo '<br/>';     print_r($k['label']); echo '<br/>';
                print_r($k['price']); echo '<br/>';
            }
        }
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.