首页 > 其他分享 >magento 常用函数

magento 常用函数

时间:2022-12-16 23:34:35浏览次数:48  
标签:常用 Php 函数 product catalog magento getStoreConfig email Mage

在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):


1. <?php echo $this_helper(‘catalog/output’)->productAttribute($_product, nl2br($_product->getShortDescription()), ‘short_description’) ?>


描述(description):


1. <?php echo $this->helper(‘catalog/output’)->productAttribute($this->getProduct(), nl2br($this->getProduct()->getDescription()), ‘description’) ?>


产品名称(name):


1. <?php echo $_helper->productAttribute($_product, $this->htmlEscape($_product->getName()), ‘name’) ?>


产品编号(sku):


1. <?php echo $this->htmlEscape($_product->getSku()) ?>


产品价格(price):


1. <?php echo $_coreHelper->currency($_finalPrice,true,false) ?>


上面是magento商品几种常见的属性调用代码,把代码放在你想展示的合适位置即可。
当然,你也可以调用自定义的属性。下面介绍两种类型的属性调用:
1.你新增的属性为文本或文本域类型(Text Field or Text Area),那么你的代码应为:


1. < ?php echo $_product->getAttributeName() ?>


例如,你新增了一个属性代码(Attribute Code)为 shoe_size的属性,那么你的调用代码应写成下面格式:


1. < ?php echo $_product->getShoeSize() ?>


你应该把去掉下划线,并且第一字母大写,然后替代AttributeName ,如果你使用的是getshoesize() ,magento将无法工作。
2.你新增的属性为下拉菜单类型(Dropdown)和多项选择类型(Multiple Select),那么你的代码应写成下面格式:


1. < ?php echo $_product->getAttributeText('shirt_size') ?>




在Magento目录的分类页面里,希望在左侧导航获取到父分类和子分类,可以打开app/your_package/your_themes/template/catalog/navigation/left.phtml
显示父分类的分类名


1. $currentCat = Mage::registry('current_category');
2. //如果是根目录,则显示当前目录
3. if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
4. //显示当前目录名
5. echo $this->getCurrentCategory()->getName() ;
6. else
7. {
8. //显示当前目录的父分类名
9. echo $this->getCurrentCategory()->getParentCategory()->getName() ;
10. }


显示子分类的分类名
显示的子分类是建立在当前的父分类的基础上


1. $currentCat = Mage::registry('current_category');
2.
3. if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
4. {
5. // 当前分类是顶级分类
6. $loadCategory = $currentCat;
7. }
8. else
9. {
10. // 当前分类是顶级分类的的一个子分类,载入当前分类的父分类
11. $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
12. }
13. $subCategories = explode(',', $loadCategory->getChildren());
14.
15. foreach ( $subCategories as $subCategoryId
16. {
17. $cat = Mage::getModel('catalog/category')->load($subCategoryId);
18.
19. if($cat->getIsActive())
20. {
21. echo '<a href="%27.$cat-%3EgetURL%28%29.%27">'.$cat->getName().'</a>';
22. }
23. }


​​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. 得到产品,以及过滤:


Php代码
​​

​​​

​​​​​


1. getResourceModel('catalog/product_collection')
2. $collection = Mage::getModel('catalog/product')->getCollection();
3. ->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.取得属性集,属性过滤


Php代码
​​

​​​

​​​​​


1. $attributes = Mage::getSingleton('catalog/config')
2. ->getProductAttributes();
3. $collection->addAttributeToSelect($attributes) //选择属性
4. ->addMinimalPrice()
5. ->addFinalPrice()
6. ->addTaxPercents()
7. ->addAttributeToFilter('test_product', 1, 'left') //属性过滤 具备该属性
8. ->addStoreFilter(); //商店过滤
9.
10. Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
11. //对产品的状态进行可见性过滤 Disable,EnableMage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); //visibility过滤 Catalog,Search,Not Visible$this->_productCollection = $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.


Php代码
​​

​​​

​​​​​


1. protected function _prepareLayout() //面包屑
2. {
3. if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
4. $breadcrumbsBlock->addCrumb('home', array(
5. 'label'=>Mage::helper('catalog')->__('Home'),
6. 'title'=>Mage::helper('catalog')->__('Go to Home Page'),
7. 'link'=>Mage::getBaseUrl()
8. ));
9. }
10.
11. parent::_prepareLayout();
12. }


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.


