Table of Contents
magento 常用的函数,magento常用函数
Home Backend Development PHP Tutorial Magento commonly used functions, magento commonly used functions_PHP tutorial

Magento commonly used functions, magento commonly used functions_PHP tutorial

Jul 12, 2016 am 08:50 AM
magento function Commonly used use

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1135808.htmlTechArticlemagento commonly used functions, magento common functions 1. How to specify a custom data source in the source in the Magento eav_attribute table if you quote The class name is yebihai_usermanage_model_entity_school you...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

Advanced usage of reference parameters and pointer parameters in C++ functions Advanced usage of reference parameters and pointer parameters in C++ functions Apr 21, 2024 am 09:39 AM

Reference parameters in C++ functions (essentially variable aliases, modifying the reference modifies the original variable) and pointer parameters (storing the memory address of the original variable, modifying the variable by dereferencing the pointer) have different usages when passing and modifying variables. Reference parameters are often used to modify original variables (especially large structures) to avoid copy overhead when passed to constructors or assignment operators. Pointer parameters are used to flexibly point to memory locations, implement dynamic data structures, or pass null pointers to represent optional parameters.

See all articles