マゼントは、該当する価格を取得するために子供製品を排除する[閉じ
-
16-10-2019 - |
質問
私が書いた構成可能な製品の価格を動的に取得するスクリプトがあり、今、私は戻って、それが存在する可能性のある構成可能な属性を考慮していることを確認しています。以下はAjaxを介して呼び出され、ループの最も中心部までデバッグしました(したがって、すべてのMagentoメソッドが機能しています)。属性と一致しない子供を削除する必要があります。最終目標は、1人の子供を残し、価格をエコーすることです。各子供にはユニークな価格が含まれています(これは私が知っています)。なぜこれが今機能していないのかわかりますか?
<?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]
?>
解決
私はループの周りに以下を置き、それにより、一致しなかったすべての製品を排除することを確認しました。
while(sizeof($childProducts)>1){
//Loops
}
所属していません magento.stackexchange