Tag: magento

  • A tool to synchronise Magento database between servers

    我时不时需要在测试服务器上加载生产服务器的实时数据,以前都是把数据下载到本地的测试服务器后,手工键入一些命令完成数据加载,每次都要花费几分钟时间。为了避免一再“浪费”这几分钟,我今天一次性投入了几小时完成了一个 php 脚本。虽然这是为 magento 的数据迁移而写的脚本,但我写完一看,用在其他地方也是可以的。

    为了安全起见,该脚本是用 php 命令行运行的,所有输出针对 terminal 美化,不是 browser。保存源码为 data_mover.php,同一目录下要有 mysqldump 得到的经 gzip 的 sql 文件,文件名以 FILENAME_PREFIX 开头,以 .sql.gz 结尾。启动时只需键入

    /path/to/php -f data_mover.php

    即可。

    初始化 PDO 对象时,按理只需要 host=localhost,不需要 unix_socket=MYSQL_SOCKET。但奇怪的是,如果通过 apache 调用本程序(虽然不是本程序的初衷,但我希望它在浏览器下也能运行),仅指定 host=localhost 作 PDO __construct() 参数,会产生一个莫名其妙的错误:

    SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

    似乎是 PDO bug。可以通过 host=127.0.0.1 或者追加 unix_socket=MYSQL_SOCKET 来避免。如果通过 php 命令行启动则没有这个问题。

    而后,还涉及怎么删除所有数据表的问题。看似简单的一个问题,我发现 mysql 竟然没有一个类似于 DROP/TRUNCATE TABLE * 单行命令。于是除了本脚本用的方法外,我还想了不下两种办法:

    其一,删掉整个数据库重新创建。
    mysqladmin -f -h localhost -u (DB_ROOT_USERNAME) -p(DB_ROOT_PASSWORD) drop (DB_NAME)
    mysqladmin -h localhost -u (DB_ROOT_USERNAME) -p(DB_ROOT_PASSWORD) create (DB_NAME)
    但这需要比数据表操作更高权限的用户,在这个无关大局的脚本里去使用高权限的用户的密码,实在非我所愿。

    其二,是我 google 来的,方法很巧,但很可惜,因为 foreign keys 的存在,运行这条命令会出错。
    mysqldump -u (DB_USERNAME) -p(DB_PASSWORD) –add-drop-table –no-data (DB_NAME) | grep ^DROP | mysql -u (DB_USERNAME) -p(DB_PASSWORD) (DB_NAME)

    
    <?php
    
    define('MYSQL_SOCKET', '/path/to/mysql/socket');
    define('DB_NAME', 'db_name');
    define('DB_USERNAME', 'db_username');
    define('DB_PASSWORD', 'db_password');
    define('FILENAME_PREFIX', 'filename_prefix');
    define('TEST_URL', 'http://test.domain/');
    
    if ($handle = opendir(dirname(__FILE__))) {
    $found = false;
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
    if (substr($file, 0, 12) == FILENAME_PREFIX && substr($file, -7) == '.sql.gz') {
    if ($found) {
    //compare which one is newer
    if (filemtime($file) > filemtime($fileFound)) {
    $fileFound = $file;
    }
    }
    else {
    $found = true;
    $fileFound = $file;
    }
    }
    }
    
    if ($found) {
    echo "Found the newest file $fileFound, and will work on it.\n";
    try {
    $pdo = new PDO(
    'mysql:host=localhost;dbname=' . DB_NAME . ';unix_socket=' . MYSQL_SOCKET,
    DB_USERNAME,
    DB_PASSWORD,
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
    );
    echo "Connected to database.\n";
    }
    catch (Exception $e) {
    die ("Error occurred when connecting to database. Quoting error message: " . $e->getMessage() . "\n");
    }
    $sql = 'SET FOREIGN_KEY_CHECKS = 0';
    $pdo->prepare($sql)->execute();
    
    /* query all tables */
    $sql = "SHOW TABLES FROM ". DB_NAME;
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $result = $stmtDale->fetchAll(PDO::FETCH_COLUMN, 0);
    $magentoTableNames = array();
    /* add table name to array */
    foreach ($result as $tableName) {
    if (substr($tableName, 0, 8) == 'magento_') {
    $magentoTableNames[] = $tableName;
    }
    }
    
    //drop all magento tables
    $count = count($magentoTableNames);
    if ($count > 0) {
    $sql = 'DROP TABLE '. implode(',', $magentoTableNames);
    $pdo->prepare($sql)->execute();
    echo "Found and dropped $count magento tables.\n";
    }
    else {
    echo "No existing magento tables found.\n";
    }
    
    //import data via pipe
    echo "Importing data. It may take a while...\n";
    $output = shell_exec("gunzip < $fileFound | mysql -h localhost -u " . DB_USERNAME . " -p" . DB_PASSWORD . " " . DB_NAME);
    echo "Importing data completed.\n";
    if ($output) {
    echo "This is output during data import:\n$output\n";
    }
    
    //after import, change some data for test domains
    $sql = 'UPDATE magento_core_config_data SET value=? WHERE scope=? AND scope_id=? AND path=?';
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array(TEST_URL, 'default', 0, 'web/unsecure/base_url'));
    $stmt->execute(array(TEST_URL, 'default', 0, 'web/secure/base_url'));
    $stmt->execute(array(TEST_URL, 'websites', 2, 'web/unsecure/base_url'));
    $stmt->execute(array(TEST_URL, 'websites', 2, 'web/secure/base_url'));
    echo "Config data changed to fit testing environment.\n";
    echo "All missions completed.\n";
    }
    else {
    echo "File not found. What else can I do?\n";
    }
    closedir($handle);
    }
    else {
    echo "File access not permitted.\n";
    }
    
    
  • Magento Googlecheckout module ignore free shipping shopping cart price rule

    We charge a fixed shipping rate for subtotal under a certain amount, and offer free shipping to orders over this threshold.

    To achieve that, initially I created a shopping cart price rule in Magento promotions, and specified free shipping conditions and actions. Then I enabled Flatrate as a shipping method. This shipping method working in conjunction with the promotion rule served me well until I find it does not work for Google Checkout as a payment method.

    Free shipping promotion conditions
    Free shipping promotion conditions
    Free shipping promotion actions
    Free shipping promotion actions

    Magento Googlecheckout always charges that fixed shipping rate regardless of the promotion rules. I assume it is a bug. To avoid the bug, I delete the free shipping promotion rule and enable Tablerates to replace Flatrate. I can not fix this bug. The bug exists in Magento 1.4.0.1. Giving time it might be fixed by Magento core team.

  • How to override abstract class in Magento?

    If you are googling “override abstract class in magento” to find my blog, probably you already tried Mage::getModel(‘module/model_abstract’) as it worked for instantiatable classes.

    The scenario when requires override an abstract class is overriding isActive() method in Mage_Shipping_Model_Carrier_Abstract. If Mage_Shipping_Model_Carrier_Abstract can be overridden, all carrier methods’ isActive logic can be changed without changing each instantiatable class. However, whether Magento overriding mechanism can work has a prerequisite –

    All instantiation of parent class (in Mage namespace, to be overridden) are using Mage::getModel(‘module/model_class’), or $layout->getBlock(‘module/block_class’), or Mage::helper(‘module/helper_class’) instead of new Class_Name(). This guarantees Mage is the single entry point of class instantiation, then Mage Config can always instantiate an overridden class if any.

    The codes coming with Magento follow this rule to the maximum. But when it comes to class inheritance, the rule is broken. Say Mage_Class_A extends Mage_Abstract_B, Mage_Class_A makes reference to Mage_Abstract_B using normal php syntax. It means even if Mynamespace_Abstract_B is overriding Mage_Abstract_B, Mage_Class_A still inherit from Mage_Abstract_B.

    Come back to the question: How to override abstract class in Magento? Use Mage_Shipping_Model_Carrier_Abstract for example, to override isActive() method, you need to override all chid classes, e.g. Mage_Shipping_Model_Carrier_Flatrate, Mage_Shipping_Model_Carrier_Freeshipping, Mage_Shipping_Model_Carrier_Tablerate, etc.

    You may not satisfied with the answer simply because you do not want to override every carrier class. Let me raise the question again: Is there another way to override abstract class in Magento? Yes! Copy Mage_Shipping_Model_Carrier_Abstract from app/code/core/Mage/Shipping/Model/Carrier to app/code/local/Mage/Shipping/Model/Carrier, do NOT change the class name, just change or add the methods as you need. This is a trick. Magento loads a class from several locations, and app/code/local comes before app/code/core. That’s why it works.

    By the way, if php_apc is running, you must force apc to refresh for new class created in include_path. Please refer to “when does php apc refresh cache data” for details.

  • Magento meta keywords field name inconsistent

    I don’t know whether Magento did this on purpose. Both of category page and product page have a meta keywords attribute. Because meta keywords attribute of category or product belongs to EAV system, it has two entries in eav_attribute table, one for catalog_category (entity_type_id = 3) and one for catalog_product (entity_type_id = 4). However, if you look closely at attribute_code, you will find –

    • for category, the code is meta_keywords
    • for product, the code is meta_keyword

    The cms_page also has meta keywords, but it does not belongs to EAV system. The field name is meta_keywords.

    In summary, you won’t retrieve any meta keywords of a product if you write code like this:

    Mage::getModel('catalog/product')->load($productId)->getData('meta_keywords');
    
    
  • Magento code snippet

    Magento provide a beautiful GUI, which let you setup or change nearly everything. However, occasionally you prefer do it with php code. Here are a collection of codes for various functionalities. I will keep editing this post to add new methods.

    Read if a product attribute is “Use Default Value” in a Store View:

    Assume the storeview ID is $storeId, the product attribute code is $attributeCode;

    $result = Mage::getModel(‘catalog/product’)->setStoreId($storeId)->getAttributeDefaultValue($attributeCode);

    $result === false when the attribute IS “Use Default Value”;

    $result === Mage::getModel(‘catalog/product’)->setStoreId(0)->getAttributeDefaultValue($attributeCode) when the attribute HAS storeview specific value.

    Please note even if the attribute is “Yes/No” choice type, the attribute value is 1 or 0. So there is no conflict – when getAttributeDefaultValue returns false means “Use Default Value”; when it returns 0 means “default value is No”.

    Read storeview specific value to a product attribute:

    public function getAttributeOptionValue($optionId, $storeId = 0, $attributeId = null)
    {
    	$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
    		->setStoreFilter($storeId, false)
    		->addFieldToFilter('main_table.option_id', $optionId)
    		->setAttributeFilter($attributeId);
            return $valuesCollection->getFirstItem()->getValue();
    }
    

    CRUD manipulation on entity attributes, such as product attribute

    All need to do is construct an array in the right structure, add to the attribute, and save. Do not operate on ‘eav/entity_attribute_option’ directly. It won’t work because relationship between table eav_attribute_option and eav_attribute_option_value is not set in this model.

    Add options to attribute

    $attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
    $data = array(
    	'option' => array(
    		'value' => array(
    			$optionId => array(
    				$storeId => $value
    			)
    		)
    	)
    );
    $attribute->addData($data)->save();
    

    Bear in mind, as this is to add a new option, you do not need to specify an actual $optionId. It can be any string which, if convert to number, must be 0. If $optionId’s numeric value is positive, and if that option does not exist or does not belong to the $attribute, it will throw exception like:

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails.

    You can construct an array with more than one option ID’s and store ID’s inside to add multiple options or give option values to multiple stores at a time.

    Read options

    $attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
    $optionCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
    	->setStoreFilter($storeId, false)
    	->setAttributeFilter($attribute->getId());
    foreach ($optionCollection as $option) {
    	echo $option->getValue();
    }
    

    The code is straight forward. I did not find another way retrieving options. I tried
    $attribute->getOptions() and $attribute->getOption(). Neither worked.

    Delete options

    $attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
    $data = array(
    	'option' => array(
    		'value' => array(
    			$optionId => ''
    		),
    		'delete' => array(
    			$optionId => 1
    		)
    	)
    );
    $attribute->addData($data)->save();
    

    The tricky thing is: the $optionId to delete must exist in both ‘value’ and ‘delete’, although the value itself is not critical (because it will be deleted).

    Update options

    $attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
    $data = array(
    	'option' => array(
    		'value' => array(
    			$optionId => array(
    				$storeId => $value
    			)
    		)
    	)
    );
    $attribute->addData($data)->save();
    

    It is the same code as adding options. The difference is in $optionId. You need work out the existing option ID.

    CRUD manipulation does not need to construct $data with the complete list of existing options. Any options not in $data is untouched.

  • Magento extension: Root Category As Homepage

    It is my first time publish an Magento extension – Qian_Rcah. By default, Magento prevent root category from being shown at frontend, not to say use it as homepage. I do not see significant security leaks if showing root category. There are some benefits using root category as homepage:

    • Layered navigation starts with a complete product collection on site.
    • Selective products show on homepage without prerequisite knowledge of cms block.

    Download RootCategoryAsHomepage.tar.gz

    How to install:
    Copy the whole “app” folder to magento/app. That’s it.

    How to use:
    Log in Magento backend, go to System -> Configuration -> General -> Web -> Default Pages, change “Default web URL” to “rcah” (default is cms). Besides of root category, this module can load an arbitrary category page as homepage. In this case, change “Default web URL” to “rcah/index/index/id/ARBITRARY_CATEGORY_ID”.

    How is it created:
    This module is created by koukou1985’s module creator (http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table). Thanks to koukou1985.

    How good is it:
    This module does not override any Mage classes. It is safe to use with other extensions as long as namespace and module name do not conflict.

  • How can Magento generate sitemap.xml belongs to root?

    今天看到 magento 目录下的 sitemap.xml 的属主和属组分别是 root:root,属性是644,而这一台的 webserver 是以 apache:apache 身份运行的,所以我觉得很奇怪:

    • apache 怎么能建立一个 root:root 的文件?
    • 如果这个文件不是 apache 建的,apache 怎么有权去更新它(最近更新就在今天)?

    想了好一会明白过来,sitemap.xml 是 cron job 建的,crontab 这么写

    */10 * * * * /usr/bin/php -f /path/to/magento/cron.php

    那么 sitemap.xml 归 root:root 所有就不奇怪了。

    我想这样写会好些:

    */10 * * * * sudo -u apache -g apache /usr/bin/php -f /path/to/magento/cron.php

    2010年6月29日更新:上行命令不对,设想在 cron job 里以 apache 身份运行 cron.php,但出错。正确的写法应该是:

    */10 * * * * su -c '/usr/bin/php -f /path/to/magento/cron.php' -s /bin/bash apache

  • Google do not need windows. Neither do I

    Magento grid is flexible to customise. A lot of options can be controlled by addColumn(). I knew the parameters could be header, width, type, index, but I just found a new one sortable. I am thinking where I can get a complete list of parameters for addColumn(). The best way I can imagine is searching magento source and find the file which contains all words of “header width type index sortable”. File search in eclipse can not do this kind search, so I am thinking google desktop search.

    I have not used google desktop search for a while. Last time I used it, it only has windows version. Today I am glad find google desktop search linux version is available. Up to now, my favourite google programmes, chrome, picasa, and google desktop, all embrace linux users.

    It reminds me a piece of news that google stop offering staff windows as operation system. I regard this is competition between google and microsoft, none of my business. However, google makes programmes independent of windows, which really benefits me.

    What else left in windows which has to be in windows?

    • IE: it is shame our Chinese banks’ website only support IE;
    • MS Office: how many advanced users are using Office features which are exclusive to MS Office? Not many.
    • Photoshop: it’s a pity Gimp still can not beat photoshop at moment.
  • Best place to put module installation scripts in Magento

    Magento module 中,在哪里放置安装脚本比较好?

    当然,把它放在 sql/$resourceName 目录下 $resModel.’-‘.$actionType.’-(.*)\.(sql|php) 文件里是不错的。安装脚本通常是修改数据库结构,所以放在.sql文件理所当然。但如果安装脚本跟数据库无关,放在.sql略显勉强,为此,我找了一个更好的地方。

    首先在 config.xml 声明一个 setup 类:

    
    <config>
    
    <resources>
    <mymodule_setup>
    <setup>
    <module>Mynamespace_Mymodule</module>
    <class>Mynamespace_Mymodule_Model_Setup</class>
    </setup>
    </mymodule_setup>
    </resources>
    </config>
    
    

    这个类只要 extends Mage_Core_Model_Abstract,不一定得 extends Mage_Core_Model_Resource_Setup。

    然后在该类里放置 applyUpdates() 方法,把安装脚本都写到这个方法里就可以了。模块在安装时就是执行这个方法,而且是在 .sql 执行之前。

  • What if I change the EAV attribute backend type in Magento?

    The question is raised because in my catalog_category, I have a user attribute originally 255 length varchar. Now I am told by users it was not long enough for block text. I want to change it to text type in mysql to solve the problem.

    I know EAV attributes use different tables for different type. My worry is moving attribute value from varchar table to text table when changing the EAV attribute backend type. Anyway, I have to try before I know. Here is my steps:

    1. go to table magento_eav_attribute, find the attribute, change backend_type from varchar to text
    2. (what’s next?) I am about to change something else, but surprisingly, nothing else! All done after step 1.

    I do not need to move old values from magento_catalog_category_entity_varchar to magento_catalog_category_entity_text. After change attribute’s backend_type, Magento can still read existing value from magento_catalog_category_entity_varchar, but if I save the category, the value will save to magento_catalog_category_entity_text. Old value is not deleted from magento_catalog_category_entity_varchar, but it does no harm.

    I am curious at how Magento did it – Does Magento go through 5 tables (datetime, decimal, int, text, varchar) to get a value? Or Magento cache attributes’ backend type (so even I make changes in magento_eav_attribute, Magento still have records of old type)?