我需要收集给定产品的所有可用属性,然后使用它们创建一个多维数组。希望您可以创建具有两个以上维度的多维数组?生成的数组声明应如下所示:
$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"
有什么建议吗?
答案 0 :(得分:0)
在编写代码时,您需要使用递归来执行您要求的操作而不知道属性名称。
这将循环并提供基于可配置属性的多维数组中的所有子产品价格。它假定$ _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代码移动到函数中,使其成为可选参数,并在它不存在时以价格初始化它。我还没有包含任何错误检查(如果产品不可配置,儿童产品没有设定价格等)。