Home > php教程 > php手册 > magento commonly used functions, magento commonly used functions

magento commonly used functions, magento commonly used functions

WBOY
Release: 2016-07-06 14:25:06
Original
1270 people have browsed it

magento 常用的函数,magento常用函数

1.Magento eav_attribute表中source如何指定自定义数据来源
  如果你引用的类名为yebihai_usermanage_model_entity_school你必须完整的给出地址,不能usermanage/entity_school,这样默认是在Mage下面去找的。
  如:

$setup->addAttribute('customer', 'school', array( 'type' => 'int', 'input' => 'select', 'label' => 'School', 'global' => 1, 'visible' => 1, 'required' => 0, 'user_defined' => 1, 'default' => '0', 'visible_on_front' => 1, 'source'=> 'yebihai_usermanage_model_entity_school', //数据来源,text留空即可 ));


2.Magento getPrice()的结果小数点位数的处理
echo Mage::helper('core')->currency($_product->getPrice()); 
输出格式:888.673 => 888.67

3.Magento config.xml中global节点中的block重写与blocks下面的命名标签必须小写,如:


                                                Yebihai_CategoryList_Block_Category_View                                                                              Yebihai_CategoryList_Block                            


4.Magento获取列表当前排序方式ASC or DESC?
获取当前排序:$this->getAvailableOrders()
获取当前分页:$this->getCurrentPage()
列表页的各种内容获取都在:Mage_Catalog_Block_Product_List_Toolbar这个类里面,有需要直接去这里面找。

5.Magento Collection添加排序?

$subCategories = Mage::getModel('catalog/category')->getCollection(); $subCategories->setOrder('position', 'ASC');


6.Magento Collection where里面的或条件如何实现?

$result = $selfread->select()->from( $table, array('id'))  ->where( 'reid = '.$reid,'topid ='.$reid);//reid=$reid 或 topid=$reid


7.Magento操作某条数据下面的多个字段,使用场景如下:
本 人在做订单备注的时候在监听类里面通过Magento系统的addStatusHistoryComment方法把订单内容能成功写入 sales_flat_order_status_history表,但是我的需求是还要修改is_visible_on_front此字段的值,让内容 在前台可见。头痛的问题来了,想了各种方法最后按下面的方式解决了。
监听类全部源码:

class Yebihai_CustomerOrderComments_Model_Observer   { public function setCustomerOrderComments(Varien_Event_Observer $observer) { $_order = $observer->getEvent()->getOrder(); $_request = Mage::app()->getRequest(); $_comments = strip_tags($_request->getParam('customerOrderComments')); if(!empty($_comments)){ $_order->setCustomerNote('
订单备注: ' .$_comments); $_order->addStatusHistoryComment('订单备注: ' .$_comments)->setIsVisibleOnFront(1); } return $this; } }

8.Magento个人中心左侧菜单控制
关于个人中心的主要功能都是在customer这个模块进行,需要修改相应的功能,直接去你的模板customer文件夹去修改。
左侧菜单模板路径:customer/account/navigation.phtml
9.Magento把html转换为转义字符,用什么方法?
core助手里面有一个escapeHtml方法,使用如下:
Mage::helper('core')->escapeHtml("yebihai 加油

go
");
The actual location of the method: in the Mage_Core_Helper_Abstract class.
ps: Some common operation methods are encapsulated in the core module. If necessary, you can analyze the source code.

10.Magento dynamically creates blocks and references actions?

The following is the layout (Layout) configuration file of one of my modules. I now need to dynamically call checkoutcart through Ajax. Direct calling is definitely not possible. How to solve it? Yes?

          simplecheckout/cart_item_renderer                            
Step 1: Call a custom controller through ajax, such as:
jQuery.post('getUrl('gouwuche/cart/updateQuickShoppingCar') ?>', function(data){ jQuery('#shopping-cart-table thead').after(data); }); Step 2: Dynamically create a block in the controller method, such as: public function updateQuickShoppingCarAction(){ $block = $this->getLayout()->createBlock('checkoutrewrite/quickcart')->setTemplate('quickbuy/cart/cart.phtml'); echo $block->toHtml(); }
Step 3: Create a new block file (quickcart), and initialize the action content in the configuration file in the construct method in this file, such as:
public function __construct() { parent::__construct(); $this->addItemRender('simple', 'checkout/cart_item_renderer', 'quickbuy/cart/item/item_view.phtml'); }

PS: When proceeding to the second step, the cart.phtml template has been loaded. The third step is just to load the action under the cart block.

11. What should we pay attention to in the parameters of Magento getTable method?
Instance, the method of querying the specified table and conditions in the database is as follows:

public function getExcelKucunJiage($id,$status){ $selfread = $this->_getConnection('excelmanage_read'); $table = $this->getTable('excelmanage/excelkucunjiage'); $result = $selfread->select() ->from( $table ) ->where( 'excel_id=?', $id) ->where( 'is_active=?', $status); return $selfread->fetchRow($result); }

The parameter settings of the getTable method need to be noted as follows. excelmanage is the name of your module, and excelkukunjiage is the name of the entity node you operate. My entity configuration is as follows:

Yebihai_ExcelManage_Model_Resource_Mysql4 excelkucunjiage

The parameters after "/" are derived from the entity names in front of the table.

12. How to update the specified ID information in the data table?
$excelModel = Mage::getModel('excelmanage/excelkukunjiage')->load(1);
$excelModel->setExcelAdddate(Mage::getModel('core/date')- >timestamp(time()));
$excelModel->setIsActive(0);
$excelModel->save();
The above code is to modify the data table information with ID 1 .
Extension: How to add and modify specified table information in Magento?

13. How to change the default sorting field of product list?
Set the path at: System-->Directory-->Advanced Product Management-->Default List Status

14. Get the number of items in a data set?

Get the number of _productCollection data sets, the case is as follows:

$this->setStoreId($storeId);

 $this->_productCollection = Mage::getResourceModel('catalog/product_collection'); //Get the data set

$this->_productCollection = $this->_productCollection->addAttributeToSelect('*')

->addAttributeToSelect('manufacturer') //Add query attribute

->setStoreId($storeId) //Set the store

 ->addAttributeToFilter('cuxiaobiaoqian',array('eq'=>39)) //Attribute filtering specification

->addStoreFilter($storeId) //Add store filter conditions

 ->setPageSize(6); //Get the number of items

15. Query the specified data table through the select() method. How to control the number of items read?

The code application background is as follows:

 $selfread = $this->_getConnection('yafo_bbs_setup'); //Database connection object

$table = $this->zixunTablePrefix."forum_post"; //Table to be queried

 $result = $selfread->select()->from( array('a'=>$table), array('tid','subject')) //Specify the table and the object to be queried Field

 ->limit($size) //Read the specified number of items

->order("a.dateline DESC") //Specify sorting conditions

 ->where( $selfwhere ); //Add filter conditions

Return $selfread->fetchAll($result); //Return query results

16. Modify the specified product price and group price (code operation)?

$selfPrc = Mage::getModel('catalog/product')->load(614); $selfData = $selfPrc->getData(); $selfData['price'] = 25; $selfData['group_price'] = array( 0 => Array(                                                        "website_id" => 0,                            "all_groups" => 0,                            "cust_group" => 0,                            "price" => 97.0000,                            "website_price" => 97.0000                        ),                      1=> Array                        (                            "website_id" => 0,                            "all_groups" => 0,                            "cust_group" => 1,                            "price" => 27.0000,                            "website_price" => 27.0000                        ),                      2=> Array                        (                            "website_id" => 0,                            "all_groups" => 0,                            "cust_group" => 2,                            "price" => 17.0000,                            "website_price" => 17.0000                        ),                      3=> Array                        (                           "website_id" => 0,                            "all_groups" => 0,                            "cust_group" => 3,                            "price" => 67.0000,                            "website_price" => 67.0000                        ),                      4=> Array                        (                            "website_id" => 0,                            "all_groups" => 0,                            "cust_group" => 4,                            "price" => 66.0000,                            "website_price" => 66.0000                        )); $selfPrc->setData($selfData); $selfPrc->save();


17.修改指定产品库存(代码操作)?

$selfPrc = Mage::getModel('catalog/product')->load(614); $aa = Mage::getModel("cataloginventory/stock_item")->loadByProduct($selfPrc); $selfaa = $aa->getData(); $selfaa['qty'] = 23; $aa->setData($selfaa); $aa->save();


18. How to output sql statement
$result = $selfread->select()->from(array('ft'=>$flatTable),array ())

->join(array('pc'=>$prcCategory),'ft.entity_id=pc.entity_id',array('pc.value')) ->where( 'ft.attribute_set_id=?', $attsetid) ->where( 'pc.attribute_id=?', $attid) ->group("pc.value"); //echo $result; exit;//Output sql statement


19. Background form configuration, how to add comments in the code?

$fieldset->addField('dict_grade', 'select', array( 'name' => 'dict_grade', 'label' => Mage::helper('catalogsearchrewrite')->__('Advanced Search Ciku Manage Grade'), 'title' => Mage::helper('catalogsearchrewrite')->__('Advanced Search Ciku Manage Grade'), 'type' => 'options', 'options' => Mage::getSingleton('catalogsearchrewrite/cikumanage')->getCikuGradeOptionArray(), 'after_element_html' => Mage::helper('catalogsearchrewrite')->__('Keywords Grade Description.'), //after_element_htmlThis attribute is used to add comments 'required' => true, ) );


20. Instantiate the model and how to get the value of the specified content of the specified field through the load method?
$dictModel=Mage::getModel('catalogsearchrewrite/cikumanage')->load($dictname,'dict_name'); //Parameter 1: Specify the value, Parameter 2: Specify the field

$dictModel->getDictName(); //Get the returned specified field value

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template