Magento 제품 속성 값 가져 오기
전체 제품을로드하지 않고 제품 ID를 알고있는 경우 특정 제품 속성 값을 얻는 방법은 무엇입니까?
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
내가 아는 방법 :
$product->getResource()->getAttribute($attribute_code)
->getFrontend()->getValue($product)
당신이 사용할 수있는
<?php echo $product->getAttributeText('attr_id') ?>
대부분의 경우에 효과가 있으므로 Daniel Kocherga의 답변을 참조하십시오 .
속성의 값을 가져 오는 방법 외에도 때때로 select
또는 레이블을 가져오고 싶을 수 있습니다 multiselect
. 이 경우 헬퍼 클래스에 저장하는이 메서드를 만들었습니다.
/**
* @param int $entityId
* @param int|string|array $attribute atrribute's ids or codes
* @param null|int|Mage_Core_Model_Store $store
*
* @return bool|null|string
* @throws Mage_Core_Exception
*/
public function getAttributeRawLabel($entityId, $attribute, $store=null) {
if (!$store) {
$store = Mage::app()->getStore();
}
$value = (string)Mage::getResourceModel('catalog/product')->getAttributeRawValue($entityId, $attribute, $store);
if (!empty($value)) {
return Mage::getModel('catalog/product')->getResource()->getAttribute($attribute)->getSource()->getOptionText($value);
}
return null;
}
제품 모델을로드하지 않고는 가치를 얻을 수없는 것 같습니다. app / code / core / Mage / Eav / Model / Entity / Attribute / Frontend / Abstract.php 파일을 살펴보면 메서드가 표시됩니다.
public function getValue(Varien_Object $object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
if (in_array($this->getConfigField('input'), array('select','boolean'))) {
$valueOption = $this->getOption($value);
if (!$valueOption) {
$opt = new Mage_Eav_Model_Entity_Attribute_Source_Boolean();
if ($options = $opt->getAllOptions()) {
foreach ($options as $option) {
if ($option['value'] == $value) {
$valueOption = $option['label'];
}
}
}
}
$value = $valueOption;
}
elseif ($this->getConfigField('input')=='multiselect') {
$value = $this->getOption($value);
if (is_array($value)) {
$value = implode(', ', $value);
}
}
return $value;
}
보시다시피이 메서드는 데이터를 가져 오기 위해로드 된 개체가 필요합니다 (세 번째 줄).
먼저 원하는 속성이로드되었는지 확인한 다음 출력해야합니다. 이것을 사용하십시오 :
$product = Mage::getModel('catalog/product')->load('<product_id>', array('<attribute_code>'));
$attributeValue = $product->getResource()->getAttribute('<attribute_code>')->getFrontend()->getValue($product);
이 시도
$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute)
{
echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}
You don't have to load the whole product. Magentos collections are very powerful and smart.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('entity_id', $product->getId());
$collection->addAttributeToSelect('manufacturer');
$product = $collection->getFirstItem();
$manufacturer = $product->getAttributeText('manufacturer');
At the moment you call getFirstItem() the query will be executed and the result product is very minimal:
[status] => 1
[entity_id] => 38901
[type_id] => configurable
[attribute_set_id] => 9
[manufacturer] => 492
[manufacturer_value] => JETTE
[is_salable] => 1
[stock_item (Varien_Object)] => Array
(
[is_in_stock] => 1
)
This one works-
echo $_product->getData('ATTRIBUTE_NAME_HERE');
You can get attribute value by following way
$model = Mage::getResourceModel('catalog/product');
$attribute_value = $model->getAttributeRawValue($productId, 'attribute_code', $storeId);
$orderId = 1; // YOUR ORDER ID
$items = $block->getOrderItems($orderId);
foreach ($items as $item) {
$options = $item->getProductOptions();
if (isset($options['options']) && !empty($options['options'])) {
foreach ($options['options'] as $option) {
echo 'Title: ' . $option['label'] . '<br />';
echo 'ID: ' . $option['option_id'] . '<br />';
echo 'Type: ' . $option['option_type'] . '<br />';
echo 'Value: ' . $option['option_value'] . '<br />' . '<br />';
}
}
}
all things you will use to retrieve value product custom option cart order in Magento 2: https://www.mageplaza.com/how-get-value-product-custom-option-cart-order-magento-2.html
You could write a method that would do it directly via sql I suppose.
Would look something like this:
Variables:
$store_id = 1;
$product_id = 1234;
$attribute_code = 'manufacturer';
Query:
SELECT value FROM eav_attribute_option_value WHERE option_id IN (
SELECT option_id FROM eav_attribute_option WHERE FIND_IN_SET(
option_id,
(SELECT value FROM catalog_product_entity_varchar WHERE
entity_id = '$product_id' AND
attribute_id = (SELECT attribute_id FROM eav_attribute WHERE
attribute_code='$attribute_code')
)
) > 0) AND
store_id='$store_id';
You would have to get the value from the correct table based on the attribute's backend_type (field in eav_attribute) though so it takes at least 1 additional query.
If you have an text/textarea attribute named my_attr you can get it by: product->getMyAttr();
참고URL : https://stackoverflow.com/questions/6924226/magento-product-attribute-get-value
'code' 카테고리의 다른 글
C / C ++에서 프로그래밍 방식으로 코어 덤프를 발생시키는 방법 (0) | 2020.09.23 |
---|---|
JavaScript를 사용하여 HTML 테이블 데이터를 Excel로 내보내기 / JQuery가 Chrome 브라우저에서 제대로 작동하지 않습니다. (0) | 2020.09.23 |
배포 nodejs 패키지 (Ubuntu)를 사용하여 홈 디렉터리에 NPM 설치 (0) | 2020.09.23 |
섭씨 기호를 웹 페이지로 인코딩하는 가장 좋은 방법은 무엇입니까? (0) | 2020.09.23 |
Python 및 NumPy를 사용하는 매우 큰 행렬 (0) | 2020.09.23 |