【问题标题】:Magento - Customer Profile Photo uploadMagento - 客户资料照片上传
【发布时间】:2012-05-02 21:35:03
【问题描述】:

我一直在尝试寻找一种方法将照片添加到 Magento 的客户资料中。 除了这个,他们什么都有,我在任何地方都找不到怎么做。 任何帮助表示赞赏。 我正在使用社区版。

【问题讨论】:

    标签: magento


    【解决方案1】:

    要在 magento 中为客户上传个人资料照片,我们需要执行以下几个步骤。

    • 为个人资料照片添加一个新字段(How to create new fields for customer - 检查此链接它将对您有所帮助)。
    • 以上链接可帮助您在数据库中添加新文件,您需要手动上传该照片,以下代码将帮助您在 magento 中上传照片。

                      if(isset($_FILES['logo']['name']) and (file_exists($_FILES['logo']['tmp_name']))) 
                      {
      
                        try {
                          $uploader = new Varien_File_Uploader('logo');
                          $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                          $uploader->setAllowRenameFiles(false);
                          $uploader->setFilesDispersion(false);
                          $path       = Mage::getBaseDir('media') . DS .'catalog/customer/logo/';
                          $newName    = time() . $_FILES['logo']['name'];
                          $uploader->save($path, $newName);
                          $customer->setLogo($newName);
      
                          // actual path of image
                          $imageUrl = $path . $newName;
      
                          // path of the resized image to be saved
                          // here, the resized image is saved in media/resized folder
                          $imageResized = $path . $newName;
      
                          // resize image only if the image file exists and the resized image file doesn't exist
                          // the image is resized proportionally with the width/height 135px
                          if (!file_exists($imageResized)&&file_exists($imageUrl)) :
                              $imageObj = new Varien_Image($imageUrl);
                              $imageObj->constrainOnly(TRUE);
                              $imageObj->keepAspectRatio(TRUE);
                              $imageObj->keepFrame(FALSE);
                              $imageObj->resize(150, 150);
                              $imageObj->save($imageResized);
      
                          endif;
                        }catch(Exception $e) {
      
                        }
                      }
      
    • 上传后我们需要将文件名保存在数据库中。

      $customer->setLogo($newName);

    【讨论】:

    • 嗨桑卡尔。感谢您的解决方案!我已将代码放入模块的观察者中,但每当“customer_save_before”或“customer_save_after”事件触发时(在客户后端)$_FILES 数组为空。我在 $_POST 数组中得到文件名,但我没有得到 $_FILES。有任何想法吗?也许您可以建议我可以放置此代码的其他地方?
    • 好的,我知道了。 “附加信息”表单需要 enctype="multipart/form-data" 属性。否则不会提交文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多