Tag: magento

  • Style Magento checkout form fields without touching the core code

    Although I can change css complete restyle the checkout forms, this is not what I want to discuss today. What I want to achieve are:

    1. fix City required asterisk not showing bug
    2. change Address lines from full width to half width as City

    The City required asterisk is hidden by a javascript wrongly. I found City and State/County/Province are put into one li, i.e. <li><div></div></li>. It might be something should happen on State/County/Province but happens on City. If I close li after City, and reopen it before State/County/Province, bug is fixed. By the way, I hide State/County/Province.

    To show a field in full length, Magento uses
    <li><label><span></span></label><input/></li>
    To show a field in half length, Magento uses
    <li><div><label><span></span></label><input/></div></li>

    So just find the right place to insert <div> and close it for Address lines, I easily style them to the same length of City.

  • Magento idea: business intelligence

    It is very time consuming to set Related Products, Cross Sells, Up Sells on individual product basis, and not accurate if you work them out just using your brain. It is good to see Magento has these marketing terms built in, but I hate to do the job by hand (or brain).

    So I am thinking of developing a business intelligence module for Magento to let computer work Related Products, Cross Sells, Up Sells for us.

    Here is the overview of this module:

    First, we need record more data than what Magento provides. we specify a period of data to data-mine.

    Then, for Related Products, mine product pageviews by each visit. Put a visitor’s all visited products into an array, and count all visitors’ arrays for every product-product combination.

    As for Cross Sells, mine orders received or fulfilled. The algorithm is similar to Related Products.

    As for Up Sells, mine the products were added to cart but dropped from final order. Compare the product dropped with the products in final order, and if the former is of higher value, and some sort related to the latter, we can call the former Up Sells. This is my definition, and the algorithm is some sort complicated.

  • Extend Magento inventory management

    Magento inventory management is very basic – only an Inventory Qty figure. Strictly speaking, it is not an “in-stock” figure, but an “orderable”. Because orders keep coming in but you only do despatch once a day, Magento inventory quantity changes upon order is placed online, so you can not tell how many pieces of a product sitting in the warehouse.

    Use only one figure to manage inventory is not enough. Imagine this scenario:
    You are doing stocktake someday. At the time of last despatch, Magento inventory quantity of product X is 100. Stocktaking takes many hours, and when it finishes, Magento inventory quantity has changed to 80. If you physically count product X as 95, you can not simply update Magento inventory figure to be 95. Instead you should change it to 75 (worked out as 80 – 100 + 95). It is very easy to make such mistakes in stocktaking, but I have only Magento to blame – if stocktaking is based on a figure which is changing from time to time, it is not rock solid.

    However, I am not thinking of introducing a 3rd party inventory management system to work with Magento, largely because I do not know which one can fit.

    I am thinking of a simple extension to Magento – use 3 figures to show stock level from different concerts. Let’s clarify the terminology first.
    Figure 1: inventory orderable, i.e. the existing Magento Inventory Qty;
    Figure 2: inventory in-stock, i.e. how many pieces in the warehouse;
    Figure 3: inventory coming, i.e. expected purchase.

    • When an order is placed, decrease inventory orderable
    • When an order is despatched, decrease inventory in-stock
    • When an order is cancelled before despatch, increase inventory orderable
    • When an order is cancelled after despatch, do nothing (some companies raise RMA at this point but we do not need it.)
    • When an order is returned, if goods in good condition, increase inventory orderable, and increase inventory in-stock
    • When a purchase order is placed (by us to our supplier), increase inventory coming
    • When a purchase order arrives, decrease inventory coming, increase inventory orderable, and increase inventory in-stock
    • When stocktake begins, snapshot inventory in-stock as inventory1
    • When stocktake ends, update inventory in-stock to inventory2, and add (inventory2 – inventory1) to inventory orderable

    If all above events are logged, we have a kind of traceability. The log gives some clue to analyse where “inventory2 – inventory1” is from.

    In case a customer asks “how many you have? I take them all”, we tell him/her inventory orderable.

    In case we need find out “how many on hold (for late despatch)”, we use the balance between inventory orderable and inventory in-stock.

    Inventory orderable (figure 1) is built-in with Magento. Inventory coming (figure 3) is not essential to stock control. We can introduce it after we have implemented inventory in-stock (figure 2).

  • Forget Magento bundle product, for now

    Magento bundle product type 是我想往已久的:如果 A bundle 和 B bundle 都捆绑了某些数量的 simple product,它就可以实现 MRP 中 material consumed 功能。一开始我把所有产品都设为 bundle product,结果发现在后台 create order 时,一个可选产品也没有,原来这里只能把 simple product 加入订单。

    总之,bundle product 的适用性大大有待提高,否则如同鸡肋。目前我全盘放弃了 bundle product,而且在新建产品选择 product type 时,我尽量使用 simple product,毫无疑问 simple product 的适用性是最高的。

  • 5 approaches to customise Magento product

    1. Configurable product. It is a built-in feature of Magento, and very easy to implement. No coding skills required. However, every sub product of the configurable must pre-exist as a simple product in Magento products. It is literally impossible to use it as made-to-order functionality, which aims at taking lots of parameters from customer’s input, calculating prices under millions scenarios.

    2. Product custom options. Again it is a built-in feature of Magento, and easy to implement. But it is labour intensive to set it up if you have thousands of products of same set of custom options. Alternatively, you can buy an extension called Custom Options Template for less than $100. But I do not like data redundancy. Also, custom options on its own can not build the prices on the fly.

    3. Made-to-order extension (MTO) developed by metrofindings.com. It does not require coding, either. I must admit it is a promising extension to some business. However, I think the majority of business pricing models are much more complicated than this MTO module can dealt with. It is not possible to describe pricing just based on xml or cvs, as far as my business is concerned.

    4. My favourite approach version 1.0. It is inspired by MTO module with two major differences.
    a) All raw data are stored in database rather than external files so a backend administrator can manage data changes without uploading xml or cvs files. I think it is a bad idea to mix daily maintenance with programming.
    b) All business logics (cost / profit calculation, supplier screening, etc) fully rely on programming. Because I am a programmer so it is easier for me to control the program to follow my business logics. But if you are not, you’d better find a programmer to do it rather than fiddling with xml. Your business logics are usually more complex than an xml can solve.

    5. My favourite approach version 2.0. When I developed version 1.0, I did not think carefully about how to store custom options. I added fields to quote_item and order_item, and created a session to store what customer’s inputs before they were written to database. Because it was not Magento native way to store options, when it came to reorder, I had to take extra care of those options. So, in version 2.0, I am going to (I have not implemented it) create ONE simple product that will be used for customisation across ALL made-to-order products. This product will be assigned a full range of custom options. If some custom options do not apply to certain made-to-order products, just leave them blank. And most importantly, this product will have a custom option to store sku telling us from which product the customisation is based on. I assume this approach does not break Magento reorder process. I only need to change some view templates to show this special product’s sku, image, description properly.

  • How Magento cache Blocks HTML output

    我曾错误地认为 Magento 安装后,只要启用 cache,Magento 就会缓存页面的大部分内容,比如,cms, product, category 的 content block。

    直到今天第 N 遍看 Magento wiki,才意识到 Magento 初始只启用了很小一部分 block cache。细想之下,Magento 默认不缓存 content block 有一定的道理:各用户对内容缓存的要求不一而足,所以 Magento 把这个问题留给用户自己去思考。

    Block cache 有三个参数,cache_lifetime 顾名思义,cache_tags 关系到 cache 何时更新,cache_key 关系到 cache 有多少个版本。

    在 Magento 目录下所有文件里搜索 cache_tags 这个词,我只发现它只出现在跟 Navigation (产品菜单),Footer (脚注),Adminhtml Menu (后台菜单) 相关的少数文件里。由此说明 Magento 根本没有缓存页面的最主要部分:content block。我联想到很久以前我在 Magento forum 上提问的一个问题:为什么 cache 只用掉了 0.5 MB 内存?当时我用的是memcached,结果热心人来问我 memcached 有没有安装正确啊,php-perl-memcache 有没有安装啊。就是没有人告诉我——一切正常,因为 Magento 尚未缓存页面主要内容

    了解了 Magento cache 机制,再根据自己的实际情况对 product page 缓存 content block 就简单了:只要在 extends Mage_Catalog_Block_Product_View 的基础上加入

        protected function _construct()
        {
            $this->addData(array(
                'cache_lifetime'    => 86400,  //seconds
                'cache_tags'        => array(Mage_Catalog_Model_Product::CACHE_TAG . "_" . $this->getProduct()->getId()),
                'cache_key'         => $this->getProduct()->getId(),
            ));
        }
    
    

    以此类推 cms page content block cache。Category page content block cache 稍微复杂一些,具体去看 Magento wiki。

    设置 content block cache 对速度优化效果显著,我的 product page requests per second 指标提高了约 70%。

    但我还是想让 Magento 跑得再快些,常说的那些 Magento 速度优化结果让我感觉不够畅快淋漓。我有个 page cache 想法,就是把整个页面缓存下来。Nginx 或其他的 web server 都有很好的机制去调度 html cache。据我测试,同一个静态内容的页面,保存为 html (Nginx 直接读取) 比保存为 php (经 php backend on socket or port 读取) 就快好几倍,这个结果让我对 page cache 充满了憧憬。

    如使用 page cache,必须对页面中的 dynamic block (如 sidebar cart,recent viewed/compared products, etc)进行改写,简言之就是 load pages in two stages by ajax。Magento Enterprise Edition 就有 page cache feature,但我不清楚它是不是跟我同个思路。

    与其买个 Magento Enterprise Edition,不过自己动手或请人实现 page cache。如果你恰好跟我有同样想法,请留言。

  • Expires affects Firefox

    我把 Magento 网店从 A 服务器迁移到 B 服务器后,发现一个问题:在 Firefox 下点 “Add to cart”,无论添加是否成功,系统没有提示(购物车有个选项是添加商品后留在当前页面),非得 F5 刷新后才能看到成功或错误信息。Safari 下正常,IE 下未实测。

    我原以为是 Magento cache 的问题,但禁用 Magento cache 后问题依旧。而且,只有 B 服务器才有这样的毛病。想来半天,觉得跟服务器参数有关。

    于是我查看了 Magento 在两台服务器的 http header,发现 B 服务器多了 cache 和 expiry 等 headers。这才特别关注了一下 nginx 的配置问题,发现 B 服务器的 server {} 里有一句 expires 1d; 不知这句是谁写到 nginx conf 文件里。这指名 Firefox 直接从本地读取,除非 F5。去掉 expires 这条目就好了。

  • Limitation of Mage::getModel method

    Magento getModel can not initialise an instance whose class requires more than 1 argument.

    I assume Magento native classes can explode options from the first argument to satisfy getModel. However, if I want to use 3rd party class like Zend_Form_Element inside Magento, there is no ways to use getModel to achieve the same result as

    $element = new Zend_Form_Text('name', $options);
    

    because

    $element = Mage::getModel('moduleName/modelName', 'name');
    

    takes 1 argument which is ‘name’ only. No way to pass $options on.

  • A good practice in naming rewritten Magento modules

    Magento 里的namespace, module name, controller name, action name, class name, function name, model name, router name, xml identifier name, etc, etc 实在太过复杂,凭我的脑袋实在记不全什么时候该大写,什么时候该小写,什么时候该加一个下划线。

    为了不出错,我尽量照抄 Magento source code,比如 extend Mage_Catalog 模块时,我取名 Myname_Catalog。我这个模块名作前缀重写了一些 Model 和 Block,没遇到问题。最近在重写 Controller 时,出问题了——如用/catalog/product/view/id/1 能执行重写后的 controller;但用/seo-product-page-name.html 却执行了重写前的 controller。这问题不一定是命名引起的,但我在寻找解决方案时意识到要有一个好的命名习惯。

    简言之,网上讨论比较多的 Magento upgrade safe 重写规则只适合 Model 和 Block,不适合 Controller。要写出 upgrade safe Controller 也不难,就是谈的人少,不容易找到适用于最新版 Magento 的文档。有空我整理一下如何在 Magento 1.3 下重写 controller,但这不是我今天想说的话题。

    我只想说我在重写 Controller 时意识到命名习惯。命名时肯定是用自己的 namespace,但也建议不使用原模块名。比如要重写 Mage_Catalog,不妨使用 Myname_Catalogue,这样最大程度地避免了可能的名字冲突。

  • It’s time to override Magento controller

    I’m quite happy to override Magento models and blocks. Since overriding controllers is different from overriding models and blocks, I do not want to touch it until recently.

    What I was trying to achieve was a customised product view page which could show different content according to url parameters and login status.

    At first, it was Block detecting url parameters and login status. If the criteria did not meet, Block add an error message and show default content. Then I found this error message would not show up (until next page) because this Block was after Message Block.

    So, for the sake of logic, I have to rewrite the product view controller. And it makes more sense if I conduct a redirect rather than show default content if the criteria do not meet.