2012-01-04 25 views
6

Tôi đang tạo menu điều hướng tùy chỉnh trong Magento hiển thị các danh mục từ một cửa hàng khác với một danh mục gốc khác từ cửa hàng hiện tại. Một số hạng mục nên được ẩn khi họ có 'Bao gồm trong Menu' đặt lên vị trí thứMô hình Magento Danh mục không tải tất cả dữ liệu

Nó phải có khả năng đọc thuộc tính này từ mô hình loại như trong câu hỏi này: How do I detect if a category has Include in Navigation Menu set to NO?

Tuy nhiên $ Category- > getIncludeInMenu() trả về NULL cho tất cả các hạng mục trong cài đặt Magento EE 1.11 của tôi.

// Load child categories for a specific root category 
$_uk_default_root_id = Mage::app()->getStore('uk_default')->getRootCategoryId(); 
$_uk_default_root_category = Mage::getModel('catalog/category')->load($_uk_default_root_id); 
$_sub_categories = $_uk_default_root_category->getChildrenCategories(); 

// Loop through the categories 
foreach ($_sub_categories as $_category) 
{ 
    if ($_category->getIsActive() && $_category->getIncludeInMenu()) 
    { 
     <echo navigation link> 
    } 
} 

Một biến thể của một danh mục không hiển thị thuộc tính 'include_in_menu', mặc dù thuộc tính 'is_active' hoạt động như mong đợi. Có cách nào khác để xác định xem danh mục có được hiển thị trong điều hướng không?

Var bãi chứa của đối tượng mục:

object(Mage_Catalog_Model_Category)[423] 
    protected '_eventPrefix' => string 'catalog_category' (length=16) 
    protected '_eventObject' => string 'category' (length=8) 
    protected '_cacheTag' => string 'catalog_category' (length=16) 
    protected '_useFlatResource' => boolean false 
    private '_designAttributes' => 
    array (size=6) 
     0 => string 'custom_design' (length=13) 
     1 => string 'custom_design_from' (length=18) 
     2 => string 'custom_design_to' (length=16) 
     3 => string 'page_layout' (length=11) 
     4 => string 'custom_layout_update' (length=20) 
     5 => string 'custom_apply_to_products' (length=24) 
    protected '_treeModel' => null 
    protected '_defaultValues' => 
    array (size=0) 
     empty 
    protected '_storeValuesFlags' => 
    array (size=0) 
     empty 
    protected '_lockedAttributes' => 
    array (size=0) 
     empty 
    protected '_isDeleteable' => boolean true 
    protected '_isReadonly' => boolean false 
    protected '_resourceName' => string 'catalog/category' (length=16) 
    protected '_resource' => null 
    protected '_resourceCollectionName' => string 'catalog/category_collection' (length=27) 
    protected '_dataSaveAllowed' => boolean true 
    protected '_isObjectNew' => null 
    protected '_data' => 
    array (size=15) 
     'entity_id' => string '16' (length=2) 
     'entity_type_id' => string '3' (length=1) 
     'attribute_set_id' => string '3' (length=1) 
     'parent_id' => string '15' (length=2) 
     'created_at' => string '2011-11-16 12:16:27' (length=19) 
     'updated_at' => string '2011-12-19 16:19:08' (length=19) 
     'path' => string '1/15/16' (length=7) 
     'position' => string '1' (length=1) 
     'level' => string '2' (length=1) 
     'children_count' => string '8' (length=1) 
     'is_active' => string '1' (length=1) 
     'request_path' => null 
     'name' => string 'Vacuum Cleaners' (length=15) 
     'url_key' => string 'vacuum-cleaners' (length=15) 
     'is_anchor' => string '1' (length=1) 
    protected '_hasDataChanges' => boolean true 
    protected '_origData' => 
    array (size=15) 
     'entity_id' => string '16' (length=2) 
     'entity_type_id' => string '3' (length=1) 
     'attribute_set_id' => string '3' (length=1) 
     'parent_id' => string '15' (length=2) 
     'created_at' => string '2011-11-16 12:16:27' (length=19) 
     'updated_at' => string '2011-12-19 16:19:08' (length=19) 
     'path' => string '1/15/16' (length=7) 
     'position' => string '1' (length=1) 
     'level' => string '2' (length=1) 
     'children_count' => string '8' (length=1) 
     'is_active' => string '1' (length=1) 
     'request_path' => null 
     'name' => string 'Vacuum Cleaners' (length=15) 
     'url_key' => string 'vacuum-cleaners' (length=15) 
     'is_anchor' => string '1' (length=1) 
    protected '_idFieldName' => string 'entity_id' (length=9) 
    protected '_isDeleted' => boolean false 
    protected '_oldFieldsMap' => 
    array (size=0) 
     empty 
    protected '_syncFieldsMap' => 
    array (size=0) 
     empty 
