我需要收集给定产品的所有可用属性,然后用它们创建一个多维数组。希望你能创建一个二维以上的多维数组?生成的数组声明应该像这样:
$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"
有什么建议吗?
在编写代码时,您需要使用递归来完成您所请求的操作,而无需知道属性名称。
这将在基于可配置属性的多维数组中循环并提供所有子产品价格。它假设$_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.
在同一个控制器中包括:
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;
}
我还没有测试过这段代码,所以可能会有一些小错误。
您可以做一些事情来使代码更简洁,例如将第一个$topLevel代码移动到函数中,使其成为可选参数,并在它不存在时使用price初始化它。我也没有包括任何错误检查(如果产品是不可配置的,子产品没有它的价格设置,等等)。