php 如何在 Magento 中以编程方式获取自定义选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2829164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:47:46  来源:igfitidea点击:

How to get Custom Options Programmatically in Magento

phpmagento

提问by DaveC

I have a couple products at checkout that I need to be able to get all of the custom options that are selected for them through code.

我在结账时有几个产品,我需要能够通过代码获得为它们选择的所有自定义选项。

Any help is much appreciated!

任何帮助深表感谢!

回答by Knowledge Craving

I will just give you an example of one product. Let's say that you know the Sku (for example, let it be "ABCDE") of your required product. So you will be able to get the ID of that product.

我只会给你举一个产品的例子。假设您知道所需产品的 Sku(例如,将其设为“ABCDE”)。因此,您将能够获得该产品的 ID。

The code will be somewhat like:-

代码将有点像:-

$productSku = "ABCDE";
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku( $productSku );
$product->load($productId);

/**
 * In Magento Models or database schema level, the product's Custom Options are
 * executed & maintained as only "options". So, when checking whether any product has
 * Custom Options or not, we should check by using this method "hasOptions()" only.
 */
if($product->hasOptions()) {
    echo '<pre>';

    foreach ($product->getOptions() as $o) {
        $optionType = $o->getType();
        echo 'Type = '.$optionType;

        if ($optionType == 'drop_down') {
            $values = $o->getValues();

            foreach ($values as $k => $v) {
                print_r($v);
            }
        }
        else {
            print_r($o);
        }
    }

    echo '</pre>';
}

I think this will let you get started.

我想这会让你开始。

Depending upon the type of the option in the variable "$optionType", you need to call another nested "foreach" loop. I have worked on text boxes, text fields, drop downs, but not on other types. So I suppose you need to do some more RnD by yourself.

根据变量“ $optionType”中选项的类型,您需要调用另一个嵌套的“ foreach”循环。我曾研究过文本框、文本字段、下拉列表,但没有研究过其他类型。所以我想你需要自己做更多的 RnD。

回答by jazkat

For those who want to see selected custom options later in admin panelin Order/Invoice/Shipment/Creditmemo, find files: /app/design/adminhtml/[default]/default/template/sales/order/view/items/renderer/default.phtml
/app/design/adminhtml/[default]/default/template/sales/order/invoice/view/items/renderer/default.phtml /app/design/adminhtml/[default]/default/template/sales/order/shipment/view/items/renderer/default.phtml /app/design/adminhtml/[default]/default/template/sales/order/creditmemo/view/items/renderer/default.phtml PS: I haven't changed configurated.phtml files for invoice/shipment/creditmemo

对于那些希望稍后在Order/Invoice/Shipment/Creditmemo 的管理面板中查看所选自定义选项的人,请查找文件:/app/design/adminhtml/[default]/default/template/sales/order/view/items/renderer/ default.phtml
/app/design/adminhtml/[default]/default/template/sales/order/invoice/view/items/renderer/default.phtml /app/design/adminhtml/[default]/default/template/sales/ order/shipment/view/items/renderer/default.phtml /app/design/adminhtml/[default]/default/template/sales/order/creditmemo/view/items/renderer/default.phtml PS:我没变发票/发货/信用备忘录的 configurated.phtml 文件

