Magento去掉价格的小数点

Magento去掉价格小数点
Magento去掉价格小数点

Magento的默认情况,价格后面是有小数点的,我们来看下如何正确的来去掉小数点。

1.复制如下路径的文件 app/code/core/Mage/Directory/Model/Currency.php 到 app\code\local\Mage\Directory\Model\ 文件夹下面,这个文件夹没有自己手动创建下。

2.打开新复制的这个文件 Currency.php

继续阅读“Magento去掉价格的小数点”

Magento清空数据库log和var缓存文件脚本

magento清空log与var脚本
magento清空log与var脚本

Magento的数据库,用久了,很多log表会变的非常巨大,数据库体积也越来越大,这会严重影响到数据库的运行。

此脚本可以自动清空数据库log表。如果你的log表有用,可以按照实际情况修改此脚本。

将文件上传至你的magento文件根目录,然后用如下地址执行清空命令:

清空数据库log表:http://你网站的网址/clear-magento.php?clean=log

清空magento缓存文件:http://你网站的网址/clear-magento.php?clean=var

如果你的数据库log表比较大,那么使用如上命令清log可能会失败,或者用去很长时间,你可以手动清空,在脚本代码中的log表有:

‘dataflow_batch_export’,
‘dataflow_batch_import’,
‘log_customer’,
‘log_quote’,
‘log_summary’,
‘log_summary_type’,
‘log_url’,
‘log_url_info’,
‘log_visitor’,
‘log_visitor_info’,
‘log_visitor_online’,
‘index_event’,
‘report_event’,
‘report_compared_product_index’,
‘report_viewed_product_index’,
‘catalog_compare_item’,
‘catalogindex_aggregation’,
‘catalogindex_aggregation_tag’,
‘catalogindex_aggregation_to_tag’,
‘catalogsearch_query’,
‘catalogsearch_fulltext’,
‘catalogsearch_result’

清空var缓存文件一般不会花费太多时间,缓存文件在脚本中路径分别为:

继续阅读“Magento清空数据库log和var缓存文件脚本”

magento set image fit or fill the parent container

magento图片自适应容器大小
magento图片自适应容器大小

magento原始模板原先自带的图片处理会有白边,就是图片比例不符合代码中的尺寸时,magento会自动resize图片尺寸,并且自动加上白边。

其实去掉白边好搞,增加去白边的代码即可,如下

$this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(210);

keepFrame(FALSE)这个是控制白边的,参数false就是不带白边。resize控制图片的比例,单独一个数字,如上面 200,是将图片搞成200*200的尺寸,这个只有在有白边的时候才有效,没有白边了这个resize就不太管用了,会按照实际比例缩放图片,你可以试试。

对于很多人来说也许去掉白边就够用了,但是有的时候有变态的情况,比如图片比例是1:5甚至1:10,尼玛 – – ! 怎么会有这么变态的图片比例,这会将你的布局搞的乱七八糟。如果你有这个情况,又想控制图片的最大宽高的话,看这篇博文会有所帮助。

首先我们将magento的图片的白边去掉了,下一步就是使图片自适应到你设置的大小,即使比例差别很大,也自适应下,图片不超出你指定的长宽。相当于,你设置了图片的最大宽度,最大高度。这样对于你布局来说,会好狠多。

jquery有很多图片缩放,自适应的插件,我找到的这个是我自己认为比较合适的 http://imgscale.kjmeath.com/,当然大家也可以找自己喜欢的,或者更满足大家自己需求的js插件。

看插件的说明,他要求在图片的外层有个div或者什么的是block类型的html标签,并且css中写上宽高。

继续阅读“magento set image fit or fill the parent container”

Magento获取当前Collection的排序方式

 

汤面和namu
汤面和namu

在Magento中,平时获得collection的时候,我们会根据实际情况设置排序,使用setOrder。如果只是静态的,这个好说,直接写上即可。

