Magento消除儿童产品以获得适用的价格

时间:2013-06-09 15:27:41

标签: php magento

我有一个脚本可以动态检索我编写的可配置产品的价格,现在我要返回并确保它考虑到可能存在的任何可配置属性。以下是通过ajax调用的,我已将其调试到循环的最中心部分(因此所有magento方法都正常工作)。它应该删除与属性不匹配的子项。最终目标是让一个孩子离开,并回应价格。每个孩子都有一个独特的价格(我知道)。你能明白为什么现在不行吗?

<?php
require_once('/var/www/Staging/public_html/app/Mage.php');
umask(0);
Mage::app(); 

$product_id = $_POST['productID'];
$optionSelectionArray = $_POST['optionSelectionArray']; //the ids of configurable options selected


//var_dump($optionSelectionArray);

$product =  Mage::getModel('catalog/product')->load($product_id); // no need to use the _ here, it's not protected/private; additonally Mage::registry won't work because you're technically not on a product detail page
$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product); 

//Gather all attribute ids for given product    
$availAttrs=[];
foreach($attributes as $attribute){ 
        $availAttrs[] = $attribute->getAttributeId();
}

foreach($availAttrs as $attr){//cycle available attributes
        foreach ($optionSelectionArray as $key => $value) {//cycle option for key
            if ($attr == $key){//if available attribute and key of optionSelectionArray match
                for($i=0; $i<sizeof($childProducts); $i++){//for each child in children
                    //get label of attribute code
                    foreach($attributes as $attribute){ 
                        if ($attr == $attribute->getAttributeId()){
                            $attrName = strtolower($attribute->getLabel());//get code name from attribute id
                        }//end if attribute matches attribute id
                    }//end attributes cycle

                    //CAITLIN LEFT OFF echoing the next line here to see if it is doing what it should

                    if($childProducts[$i]->getData($attrName) != $value){
                        array_splice($childProducts, $i, 1);
                    }//end if attribute values match
                    else{
                        echo("This is a match! : ");
                        echo('The vendor is ' . $childProducts[$i]->getData('vendor'). ";");
                        echo (" The price is " . $childProducts[$i]->getPrice());   
                    }
                }//end childproducts loop
            }//end if
        }//end foreach optionSelectionArray
    }//end foreach availattrs

//Echo final price $childProducts[0]
?>

1 个答案:

答案 0 :(得分:0)

我在循环周围放了以下内容,这确保我删除了所有不匹配的产品。

while(sizeof($childProducts)>1){
//Loops
}