Magento - 查询产品选项 [英] Magento - Query for Product Options

查看:167
本文介绍了Magento - 查询产品选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个控制器,用于查找给定产品的不同选项(例如大,中,小,红,蓝等)。



我越来越近了,但我还是不能明白。这是我在我的控制器中写的代码

  $ db = Mage :: getModel('catalog / product') - > load ($ productId); 
print_r($ db-> getOptions()); //返回一个空数组
echo $ db-> getHasOptions(); // echo 1

但是当我在第二行执行print_r空数组。第三行echo的值为1,表示有SHOULD BE选项。



附加详情
我试过clockworkgeek的 $ db-> getProductOptions(),不返回任何内容。我尝试了 $ db-> getProductOptionsCollection(),并得到这个输出

  Array 

[totalRecords] => 0
[items] => Array




我的代码有什么问题,它没有返回允许的产品选项?

$ b $ c getCustomOptions() code>或 getProductOptionsCollection() getProductOptionsCollection() - > load()

编辑

我在一个我知道有选项的产品上尝试过。

 <?php 
require'app / Mage.php';
Mage :: app();

$ product = Mage :: getModel('catalog / product') - > load($ productId);
foreach($ product-> getProductOptions()as $ option){
print_r($ option-> debug());
}

得到了这样的东西:

 数组

[option_id] => 37
[product_id] => 8
[type] = > multidate
[is_require] => 1
[sku] =>
[image_size_x] => 0
[image_size_y] => 0
[sort_order] => 1
[default_title] =>日期
[title] = 日期

但是, getOptions()也为我工作,所以我不知道发生了什么。



后编辑

混淆是语义之一。一个简单的产品可以有自定义选项,它们允许创建几个表单字段,作为订单的一部分记录。可配置产品使用关联产品创建具有条件字段的表单。



例如,你可能卖的是大绿色,小绿色或大蓝色,但没有小蓝色的袜子。有了一个简单的产品,你会有一个字段为大/小和蓝色/绿色的字段 - 这允许客户选择小蓝色,这是错误的。



因此,为了找到可配置的组件部分,您可以这样做:

  if($ product-> isConfigurable()){
$ configurable = $ product-> getTypeInstance();
$ childProducts = $ product-> getUsedProducts($ product);
foreach($ childProducts as $ child){
//你有一个$ child现在
}
}

要找到等效于 getOptions()的文件,您需要:

  if($ product-> isConfigurable()){
$ configurable = $ product-> getTypeInstance
$ attributes = $ configurable-> getConfigurableAttributes($ product);
// $ attributes是一个Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute_Collection
foreach($ attributes as $ attribute){
// $ attribute是一个Mage_Catalog_Model_Product_Type_Configurable_Attribute
print $ attribute-> getLabel
}
}

Mage_Catalog_Model_Product_Type_Configurable_Attribute 没有太多资料可以揭示其本身。


I want to write a controller that finds the different options for a given product (eg. Large, Medium, Small, Red, Blue etc...).

Can anyone show me the code I would write into my controller?

Additional details

I'm getting closer, but I still can't figure it out. Here's the code I wrote in my controller

$db = Mage::getModel('catalog/product')->load($productId);
print_r($db->getOptions());  // returns an empty array
echo $db->getHasOptions();  // echos 1

But when I do the print_r() on the second line, the getOptions returns an empty array. The third line echo's the value 1, which means there SHOULD BE options.

Additional Details I tried clockworkgeek's solution of $db->getProductOptions(), that returned nothing. I tried $db->getProductOptionsCollection(), and got this output

Array
(
    [totalRecords] => 0
    [items] => Array
        (
        )

)

What's wrong with my code such that it is not returning the allowable product options?

解决方案

Instead of getOptions() please try getCustomOptions() or getProductOptionsCollection() or getProductOptionsCollection()->load().

Edit
I tried this on a product I knew had options.

<?php
require 'app/Mage.php';
Mage::app();

$product = Mage::getModel('catalog/product')->load($productId);
foreach ($product->getProductOptions() as $option) {
    print_r($option->debug());
}

And got something like this:

Array
(
    [option_id] => 37
    [product_id] => 8
    [type] => multidate
    [is_require] => 1
    [sku] => 
    [image_size_x] => 0
    [image_size_y] => 0
    [sort_order] => 1
    [default_title] => Dates
    [title] => Dates
)

However, getOptions() also worked for me so I don't know what's going on.

Post-edit
The confusion was one of semantics. A simple product can have "custom options", they allow creation of a few form fields which are recorded as part of the order. A configurable product uses "associated products" to create a form with conditional fields.

For example you might be selling socks that are large-green, small-green or large-blue - but no small-blue ones. With a simple product you would have a field for large/small and a field for blue/green - which allows the customer to select small-blue and that is wrong.

So to find the component parts of a configurable you might do something like this:

if ($product->isConfigurable()) {
    $configurable = $product->getTypeInstance();
    $childProducts = $product->getUsedProducts($product);
    foreach ($childProducts as $child) {
        // You have a $child now
    }
}

To find the equivalent of getOptions() you need this:

if ($product->isConfigurable()) {
    $configurable = $product->getTypeInstance();
    $attributes = $configurable->getConfigurableAttributes($product);
    // $attributes is a Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute_Collection
    foreach ($attributes as $attribute) {
        // $attribute is a Mage_Catalog_Model_Product_Type_Configurable_Attribute
        print $attribute->getLabel();
    }
}

Mage_Catalog_Model_Product_Type_Configurable_Attribute doesn't have much to reveal about itself.

这篇关于Magento - 查询产品选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