gpt4 book ai didi

magento - 如何获得捆绑选项选择 SKU?

转载 作者:行者123 更新时间:2023-12-01 09:58:29 25 4
gpt4 key购买 nike

<?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。

最佳答案

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

$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 />';
}
}
}
}

?>

关于magento - 如何获得捆绑选项选择 SKU?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20716335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com