Question

J'ai un script qui récupère dynamiquement prix des produits configurables que je vous ai écrit, et maintenant je vais revenir et faire en sorte qu'il prend en compte tous les attributs configurables qui peuvent y être. Ce qui suit est appelé via ajax et je déboguée vers le bas à la partie la plus centrale de la boucle (donc toutes les méthodes de magento travaillent). Il convient de retirer les enfants qui ne correspondent pas aux attributs. L'objectif final est d'avoir un enfant à gauche, et l'écho du prix. Chaque enfant contient un tarif unique (ce savoir I). Pouvez-vous voir pourquoi cela ne fonctionne pas en ce moment?

<?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]
?>
Était-ce utile?

La solution

Je mets les éléments suivants autour des boucles et qui fait en sorte que tous les produits que j'éliminé qui ne correspondent pas.

while(sizeof($childProducts)>1){
//Loops
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top