dousong5161 2013-11-03 15:16
浏览 34
已采纳

过滤PHP中的数组

im doing some work with the Magento SOAP API and i am trying to create a valid list of orders. I have got the list but it shows both the configurable products as well as simple product giving me duplicating like the image below: item list

The first listing of the same product is the configurable product, the second is the simple product. What i need to do is filter out the simple products so they dont shown in the list or get stored. This sounds simple but there is one problem that im struggling to get my head around how to solve, and that is products like the Nokia in the image above are standard simple products and have no configurable properties and they also need to be shown!

So basically i need to get rid of the simple products relating to the configurable products but none others.

Here is my loop and array push:

foreach($sales_order_info->items as $i){
                $nextItem = new item();
                $nextItem->set_order_id($i->order_id);
                $nextItem->set_sku($i->sku);
                $nextItem->set_name($i->name);
                $nextItem->set_qty_ordered($i->qty_ordered);
                $nextItem->set_price($i->price);
                $nextItem->set_row_total($i->row_total);

                array_push($this->items, $nextItem);
            }

Thanks very much

  • 写回答

1条回答 默认 最新

  • doudai8783 2013-11-03 16:40
    关注

    If you need to filter simple product of a configurable product:

     if ($product->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {              
        if ( $product->getParentItemId()) {
                            $parent_product_type = Mage::getModel('sales/order_item')->load($product->getParentItemId())->getProductType();
                            //if Parent product type is configurable don't output its simple product
                            if ($parent_product_type == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
                                continue; 
                            }
                        }
    }
    

    If you don't want to extend Magento API and write your own custom calls you can try the following.

    The second option is to try using (not sure how you load it, with what Call):

    $i->type
    

    and then filter the array with PHP (i.e. if there are 2 elements with the same name and quantity remove the one that has type==simple)

    Or the third option would be (if you don't have any free products) just check if price is equal to zero, as simple products which belong to configurable has price = 0.0000

    if ($i->price==0.0000) {
        continue;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?