在magento产品详细页显示自定义的属性是非常容易的。首先你需找到/app/design/frontend/default/default/template/catalog/product/view.phtml
该目录下的view.phtml 文件,当然这个文件的路径取决于你选择的模板,在你模板的路径可能是/app/design/frontend/default/yourthemes/template/catalog/product/view.phtml
找到后打开编辑,增加属性调用代码,常见的几种属性代码如下:
简单描述(short description):
- <?php echo $this_helper(‘catalog/output’)->productAttribute($_product, nl2br($_product->getShortDescription()), ‘short_description’) ?>
描述(description):
- <?php echo $this->helper(‘catalog/output’)->productAttribute($this->getProduct(), nl2br($this->getProduct()->getDescription()), ‘description’) ?>
产品名称(name):
- <?php echo $_helper->productAttribute($_product, $this->htmlEscape($_product->getName()), ‘name’) ?>
产品编号(sku):
- <?php echo $this->htmlEscape($_product->getSku()) ?>
产品价格(price):
- <?php echo $_coreHelper->currency($_finalPrice,true,false) ?>
上面是magento商品几种常见的属性调用代码,把代码放在你想展示的合适位置即可。
当然,你也可以调用自定义的属性。下面介绍两种类型的属性调用:
1.你新增的属性为文本或文本域类型(Text Field or Text Area),那么你的代码应为:
- < ?php echo $_product->getAttributeName() ?>
例如,你新增了一个属性代码(Attribute Code)为 shoe_size的属性,那么你的调用代码应写成下面格式:
- < ?php echo $_product->getShoeSize() ?>
你应该把去掉下划线,并且第一字母大写,然后替代AttributeName ,如果你使用的是getshoesize() ,magento将无法工作。
2.你新增的属性为下拉菜单类型(Dropdown)和多项选择类型(Multiple Select),那么你的代码应写成下面格式:
- < ?php echo $_product->getAttributeText('shirt_size') ?>
在Magento目录的分类页面里,希望在左侧导航获取到父分类和子分类,可以打开app/your_package/your_themes/template/catalog/navigation/left.phtml
显示父分类的分类名
- $currentCat = Mage::registry('current_category');
-
- if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
-
- echo $this->getCurrentCategory()->getName() ;
- else
- {
-
- echo $this->getCurrentCategory()->getParentCategory()->getName() ;
- }
显示子分类的分类名
显示的子分类是建立在当前的父分类的基础上
- $currentCat = Mage::registry('current_category');
-
- if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
- {
-
- $loadCategory = $currentCat;
- }
- else
- {
-
- $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
- }
- $subCategories = explode(',', $loadCategory->getChildren());
-
- foreach ( $subCategories as $subCategoryId )
- {
- $cat = Mage::getModel('catalog/category')->load($subCategoryId);
-
- if($cat->getIsActive())
- {
- echo '<a href="%27.$cat-%3EgetURL%28%29.%27">'.$cat->getName().'</a>';
- }
- }
Get store
data
Mage
:
:
app
(
)
-
>
getStore
(
)
;
Store Id
Mage
:
:
app
(
)
-
>
getStore
(
)
-
>
getStoreId
(
)
;
Store code
Mage
:
:
app
(
)
-
>
getStore
(
)
-
>
getCode
(
)
;
Website Id
Mage
:
:
app
(
)
-
>
getStore
(
)
-
>
getWebsiteId
(
)
;
Store Name
Mage
:
:
app
(
)
-
>
getStore
(
)
-
>
getName
(
)
;
Is Active
Mage
:
:
app
(
)
-
>
getStore
(
)
-
>
getIsActive
(
)
;
Store Home Url
Mage
:
:
app
(
)
-
>
getStore
(
)
-
>
getHomeUrl
(
)
;
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
// 当前分类是顶级分类
$loadCategory = $currentCat;
}
else
{
// 当前分类是顶级分类的的一个子分类,载入当前分类的父分类
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
echo '<a href="<?php echo $this->getCategoryUrl($cat) ?>">'.$cat->getName().'</a>';
}
}
1. 得到产品,以及过滤:
- getResourceModel('catalog/product_collection')
- $collection = Mage::getModel('catalog/product')->getCollection();
- ->Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
getResourceModel('catalog/product_collection')
$collection = Mage::getModel('catalog/product')->getCollection();
->Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
2.取得属性集,属性过滤
- $attributes = Mage::getSingleton('catalog/config')
- ->getProductAttributes();
- $collection->addAttributeToSelect($attributes)
- ->addMinimalPrice()
- ->addFinalPrice()
- ->addTaxPercents()
- ->addAttributeToFilter('test_product', 1, 'left')
- ->addStoreFilter();
- Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
-
$attributes = Mage::getSingleton('catalog/config')
->getProductAttributes();
$collection->addAttributeToSelect($attributes) //选择属性
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToFilter('test_product', 1, 'left') //属性过滤 具备该属性
->addStoreFilter(); //商店过滤
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
//对产品的状态进行可见性过滤 Disable,EnableMage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); //visibility过滤 Catalog,Search,Not Visible$this->_productCollection = $collection;
3.
- protected function _prepareLayout()
- {
- if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
- $breadcrumbsBlock->addCrumb('home', array(
- 'label'=>Mage::helper('catalog')->__('Home'),
- 'title'=>Mage::helper('catalog')->__('Go to Home Page'),
- 'link'=>Mage::getBaseUrl()
- ));
- }
- parent::_prepareLayout();
- }
protected function _prepareLayout() //面包屑
{
if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
$breadcrumbsBlock->addCrumb('home', array(
'label'=>Mage::helper('catalog')->__('Home'),
'title'=>Mage::helper('catalog')->__('Go to Home Page'),
'link'=>Mage::getBaseUrl()
));
}
parent::_prepareLayout();
}
4.
- protected function _beforeToHtml()
- {
- parent::_beforeToHtml();
- $toolbar = $this->getToolbarBlock();
- $toolbar->removeOrderFromAvailableOrders('position');
- return $this;
- }
protected function _beforeToHtml()
{
parent::_beforeToHtml();
$toolbar = $this->getToolbarBlock(); //工具条
$toolbar->removeOrderFromAvailableOrders('position'); //移除可用的排序
return $this;
}
5. 设定使用的模板
- $this->setTemplate('catalog/product/edit.phtml');
$this->setTemplate('catalog/product/edit.phtml'); //设置模板
6. 后台修改加入后台的块内容
- $this->_addContent($this->getLayout()->createBlock('test/adminhtml_catalog_product_edit'));
$this->_addContent($this->getLayout()->createBlock('test/adminhtml_catalog_product_edit')); //实体的 不能是布尔的
7.图片的在php文件调用
- <img src="<?php echo $this->getSkinUrl('images/tweeticon.jpg');?>"/>
<img src="<?php echo $this->getSkinUrl('images/tweeticon.jpg');?>"/>
8. 得到当前的store的RootCategoryId
- $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
9. 得到catelog
- $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
10.
- $collection = Mage::getResourceModel('catalog/product_collection')
- ->setStoreId($this->getStoreId())
- ->addCategoryFilter($this);
-
Magento has a vast configuration table (core_config_data in your database) that is managed in the Magento admin under System > Configuration. Occasionally you might need to find the value of a core configuration variable to do something on the fronted of your Magento store. To do this you can use the Magento Mage::getStoreConfig() method.
Here is an example where we get the value of the Logo Image Src found in System > Configuration > Design > Header:
-
- $logoSrc = Mage::getStoreConfig('design/header/logo_src');
// Get the logo from the current store config
$logoSrc = Mage::getStoreConfig('design/header/logo_src');
This will return something like "images/logo.gif" which you can then use to pull the logo wherever you want. This way if your logo is ever changed to say, ""images/logo.jpg", you don't have to hunt through all your templates to replace the .gif extension with .jpg. The value will be updated instantly when you save your configuration.
The "design/header/logo_src" matches the path column in the core_config_data table in the database. You can use the path value to load the value of any config row.
Example: we need to find out what language locale the current store is using. We can do this by getting the value for the general/locale/code path:
For English stores this will return something like: en_US.
I hope this makes sense and is useful to someone. Any questions or problems, please post in the comments!
- $configValue = Mage::getStoreConfig('sectionName/groupName/fieldName');
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName');
实例1 Get store contact telephone:
That's a Core configuration, so it's saved in core_config_data
table, and the phone information is the field: general/store_information/phone So, all you need is to read the configuration data as
- $storePhone = Mage::getStoreConfig('general/store_information/phone');
$storePhone = Mage::getStoreConfig('general/store_information/phone');
For CMS pages insert the following variable:
- {{config path="general/store_information/phone"}}
{{config path="general/store_information/phone"}}
You can find more info on this here, and you can always do the reverse programmatically
实例2 magento get store configuration:
How to get Magento system configuration value? It's easy:
- Mage::getStoreConfig($path, $storeCode)
Mage::getStoreConfig($path, $storeCode) // $storeCode is not required
for example you need to get value set for store phone number from system config:
- Mage::getStoreConfig('general/store_information/phone');
Mage::getStoreConfig('general/store_information/phone');
Also, you can get configuration value as true or false like this:
- Mage::getStoreConfigFlag('general/store_information/phone');
- Mage::getStoreConfigFlag('general/yes_no/choice');
Mage::getStoreConfigFlag('general/store_information/phone'); // return true
Mage::getStoreConfigFlag('general/yes_no/choice'); // true or false based on selection
实例3 Get Store Email Addresses:
General Contact
-
- Mage::getStoreConfig('trans_email/ident_general/name');
-
- Mage::getStoreConfig('trans_email/ident_general/email');
/* Sender Name */
Mage::getStoreConfig('trans_email/ident_general/name');
/* Sender Email */
Mage::getStoreConfig('trans_email/ident_general/email');
Sales Representative
-
- Mage::getStoreConfig('trans_email/ident_sales/name');
-
- Mage::getStoreConfig('trans_email/ident_sales/email');
/* Sender Name */
Mage::getStoreConfig('trans_email/ident_sales/name');
/* Sender Email */
Mage::getStoreConfig('trans_email/ident_sales/email');
Customer Support
-
- Mage::getStoreConfig('trans_email/ident_support/name');
-
- Mage::getStoreConfig('trans_email/ident_support/email');
/* Sender Name */
Mage::getStoreConfig('trans_email/ident_support/name');
/* Sender Email */
Mage::getStoreConfig('trans_email/ident_support/email');
Custom Email 1
-
- Mage::getStoreConfig('trans_email/ident_custom1/name');
-
- Mage::getStoreConfig('trans_email/ident_custom1/email');
/* Sender Name */
Mage::getStoreConfig('trans_email/ident_custom1/name');
/* Sender Email */
Mage::getStoreConfig('trans_email/ident_custom1/email');
Custom Email 2
-
- Mage::getStoreConfig('trans_email/ident_custom2/name');
-
- Mage::getStoreConfig('trans_email/ident_custom2/email');
/* Sender Name */
Mage::getStoreConfig('trans_email/ident_custom2/name');
/* Sender Email */
Mage::getStoreConfig('trans_email/ident_custom2/email');
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($this);