现在需要使用ajax的加载product list,实现当前页面动态加载后面页面的内容。这个时候页面排序和排序方式用户可以自己选择的,就需要动态的获取,当前的排序方式。

参考代码如下

$_productCollection = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('visibility', $visibility)
                .............
                ->setStoreId($storeId)
                ->addStoreFilter($storeId)
                ->addCategoryFilter($_featcategory) //feature category to list first
                //set order as CurrentOrder
                ->setOrder(Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentOrder(), Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentDirection())
                .......................

代码中如下两行分别获取order和direction,动态的设置collection

Mage::getBlockSingleton(‘catalog/product_list_toolbar’)->getCurrentOrder();
Mage::getBlockSingleton(‘catalog/product_list_toolbar’)->getCurrentDirection();

继续阅读“Magento获取当前Collection的排序方式”

Magento sql升级脚本加入新的字段add column

magento add column
magento add column

最近使用了magento的商品品牌插件,需要在此插件的数据库表中添加几个字段。

首先需要修改插件的版本号,以便magento发现插件版本升级了,运行升级脚本。

版本号在插件的ect/config.xml文件中,sql脚本示例mysql4-upgrade-0.1.0-0.1.1.php如下

<?php

$installer = $this;

$installer->startSetup();

$installer->getConnection()->addColumn(
    $installer->getTable('brand'),
    'country',
    "varchar(255) NULL"
);
$installer->getConnection()->addColumn(
    $installer->getTable('brand'),
    'role',
    "varchar(255) NULL"
);

$installer->endSetup();

继续阅读“Magento sql升级脚本加入新的字段add column”

Magento在product view页面ajax显示tag的商品

magento ajax获取tag商品
magento ajax获取tag商品

Magento的tag是点击后,有一个tag关联商品的list页面。客户需要在商品详情页面product view中点击tag使用ajax直接显示商品。

首先我们需要写个小插件,为ajax提供数据

Controller的参考代码如下:

$tagId = $this->getRequest()->getParam('tagId');
        $tag = Mage::getModel('tag/tag')->load($tagId);

        if (!$tag->getId() || !$tag->isAvailableInStore()) {
            $this->_forward('404');
            return;
        }
        $page_size = 15;
        $page = 1;
        /* $tagModel = Mage::getModel('tag/tag');
          $productCollection = $tagModel->getEntityCollection()
          ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
          ->addTagFilter($tagId)
          ->addStoreFilter(Mage::app()->getStore()->getId())
          ->addMinimalPrice()
          ->addUrlRewrite()
          ->setActiveFilter()
          ->setPageSize($page_size)
          ->setCurPage($page);
          Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($productCollection);
          Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($productCollection);
         */
        $productIds = $tag->getRelatedProductIds();
        $productCollection = Mage::getModel('catalog/product')->getCollection()
                ->setStoreId($storeId)
                ->addAttributeToSelect('*')
                ->addFieldToFilter('entity_id', $productIds)
                ->setPageSize($page_size)
                ->setCurPage($page);

        $html = '';
        foreach ($productCollection as $product) {  //to get all product list
            $html .= '<div class="item">';
            //START
            $p_url = $product->getProductUrl();
            $p_name = $product->getName();
            $p_img_url = Mage::helper('catalog/image')->init($product, 'small_image')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->resize(200, null);
            $p_final_price = Mage::helper('core')->currency($product->getFinalPrice(), true, false);

            $html .='<a class="product-image" href="' . $p_url . '"><img src="' . $p_img_url . '" width="200" /></a>';
            $html .='<h2 class="product-name" class="product-name"><a href="' . $p_url . '">' . $p_name . '</a></h2>';
            $html .='<div class="price-box"><span class="regular-price"><span class="price">' . $p_final_price . '</span></span></div>';
            //END
            $html.='</div>';
        }

        //var_dump($productCollection);
        echo $html;

模板文件中的ajax获取Controller返回商品信息的参考代码如下

继续阅读“Magento在product view页面ajax显示tag的商品”