Php代码
​​

​​​

​​​​​


1. protected function
2. {
3. parent::_beforeToHtml();
4. $toolbar = $this->getToolbarBlock(); //工具条
5. $toolbar->removeOrderFromAvailableOrders('position'); //移除可用的排序
6. return $this;
7. }


protected function _beforeToHtml()
{
parent::_beforeToHtml();
$toolbar = $this->getToolbarBlock(); //工具条
$toolbar->removeOrderFromAvailableOrders('position'); //移除可用的排序
return $this;
}

5. 设定使用的模板


Php代码
​​

​​​

​​​​​


1. $this->setTemplate('catalog/product/edit.phtml'); //设置模板


$this->setTemplate('catalog/product/edit.phtml'); //设置模板
6. 后台修改加入后台的块内容


Php代码
​​

​​​

​​​​​


1. $this->_addContent($this->getLayout()->createBlock('test/adminhtml_catalog_product_edit')); //实体的 不能是布尔的


$this->_addContent($this->getLayout()->createBlock('test/adminhtml_catalog_product_edit')); //实体的 不能是布尔的

7.图片的在php文件调用


Php代码
​​

​​​

​​​​​


1. <img src="<?php echo $this->getSkinUrl('images/tweeticon.jpg');?>"/>


<img src="<?php echo $this->getSkinUrl('images/tweeticon.jpg');?>"/>

8. 得到当前的store的RootCategoryId


Php代码
​​

​​​

​​​​​


1. $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());


$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());

9. 得到catelog


Php代码
​​

​​​

​​​​​


1. $category = Mage::getModel('catalog/category')->load($this->getCategoryId());


$category = Mage::getModel('catalog/category')->load($this->getCategoryId());

10.


Php代码
​​

​​​

​​​​​


1. $collection = Mage::getResourceModel('catalog/product_collection')
2. ->setStoreId($this->getStoreId())
3. ->addCategoryFilter($this);
4.
5.
6. 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:


Php代码
​​ ​​​​ ​​1. // Get the logo from the current store config
2. $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:

Php代码
​​

​​
​​ ​​
1. cale = Mage::getStoreConfig('general/locale/code');
cale = Mage::getStoreConfig('general/locale/code');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!

Php代码
​​

​​
​​ ​​
1. $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

Php代码
​​

​​
​​ ​​
1. $storePhone = Mage::getStoreConfig('general/store_information/phone');
$storePhone = Mage::getStoreConfig('general/store_information/phone');For CMS pages insert the following variable:

Xml代码
​​

​​
​​ ​​
1. {{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:

Php代码
​​

​​
​​ ​​
1. Mage::getStoreConfig($path, $storeCode) // $storeCode is not required
Mage::getStoreConfig($path, $storeCode) // $storeCode is not requiredfor example you need to get value set for store phone number from system config:

Php代码
​​

​​
​​ ​​
1. Mage::getStoreConfig('general/store_information/phone');
Mage::getStoreConfig('general/store_information/phone');Also, you can get configuration value as true or false like this:

Php代码
​​

​​
​​ ​​
1. Mage::getStoreConfigFlag('general/store_information/phone'); // return true
2. Mage::getStoreConfigFlag('general/yes_no/choice'); // true or false based on selection
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

Php代码
​​

​​
​​ ​​
1. /* Sender Name */
2. Mage::getStoreConfig('trans_email/ident_general/name');
3. /* Sender Email */
4. 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

Php代码
​​

​​
​​ ​​
1. /* Sender Name */
2. Mage::getStoreConfig('trans_email/ident_sales/name');
3. /* Sender Email */
4. 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

Php代码
​​

​​
​​ ​​
1. /* Sender Name */
2. Mage::getStoreConfig('trans_email/ident_support/name');
3. /* Sender Email */
4. 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

Php代码
​​

​​
​​ ​​
1. /* Sender Name */
2. Mage::getStoreConfig('trans_email/ident_custom1/name');
3. /* Sender Email */
4. 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

Php代码
​​

​​
​​ ​​
1. /* Sender Name */
2. Mage::getStoreConfig('trans_email/ident_custom2/name');
3. /* Sender Email */
4. 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);

标签:常用,Php,函数,product,catalog,magento,getStoreConfig,email,Mage
From: https://blog.51cto.com/u_5112239/5948919

相关文章