1

我需要收集给定产品的所有可用属性,然后用它们创建一个多维数组。希望您可以创建一个多于二维的多维数组?生成的数组声明应如下所示:

$simpleArray[$child->getVendor()][$child->getColor()]=$child->getPrice();

首先,我收集所有属性,然后将它们添加到一个字符串中,稍后我可以调用每个属性:

$_product    = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());


//Gather all attribute labels for given product

    foreach($_attributes as $_attribute){ 
            $attributeString .= '[$child -> get' . ucfirst($_attribute->getLabel()) . '()]';
    }

然后我试图将该字符串附加到数组中以声明它:

foreach($childProducts as $child) { //cycle through simple products to find applicable
    //CAITLIN you are going to need way to search for other attributes, GET list of attributes
    $simpleArray. $attributeString =$child->getPrice();
}               
Mage::log('The attributeString is '. $simpleArray. $attributeString, null, 'caitlin.log');  //This is logging as "The attributeString is Array74"

有什么建议么?

4

1 回答 1

0

You'll need to use recursion to do what you're requesting without knowing the attribute names while writing the code.

This will loop through and provide all of the child product prices, in a multi dimensional array based on the configurable attributes. It assumes that $_product is the current product.

$attrs  = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
$map = array();
foreach($attrs as $attr) {
    $map[] = $attr['attribute_code'];
}
$childPricing = array();
$childProducts = $_product->getTypeInstance()->getUsedProducts(); 
foreach($childProducts as $_child) {
    // not all of the child's attributes are accessible, unless we properly load the full product
    $_child = Mage::getModel('catalog/product')->load($_child->getId());
    $topLevel = array($child->getData($map[sizeof($map)]) => $_child->getPrice());
    array_pop($map);
    $childProducts = array_merge($childProducts,$this->workThroughAttrMap($map,$_child,$topLevel));
}
//print_r childProducts to test, later do whatever you were originally planning with it.

In the same controller include this:

protected function workThroughAttrMap(&$map,$child,$topLevel) {
    $topLevel = array($child->getData($map[sizeof($map)]) => $topLevel);
    array_pop($map);
    if(sizeof($map) > 0) return workThroughAttrMap($map,$child,$topLevel);
    else return $topLevel;
}

I haven't tested this code so there may be a few minor bugs.

There are a few things you could do to make the code a bit cleaner, such as moving the first $topLevel code into the function, making that an optional parameter and initializing it with the price when it doesn't exist. I also haven't included any error checking (if the product isn't configurable, the child product doesn't have its price set, etc).

于 2013-06-06T01:31:03.490 回答