Magento 目录添加自定义属性例子

如何给magento的产品分类创建一个自定义属性?

在根目录创建个脚本文件,内容:

require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
$attribute  = array(
    'type' => 'int',
    'label'=> 'Discount(%)',
    'input' => 'text',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => true,
    'default' => "",
    'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'category_attribute_code
', $attribute);
//$installer->removeAttribute('catalog_category', 'category_attribute_hottest');
$installer->endSetup();

自定义属性的label和category_attribute_code自己更改。

脚本跑一遍,就在magento后台分类编辑页面的General Information组里新加了自定义属性。

Magento paypal express快速结帐运费丢失的问题

Magento设置Express paypal,在产品详细页面或者购物车页面点击

Magento express paypal

直接跳转到Paypal结帐页面的时候没有相关运费,而正常的结帐流程运费计算没有问题。

以默认的运输方式Flat Rate来说,可以重写方法(模块重写自己搞定 LOL):

app/code/core/Mage/Paypal/Model/Express/Checkout.php @function returnFromPaypal

                if ($this->_api->getShippingRateCode()) {
                    if ($code = $this->_matchShippingMethodCode($shippingAddress, $this->_api->getShippingRateCode())) {
                         // possible bug of double collecting rates :-/
                        $shippingAddress->setShippingMethod($code)->setCollectShippingRates(true);
                    }
                }

之后加入:

                if(empty($code)){
                    $code    = 'flatrate_flatrate
';
                    $shippingAddress->setShippingMethod($code)->setCollectShippingRates(true);
                }

$code的值可改为各自的首选默认运输方式。

经测试情况,这样处理可以解决我这边Magento express结帐paypal丢失运费
的问题。

Magento联系我们添加上传文件当邮件附件

Magento联系我们表单里添加上传附件

简单修改几步,即可让magento的联系我们可以上传文件作为发送邮件的附件。

步骤:

1. 修改模板文件“template/contacts/form.phtm”,

form修改为

<form action=
"<?php echo $this->getFormAction(); ?>"
id=
"contactForm"
method=
"post"
enctype=
"multipart/form-data"
>



再在comment文本框的下面添加

<li>
    <label for="attachment"><?php echo Mage::helper('contacts')->__('Attachment') ?></label>
    <div class="input-box">
        <input name="MAX_FILE_SIZE" type="hidden" value="2000000" />
        <input name="attachment" id="attachment" class="input-text" type="file" />
    </div>
</li>

2. 添加控制器逻辑代码。

自定义一个新模块,在新模块的配置文件/app/code/local/CsjCg/Customcontact/etc/config.xml里定义如下:

<frontend>
    <routers>
        <contacts>
            <args>
                <modules>
                    <csjcg_customcontact before="Mage_Contacts">CsjCg_Customcontact</csjcg_customcontact>
                </modules>
            </args>
        </contacts>
    </routers>
</frontend>



模块的controller文件/app/code/local/CsjCg/Customcontact/controllers/IndexController.php

<?php
/**
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @author      Sjolzy <chen@sjolzy.cn>
 */
require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';
class CsjCg_Customcontact_IndexController extends Mage_Contacts_IndexController
{
    public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);
 
                $error = false;
                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                }
                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;
                }
                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }
                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }

                /**************************************************************/
                $fileName = '';
                if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') {
                    try {
                        $fileName       = $_FILES['attachment']['name'];
                        $fileExt        = strtolower(substr(strrchr($fileName, ".") ,1));
                        $fileNamewoe    = rtrim($fileName, $fileExt);
                        $fileName       = preg_replace('/\s+', '', $fileNamewoe) . time() . '.' . $fileExt;
                        $uploader       = new Varien_File_Uploader('attachment');
                        $uploader->setAllowedExtensions(array('doc', 'docx','pdf','xls', 'xlsx','csv'));
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        $path = Mage::getBaseDir('media') . DS . 'contacts';
                        if(!is_dir($path)){
                            mkdir($path, 0777, true);
                        }
                        $uploader->save($path . DS, $fileName );
                    } catch (Exception $e) {
                        $error = true;
                    }
                }
                /**************************************************************/
 
                if ($error) {
                    throw new Exception();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
 
                /**************************************************************/
                //sending file as attachment
                $attachmentFilePath = Mage::getBaseDir('media'). DS . 'contacts' . DS . $fileName;
                if(file_exists($attachmentFilePath)){
                    $fileContents = file_get_contents($attachmentFilePath);
                    $attachment   = $mailTemplate->getMail()->createAttachment($fileContents);
                    $attachment->filename = $fileName;
                }
                /**************************************************************/
 
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );
 
                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();
                }
 
                $translate->setTranslateInline(true);
 
                Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                $translate->setTranslateInline(true);
                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }
 
        } else {
            $this->_redirect('*/*/');
        }
    }
}

