类别图像 / getimageurl()为空
-
16-10-2019 - |
题
在后端,我可以看到该类别具有图像,但是如果我尝试在模板中显示它,则它是空的:
$_category = Mage::registry('current_category');
$_imgUrl = $_category->getImageUrl();
为什么是 $_imgUrl
空的?
模板文件是 \app\design\frontend\default\kemweb\template\page
var_dump($_category)
看起来这样:
`$_category` does contain the correct item (entity_id checked). Same code worked on other magento installation. Any clue what is going wrong here?
object(Mage_Catalog_Model_Category)#96 (23) {
["_eventPrefix":protected]=>
string(16) "catalog_category"
["_eventObject":protected]=>
string(8) "category"
["_cacheTag":protected]=>
string(16) "catalog_category"
["_useFlatResource":protected]=>
bool(false)
["_designAttributes":"Mage_Catalog_Model_Category":private]=>
array(6) {
[0]=>
string(13) "custom_design"
[1]=>
string(18) "custom_design_from"
[2]=>
string(16) "custom_design_to"
[3]=>
string(11) "page_layout"
[4]=>
string(20) "custom_layout_update"
[5]=>
string(24) "custom_apply_to_products"
}
["_treeModel":protected]=>
NULL
["_defaultValues":protected]=>
array(1) {
["url_path"]=>
string(13) "schenken.html"
}
["_storeValuesFlags":protected]=>
array(1) {
["url_path"]=>
bool(true)
}
["_lockedAttributes":protected]=>
array(0) {
}
["_isDeleteable":protected]=>
bool(true)
["_isReadonly":protected]=>
bool(false)
["_resourceName":protected]=>
string(16) "catalog/category"
["_resource":protected]=>
NULL
["_resourceCollectionName":protected]=>
string(27) "catalog/category_collection"
["_dataSaveAllowed":protected]=>
bool(true)
["_isObjectNew":protected]=>
NULL
["_data":protected]=>
array(34) {
["store_id"]=>
string(1) "1"
["entity_id"]=>
string(1) "3"
["entity_type_id"]=>
string(1) "3"
["attribute_set_id"]=>
string(1) "3"
["parent_id"]=>
string(1) "2"
["created_at"]=>
string(19) "2013-06-19 11:29:11"
["updated_at"]=>
string(19) "2013-07-02 09:56:14"
["path"]=>
string(5) "1/2/3"
["position"]=>
string(1) "1"
["level"]=>
string(1) "2"
["children_count"]=>
string(1) "0"
["name"]=>
string(8) "Schenken"
["url_key"]=>
string(8) "schenken"
["meta_title"]=>
NULL
["display_mode"]=>
string(8) "PRODUCTS"
["custom_design"]=>
NULL
["page_layout"]=>
NULL
["url_path"]=>
string(13) "schenken.html"
["thumbnail"]=>
string(16) "cat_schenken.jpg"
["is_active"]=>
string(1) "1"
["include_in_menu"]=>
string(1) "1"
["landing_page"]=>
NULL
["is_anchor"]=>
string(1) "1"
["custom_use_parent_settings"]=>
string(1) "0"
["custom_apply_to_products"]=>
string(1) "0"
["description"]=>
NULL
["meta_keywords"]=>
NULL
["meta_description"]=>
NULL
["custom_layout_update"]=>
NULL
["available_sort_by"]=>
NULL
["custom_design_from"]=>
NULL
["custom_design_to"]=>
NULL
["filter_price_range"]=>
NULL
["path_ids"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
}
["_hasDataChanges":protected]=>
bool(true)
["_origData":protected]=>
array(33) {
["store_id"]=>
string(1) "1"
["entity_id"]=>
string(1) "3"
["entity_type_id"]=>
string(1) "3"
["attribute_set_id"]=>
string(1) "3"
["parent_id"]=>
string(1) "2"
["created_at"]=>
string(19) "2013-06-19 11:29:11"
["updated_at"]=>
string(19) "2013-07-02 09:56:14"
["path"]=>
string(5) "1/2/3"
["position"]=>
string(1) "1"
["level"]=>
string(1) "2"
["children_count"]=>
string(1) "0"
["name"]=>
string(8) "Schenken"
["url_key"]=>
string(8) "schenken"
["meta_title"]=>
NULL
["display_mode"]=>
string(8) "PRODUCTS"
["custom_design"]=>
NULL
["page_layout"]=>
NULL
["url_path"]=>
string(13) "schenken.html"
["thumbnail"]=>
string(16) "cat_schenken.jpg"
["is_active"]=>
string(1) "1"
["include_in_menu"]=>
string(1) "1"
["landing_page"]=>
NULL
["is_anchor"]=>
string(1) "1"
["custom_use_parent_settings"]=>
string(1) "0"
["custom_apply_to_products"]=>
string(1) "0"
["description"]=>
NULL
["meta_keywords"]=>
NULL
["meta_description"]=>
NULL
["custom_layout_update"]=>
NULL
["available_sort_by"]=>
NULL
["custom_design_from"]=>
NULL
["custom_design_to"]=>
NULL
["filter_price_range"]=>
NULL
}
["_idFieldName":protected]=>
string(9) "entity_id"
["_isDeleted":protected]=>
bool(false)
["_oldFieldsMap":protected]=>
array(0) {
}
["_syncFieldsMap":protected]=>
array(0) {
}
}
解决方案
我看到只有缩略图属性具有值(cat_schenken.jpg)。在Magento管理员中,您可以上传类别图像和类别缩略图图像。因此,您可能仅上传了缩略图图像,而不是主图像。
尝试使用zend_debug :: dump($ _ category-> getData())以更好地了解该类别属性。
其他提示
看起来您甚至没有正确设置映像属性, getImageUrl
需要这个:
public function getImageUrl()
{
$url = false;
if ($image = $this->getImage()) {
$url = Mage::getBaseUrl('media').'catalog/category/'.$image;
}
return $url;
}
根据var_dump,类别模型有问题,通常 $_data['image']
属性设置为某物。
您确定不混合图像和缩略图吗?您是否修改了数据库中的内容?喜欢属性组关系?因为那可能会导致您的问题。
我在Magento 1.9.2.1构建上遇到了同样的问题。但是我发现(怪异的)使用时未返回图像URL getChildRencategories(). 。我的答案是使用 getCollection() 相反,在模型上:
$_cat = Mage::getModel('catalog/category')->load($id);
if (isset($_cat) && !empty($_cat) && is_object($_cat)) {
// Create category collection for children
$childrenCollection = $_cat->getCollection();
// Only get child categories of parent cat
$childrenCollection->addIdFilter($_cat->getChildren());
// Only get active categories
$childrenCollection->addAttributeToFilter('is_active', 1);
// Add base attributes
$childrenCollection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->setOrder('position', Varien_Db_Select::SQL_ASC)
->joinUrlRewrite();
// Add Image
$childrenCollection->addAttributeToSelect('image');
if (count($childrenCollection) > 0) {
foreach($childrenCollection as $subcat) {
if ($subcat->getIsActive()) {
$subcatName = $subcat->getName();
$subcatUrl = $subcat->getUrl();
$img = $subcat->getImage();
$subcatImgUrl = Mage::getBaseUrl('media').'catalog/category/' . $img;
if (!empty($subcatName) && !empty($subcatUrl)) {
$html .= '<li class="brand ' . $className . '">';
$html .= '<a href="' . $subcatUrl . '" title="' . $subcatName . '">';
$html .= '<img src="' . $subcatImgUrl . '" alt="' . $subcatName . '" />';
$html .= '</a>';
$html .= '</li>';
}
}
}
}
Pandawebstudio的答案确实有助于调试情况,因此谢谢您。
根据您的VAR转储注册表变量不包含字段图像。
首先您需要检查 getImage()
给出价值与否
在 thebod答案 已经提到了 getImageUrl()
每当类别的图像字段具有任何值时,都会给出值。
在这个问题中,类别的对象 does not contain
场地 图片 那 getImageUrl() does not give
结果。
可能是此类别EAV属性(图像)不退出数据库。
要在系统中检查此属性,您可以在MySQL查询下方尝试
SELECT attribute_code
FROM eav_attribute
WHERE entity_type_id
IN (
SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code = 'catalog_category'
) and attribute_code ='image'
我正在使用它来获取类别图像URL。
<?php $child= Mage:getSingleton('catalog/layer')->getCurrentcategory()->getId();
$imagesrc= Mage:getModel('catalog/category')->load($child)->getimageUrl();
echo $imagesrc; ?>
我尝试关注以使其不会影响性能,您需要召集类别模型
在Magento 2.2的根中打开
/Vendor/magento/module-catalog/model/resourcemodel/category.php
在Line750ish上
添加
->addAttributeToSelect(
'image'
)
或替换功能
/**
* Return child categories
*
* @param \Magento\Catalog\Model\Category $category
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
public function getChildrenCategories($category)
{
$collection = $category->getCollection();
/* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
$collection->addAttributeToSelect(
'url_key'
)->addAttributeToSelect(
'image'
)->addAttributeToSelect(
'name'
)->addAttributeToSelect(
'all_children'
)->addAttributeToSelect(
'is_anchor'
)->addAttributeToFilter(
'is_active',
1
)->addIdFilter(
$category->getChildren()
)->setOrder(
'position',
\Magento\Framework\DB\Select::SQL_ASC
)->joinUrlRewrite();
return $collection;
}