如何获得捆绑选项SKU?

时间:2013-12-21 06:42:41

标签: magento

<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");

$orderNumber = 260038;  

$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);

foreach ($order->getAllItems() as $item){ 

    $productOptions = $item->getProductOptions();   
    echo $product_id = $item->product_id;

    $_product=Mage::getModel('catalog/product')->load($product_id);
    if ($_product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {

        if (isset($productOptions['bundle_options']))
        {               
            foreach ($productOptions['bundle_options'] as $productOption)
            {               
                echo $value = $productOption['value'][0]['title']; 
                echo ' || ';                
                echo $value = $productOption['value'][0]['qty'];                
                echo ' || '; 
                echo $value = $productOption['value'][0]['price'];  
                echo "<br>";                
            }  
        }           
    }       
}

enter image description here

我能够获得产品的标题,数量和价格,我也想获得产品SKU。

3 个答案:

答案 0 :(得分:6)

捆绑产品可以有选项,选项可以有选择。这是“双层”结构。如果您只想获得没有选项的所有选择,您可以使用以下内容:

$selections = $product->getTypeInstance(true)
                      ->getSelectionsCollection($product->getTypeInstance(true)
                      ->getOptionsIds($product), $product);
foreach($selections as $selection){ 
   echo $selection->getSku();
}

但如果您想获得有关选项及其选择的完整信息,请使用下一种方式(基于您的示例):

<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");

$orderNumber = 260038;  

$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
$store_id = $order->getStoreId();

foreach ($order->getAllItems() as $item){ 
    $product = Mage::getModel('catalog/product')->setStoreId($store_id)->load($item->product_id);
    $options = Mage::getModel('bundle/option')->getResourceCollection()
                                              ->setProductIdFilter($item->product_id)
                                              ->setPositionOrder(); 
    $options->joinValues($store_id);

    $selections = $product->getTypeInstance(true)
                          ->getSelectionsCollection($product->getTypeInstance(true)
                          ->getOptionsIds($product), $product);

    foreach ($options->getItems() as $option) {
        $option_id = $option->getId();
        echo 'Option: ' . $option->getTitle() . ' [id: ' . $option_id . ']<br />'; 

        foreach($selections as $selection){ 
            if($option_id == $selection->getOptionId()){
                $selection_id         = $selection->getId();
                $selection_name       = $selection->getName();
                $selection_qty        = $selection->getSelectionQty();
                $selection_sku        = $selection->getSku();
                $selection_product_id = $selection->getProductId();
                $selection_weight     = $selection->getWeight();
                $selection_price      = $selection->getPrice();

                $data = 'Selection Name: ' . $selection_name;
                $data .= ', SKU: ' . $selection_sku;
                $data .= ', Qty: ' . $selection_qty;
                $data .= ', ID: ' . $selection_id;
                $data .= ', Product ID: ' . $selection_product_id;
                $data .= ', Weight: ' . $selection_weight;
                $data .= ', Price: ' . $selection_price;

                echo $data . '<br />';
            }
        }
    }
}

?>

答案 1 :(得分:1)

我们走了,

通过选定的选项ID获取产品sku:

$optionId = "selected option id";
$bundleTable = Mage::getSingleton('core/resource')->getTableName('catalog_product_bundle_selection');
$collection=Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(array('name', 'price'));
$collection->getSelect()->joinLeft(array('bundleselect'=> $bundleTable),"entity_id = bundleselect.product_id","product_id");
$collection->getSelect()->where(" bundleselect.selection_id IN (".$optionId.") " );

$origPrice = '0';
foreach($collection as $prod) {
    $origPrice += $prod->getSku();
}

echo $origSku;

答案 2 :(得分:0)

Magento 2 get bundle options with their selections details.

Class BundleItemDetails
   public function __construct(
        \Magento\Catalog\Model\ProductRepository; $productRepository
    )
    {
        $this->productRepository = $productRepository;
     }

 public function execute(){
       $product  = $this->productRepository->get("test-bundle-product");
       $optionsCollection = $product->getTypeInstance(true)
                                    ->getOptionsCollection($product);
       $optionDetails = [];
       foreach ($optionsCollection as $option){
         $selections = $product->getTypeInstance(true)
                               ->getSelectionsCollection(
                                $option->getOptionId(),$product
                             );
         //selection details by optionids
          foreach ($selections as $selection) {
                  $optionDetails[$option->getOptionId()] = $selection->getSku();
          }
      } 
  }


}