Magento - 产品选项查询 [英] Magento - Query for Product Options
问题描述
我想编写一个控制器来为给定的产品(例如大、中、小、红色、蓝色等...)找到不同的选项.
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?
其他详细信息
我越来越近了,但我还是想不通.这是我在控制器中编写的代码
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
但是当我在第二行执行 print_r() 时,getOptions 返回一个空数组.第三行 echo 的值是 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.
其他详细信息我尝试了clockworkgeek 的$db->getProductOptions()
解决方案,但没有返回任何内容.我尝试了 $db->getProductOptionsCollection()
,并得到了这个输出
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?
推荐答案
代替 getOptions()
请尝试 getCustomOptions()
或 getProductOptionsCollection()
code> 或 getProductOptionsCollection()->load()
.
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());
}
得到了这样的东西:
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
)
但是,getOptions()
也对我有用,所以我不知道发生了什么.
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
}
}
要找到 getOptions()
的等效项,您需要:
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
没有太多可透露的信息自己.
Mage_Catalog_Model_Product_Type_Configurable_Attribute
doesn't have much to reveal about itself.
这篇关于Magento - 产品选项查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!