+0

Vết thức var của bạn không hiển thị đối tượng. Đối tượng là $ _category? Bước đầu tiên là xác minh đối tượng cần có thuộc tính/phương thức bạn muốn sử dụng. –

+0

Đối tượng là một 'Mage_Catalog_Model_Category'. Tôi đã cập nhật var dump để hiển thị đối tượng đầy đủ. –

Trả lời

14

Điều này là do Magento lười biếng tải mô hình thể loại. Phương thức getChildrenCategories() trên mô hình Mage_Catalog_Model_Category trả về một tập hợp các mô hình danh mục và Magento chỉ tải dữ liệu mô hình lõi thay vì dữ liệu EAV bổ sung cho từng mô hình. Thuộc tính include_in_menu được lưu trữ trong kho dữ liệu EAV và chưa được tải.

Mỗi danh mục có thể bị buộc tải bằng cách gọi $_category->load(); sẽ buộc Magento tải thêm dữ liệu EAV cho từng danh mục.

// Loop through the categories 
foreach ($_sub_categories as $_category) 
{ 
    $_category->load(); 
    if ($_category->getIsActive() && $_category->getIncludeInMenu()) 
    { 
     <echo navigation link> 
    } 
} 

này thay đổi _data mảng từ này:

array (size=15) 
    'entity_id' => string '16' (length=2) 
    'entity_type_id' => string '3' (length=1) 
    'attribute_set_id' => string '3' (length=1) 
    'parent_id' => string '15' (length=2) 
    'created_at' => string '2011-11-16 12:16:27' (length=19) 
    'updated_at' => string '2011-12-19 16:19:08' (length=19) 
    'path' => string '1/15/16' (length=7) 
    'position' => string '1' (length=1) 
    'level' => string '2' (length=1) 
    'children_count' => string '8' (length=1) 
    'is_active' => string '1' (length=1) 
    'request_path' => null 
    'name' => string 'Vacuum Cleaners' (length=15) 
    'url_key' => string 'vacuum-cleaners' (length=15) 
    'is_anchor' => string '1' (length=1) 

Để này:

array (size=33) 
    'entity_id' => string '16' (length=2) 
    'entity_type_id' => string '3' (length=1) 
    'attribute_set_id' => string '3' (length=1) 
    'parent_id' => string '15' (length=2) 
    'created_at' => string '2011-11-16 12:16:27' (length=19) 
    'updated_at' => string '2011-12-19 16:19:08' (length=19) 
    'path' => string '1/15/16' (length=7) 
    'position' => string '1' (length=1) 
    'level' => string '2' (length=1) 
    'children_count' => string '8' (length=1) 
    'is_active' => string '1' (length=1) 
    'request_path' => null 
    'name' => string 'Vacuum Cleaners' (length=15) 
    'url_key' => string 'vacuum-cleaners' (length=15) 
    'is_anchor' => string '1' (length=1) 
    'meta_title' => null 
    'display_mode' => string 'PRODUCTS' (length=8) 
    'custom_design' => null 
    'page_layout' => null 
    'url_path' => string 'vacuum-cleaners' (length=15) 
    'image' => string 'heading_vacuums_1.png' (length=21) 
    'include_in_menu' => string '1' (length=1) 
    'landing_page' => null 
    'custom_use_parent_settings' => string '0' (length=1) 
    'custom_apply_to_products' => string '0' (length=1) 
    'filter_price_range' => null 
    'description' => null 
    'meta_keywords' => null 
    'meta_description' => null 
    'custom_layout_update' => null 
    'available_sort_by' => null 
    'custom_design_from' => null 
    'custom_design_to' => null 

Trong đó bao gồm các thuộc tính include_in_menu.

0

Bạn phải sử dụng:

$ _category-> load ($ _ category-> getId());

đã làm việc cho tôi;)

Các vấn đề liên quan