doupingzhi9674 2013-06-06 00:50
浏览 29

Magento将属性收集为多维数组键

I need to gather all of the available attributes for the given product and then create a multidimensional array with them. Hopefully you can create a multidimensional array with more than two dimensions? The resulting array declarations should look like this:

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

First I'm gathering all the attributes then adding them to a string where I can call each one later:

$_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()) . '()]';
    }

Then I'm attempting to append that string to the array to declare it:

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"

Any suggestions?

  • 写回答

1条回答 默认 最新

  • douhuireng4407 2013-06-06 01:31
    关注

    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).

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序