有限制了上传格式,及将附件保存在/media/contacts/

3. 完成Magneto联系我们上传文件
。测试了下能作为邮件的附件顺利发送。

magento联系我们邮件附件效果图

使用Mamgi导入magento可筛选的select属性字段注意

注:本文针对的情况为Magento属性集(Attribute sets)和属性为程序批量添加的情况。如果是后台手动添加则没有这种问题。

例如csv文件的导入格式为:

"sku","store",....,"myselect"
"sku1","admin",....,"value1"
"sku2","admin",....,"value3"

这里的myselect为Magento新建的attribute,下拉可筛选的select

不同于使用magento自己的导入程序规范的csv文件格式,使用MAMGI导入的话需要更改 "value1" "value3" ..,为对应的属性字段的option的id。

可使用一下magento代码查询并替换:

$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute(" myselect
 ");
if ($attr->usesSource()) {
   $value_id = $attr->getSource()->getOptionId("value1");
}

这里的value_id的值替换"value1"再用Mamgi导入一遍即可。

Ps 泥马 被这问题吭了一天。。

Magento导入Tier Price

Magento默认无法导入Tier Price,需要在app/code/local/YOURMODULE/Catalog/Model/Convert/Adapter/Product.php 继续加入扩展方法。

已有的脚本里已实现过导入多图/多属性,自动创建目录,自动采集远程图片,现在再加导入Tier Price的功能。

核心函数:

private function _editTierPrices(&$product, $tier_prices_field = false)
{
if (($tier_prices_field) && !empty($tier_prices_field)) {
    if(trim($tier_prices_field) == 'REMOVE'){
        $product->setTierPrice(array());
    } else {
        $existing_tps = $product->getTierPrice();
        foreach($existing_tps as $key => $etp){
            $etp_lookup[$etp['price_qty']] = $key;
        }
        $incoming_tierps = explode('|',$tier_prices_field);
        foreach($incoming_tierps as $tier_str){
            $tmp = explode('=',$tier_str);
            $tps_toAdd[$tmp[0]] = array(
                                'website_id' => 0, // !!!! this is hard-coded for now
                                'cust_group' => 32000, // !!! so is this
                                'price_qty' => $tmp[0],
                                'price' => $tmp[1],
                                'delete' => ''
                            );
            if(isset($etp_lookup[$tmp[0]])){
                unset($existing_tps[$etp_lookup['price_qty']]);
            }
        }
        $tps_toAdd =  array_merge($existing_tps, $tps_toAdd);
        $product->setTierPrice($tps_toAdd);
    }
}
}

然后在saveRow函数里找地方加入:

if(
    isset($importData['tier_prices'])
    && !empty($importData['tier_prices'])
){
    $this->_editTierPrices($product, $importData['tier_prices']);
}

CSV文件里要有字段”tier_prices“,值的格式例子:qty=price|qty=price|qty=price

比如:50=12.25|500=11.70|5000=11.00

这样Magento就能导入Tier Price了

Magento: The Flat Catalog module has a limit of 64 filterable and/or sortable attributes. 错误

批量导入Magento attributeSets和Attributes之后,在重建索引的时候出现了这个错误:

The Flat Catalog module has a limit of 64 filterable and/or sortable attributes. Currently there are 772 of them. Please reduce the number of filterable/sortable attributes in order to use this module

字面上理解是Magento对于可筛选或排序的属性字段有数量限制 TAT!

排查了下,发现是在“/app/code/core/Mage/Catalog/Model/Resource/Product/Flat/Indexer.php” 这文件里头的prepareFlatTable函数抛出的错误。

限制数64是在配置文件:"app/code/core/Mage/Catalog/etc/config.xml" 里,搜索“max_index_count”就会发现有段配置:

<global>
    <catalog>
        <product>
            <flat>
                <max_index_count>64
</max_index_count>
            </flat>
        </product>
    </catalog>
</global>

修改限定值64为大于772的数字后,重建索引报错“Cannot initialize the indexer process.”

由此预测这个值应该是不能改的,解决办法只好从 filterable和sortable 的字段入手,重新导入,并限制64个以内的可筛选和可排序字段。。

具体为is_filterable和used_for_sort_by都设为0再导入magento属性集和属性

另外也可以在表catalog_eav_attribute中查出is_filterable = 1和used_for_sort_by = 1的记录

限制64以内了也就解决了The Flat Catalog module has a limit of 64 filterable and/or sortable attributes.这错误。。