and insert code somewhere after <?php echo $_item->getSku(); ?></div>and before it's row's closing tag </td>(be careful, it's different for each file)

<?php echo $_item->getSku(); ?></div>在行的结束标记之前和之后的某处插入代码</td>(注意,每个文件都不同)

Insert code:

插入代码:

        <?php  
    //---------start:---------------          
    // if ($allOptions = $_item->_getData('product_options')) {             // only for order item
    if ($allOptions = $_item->getOrderItem()->getData('product_options')) { // for invoice, shipping, creditmemo
        $options = unserialize($allOptions);

        if (isset($options['options'])) { 
            foreach ($options['options'] as $optionValues) {
                if ($optionValues['value']) {
                    echo '&nbsp;<strong><i>'. $optionValues['label'].'</i></strong>: ';

                    $_printValue = isset($optionValues['print_value']) ? $optionValues['print_value'] : strip_tags($optionValues['value']);
                    $values = explode(', ', $_printValue);
                    foreach ($values as $value) {
                        if (is_array($value))
                          foreach ($value as $_value) 
                              echo $_value;
                        else echo $value; 
                    }
                    echo '<br />';
                }
            }    
        }
    }
    //---------end:---------------                  
    ?>        

Also note that in code there is a line (if sentence) that works only in order default.phtml file, and the second if sentence works in invoice/shipping/creditmemo files. It depends where you post the code, make sure the right sentence is commented out.

另请注意,在代码中有一行(if 语句)仅适用于 order default.phtml 文件,第二个 if 语句适用于 invoice/shipping/creditmemo 文件。这取决于您发布代码的位置,确保注释掉正确的句子。

hope this helps, thanks also to Knowledge Craving whose code helped me quite a bit :-) jazkat

希望这会有所帮助,还要感谢 Knowledge Craving,他的代码对我帮助很大:-) jazkat

回答by marco birchler

Please note that

请注意

$product->hasCustomOptions()

in "Knowledge Craving"'s solution does always return false (at least in my case, Magento 1.6.2). Therefore the if-condition is never fulfilled and the block below is not executed.

在“知识渴望”的解决方案中,总是返回 false(至少在我的情况下,Magento 1.6.2)。因此,if 条件永远不会满足,下面的块也不会执行。

回答by Chiragit007

    $quote=$observer->getEvent()->getQuote();
    $quoteid=$quote->getId();
    $session= Mage::getSingleton('checkout/session');
    $getotal = Mage::helper('checkout')->getQuote()->getGrandTotal();

    foreach($session->getQuote()->getAllItems() as $item)
        {

         $sellcheck = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getissellbool();
         $options = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getProductOptionsCollection();
         foreach ($options as $o) 
             { 
                $title = $o->getTitle();
                $values = $o->getValues();
                foreach($values as $v)
                  {
                     $mydata = $v->getPrice();
                     echo 'options price: ' . $mydata;                      
                        }

               } 

          }

To access product custom options at the shopping cart you can utilise this code.

要在购物车中访问产品自定义选项,您可以使用此代码。

回答by Naresh

I hope it will useful to you for get only Custom Dropdown values in Product page

我希望它对您在产品页面中仅获取自定义下拉值有用

Just paste the following code in this file at last

最后将以下代码粘贴到这个文件中即可

app/design/frontend/base/default/template/catalog/product/view/options.phtml

app/design/frontend/base/default/template/catalog/product/view/options.phtml

<?php
    $product = Mage::getModel("catalog/product")->load($this->getProduct()->getId()); //product id
    foreach ($product->getOptions() as $_option) {
        $values = $_option->getValues();
        foreach ($values as $v) {
            print_r($v->getTitle());
            echo "<br />";
        }
    }
?>

回答by 502_Geek

We can also solve like that, that can display on checkout page.

我们也可以这样解决,可以显示在结帐页面上。

 $items = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
 foreach($items as $product) {
     $options = $product->getProduct()->getTypeInstance(true)->getOrderOptions($product->getProduct());
     if ($options)
     {
        if (isset($options['options']))
        {
           $result = $options['options'];
        }
        if(count($result)>0){
           foreach($result as $key =>$value){
                $resultoption =  $value['value'];
           }
        }
    }

回答by Surya prakash Patel

You can try this code in template/checkout/cart/item/default.php:

您可以在 template/checkout/cart/item/default.php 中尝试此代码:

if($Product->hasOptions)
            {
                $optionsArr = $Product->getOptions();
                 foreach ($optionsArr as  $optionKey => $optionVal)
                {
                          $optStr.= "<select style='display:block; clear:both;' name='options[".$optionVal->getId()."]'>";    
                  foreach($optionVal->getValues() as $valuesKey => $valuesVal)
                    {
                          $optStr.= "<option value='".$valuesVal->getId()."'>".$valuesVal->getTitle()."</option>";
                    }
                    $optStr.= "</select>";
                    }
   echo($optStr ); 
            }