This Post is about cleaning out extra media images that are created by Magento’s functionality.
For better understanding, consider a case where we upload CSV of 1000 products, each containing one main image and also one thumb image. So, there would 2000 images uploaded in media folder at location ‘media/catalog/product/…’
Imagine if you set up a cron that uploads products from some CSV file everyday at midnight. It’s not the case that you are going to add/update/delete products everyday. So, if you have not updated anything for one week in CSV file, which is going to be run everyday using cron and products are going to be imported everyday for that whole week. But as the product_id would be same for the products that are not changed in any means, duplicate product would not be created. But the images would be duplicated and copied to ‘media/catalog/product/…’
Consider a case, for a product with images abc.jpg and abc_thumb.jpg. Now, when the same product is uploaded again, then abc_1.jpg and abc_thumb_1.jpg are created in ‘media/catalog/product/…’. Likewise after the whole week, images would be created as abc_2.jpg and abc_thumb_2.jpg and it continues until the last day of the week.
So, after 1 week, 2000 images would be now 14000 images. Just imagine what it would after 1 month, if old products are still there in CSV file that is uploaded by cron everyday. Ultimately, the size of media folder would increase day by day and server memory for our site would also increase. This may cause many serious problems.So, to avoid such problems, extra images from ‘media/catalog/product/…’ should be deleted of those images, whose entries are not found in db.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
Declare following variables inside your class // image clean variables protected $result = array(); public $valdir = array(); (CODE inside your controller action) /* delete duplicated images via image clean */ $this->compareList(); (CODE inside your controller action) (METHOD inside your controller) // delete extra images of 'media/catalog/product/' public function compareList(){ $read = Mage::getSingleton('core/resource')->getConnection('core_read'); $select = $read->select() ->from('catalog_product_entity_media_gallery','*') ->group(array('value_id')); $flushImages = $read->fetchAll($select); echo count($flushImages); $array = array(); foreach ($flushImages as $item1){ $array[] = $item1['value']; } $valores = $array; $pepe = 'media' . DS . 'catalog' . DS . 'product'; $leer = $this->listDirectories($pepe); foreach ($leer as $item){ try { $item = strtr($item,'\\','/'); if(!in_array($item, $valores)){ $valdir[]['filename'] = $item; unlink('media/catalog/product'. $item); } } catch(Zend_Db_Exception $e){ } catch(Exception $e){ //Mage::log($e->getMessage()); } } } (METHOD inside your controller) // list directories public function listDirectories($path){ if(is_dir($path)){ if ($dir = opendir($path)) { while (($entry = readdir($dir)) !== false) { if(preg_match('/^\./',$entry) != 1){ if (is_dir($path . DS . $entry) && !in_array($entry,array('cache','watermark')) ){ $this->listDirectories($path . DS . $entry); } elseif(!in_array($entry,array('cache','watermark')) && (strpos($entry,'.') != 0)) { $this->result[] = substr($path . DS . $entry, 21); } } } closedir($dir); } } return $this->result; } |
Now, there is one pair of image for each product that is uploaded repetitively.
Further flushing out the images of cache folder that is created by Magento at ‘media/catalog/product/cache’,
1 2 3 |
(CODE inside your controller action) /* flush catalog images cache */ Mage::getModel('catalog/product_image')->clearCache(); Mage::dispatchEvent('clean_catalog_images_cache_after'); |
Above code would delete the ‘cache’ folder from ‘media/catalog/product/’. So, now, extra catalog images that are created due to Magento cache would be deleted every time.
While uploading process of images using .csv file, Poeple usually create some temporary folder. So, after the images are copied to Magento directory structure, that temporary folder is not needed. To save memory space on server, it is a good practice to delete, all the extra folders, images, etc. So, to delete that temporary folder.
For example temporary folder named ‘import’ in ‘media’ folder of Magento and it contains .csv file and other images that must be deleted at the end.
It can be done through adding following code:
1 2 3 4 |
(CODE inside your controller action) /* remove import directory after import */ $import = Mage::getBaseDir('media').DS.'import'.DS; system('/bin/rm -rf ' . escapeshellarg($import)); |
Issue Fixed By: Nimit Shah
Guided By: Sunil Patel
This Blog will helpful to add signup for wholesale in your store, you can also learn How to Activate customer through admin, How to assign group to registered user. How to add Custom field into form and add attribute to store.
Whole post is divided in three part
1) Add Link on home page and register form.
2) Assign group to new register user.
3) Add Customer Attribute and Custom For field into form.
4) How to Activate User Through Admin and notify Customer and Admin about new Customer or Wholesaler.
Step 1: How to Add Link and Register form?
Now We will learn to add link as per below.
For that we will Copy the core file into our default folder or template. best practice is edit core file after copying into default folder instead of direct editing into core file.
Follow below step for copy and add link.
Copy core File from location: app/design/frontend/base/default/layout/customer.xml to app/design/frontend/default/default/layout/customer.xml
Add below code to customer.xml
1 2 3 4 5 6 7 8 9 10 |
<customer_logged_out> <reference name="top.links"> <action method="addLink" translate="label title" module="customer"> <label>Wholesale</label> <url>http://www.YourSite.com/wholesale</url> <position>100</position> </action> </reference> <remove name="reorder"></remove> </customer_logged_out> |
When we click on Wholesale for open registration form it will do nothing. to start working this link we need to create CMS Page.
How to create a CMS Page?
Login into Admin Panel and follow below step
Click on CMS => Pages then
Click on Add New Pages button.
Set “Page Title” and “Url Key” as wholesale.
Now, Click on “Design” tab inside “layout update XML” add below code
1 2 3 4 5 6 7 8 |
<reference name="content"> <block type="customer/form_register" name="customer_form_register" template="wholesale/customer/form/register.phtml"> <action method="setShowAddressFields"><value>true</value></action> <block type="page/html_wrapper" name="customer.form.register.fields.before" as="form_fields_before" translate="label"> <label>Form Fields Before</label> </block> </block> </reference> |
Above code will add template to new created page.
1 |
<action method="setShowAddressFields"><value>true</value></action> |
Above code will use to show addresss fields into wholesale registration form. if you do not want to add address field then put false instead of true.
Now we, will
Create a register.phtml for wholesale login as per below structure
app/design/frontend/default/default/template/wholesale/customer/form/register.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
<br /> <br /> <div class="account-create"> <br /> <br /> <div class="page-title"> <br /> <br /> <h1><?php echo $this->__('Create an Account') ?></h1> <p> <p> </div> <p> <p> <?php echo $this->getChildHtml('form_fields_before')?> <?php echo $this->getMessagesBlock()->getGroupedHtml() ?> <br /> <br /> <form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="form-validate"> <br /> <br /> <div class="fieldset"> <input type="hidden" name="success_url" value="<?php echo $this->getSuccessUrl() ?>" /> <input type="hidden" name="error_url" value="<?php echo $this->getErrorUrl() ?>" /> <br /> <br /> <h2 class="legend"><?php echo $this->__('Personal Information') ?></h2> <p> <p> <br /> <br /> <ul class="form-list"> <br /> <br /> <li class="fields"> <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?> </li> <p> <p> <li> <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label> <br /> <br /> <div class="input-box"> <input type="text" name="email" id="email_address" value="<?php echo $this->escapeHtml($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" /> </div> <p> <p> </li> <p> <p> <?php if ($this->isNewsletterEnabled()): ?> <br /> <br /> <li class="control"> <br /> <br /> <div class="input-box"> <input type="checkbox" name="is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if($this->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" /> </div> <p> <p> <label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label> </li> <p> <p> <?php endif ?> <?php $_dob = $this->getLayout()->createBlock('customer/widget_dob') ?> <?php if ($_dob->isEnabled()): ?> <li><?php echo $_dob->setDate($this->getFormData()->getDob())->toHtml() ?></li> <p> <p> <?php endif ?> <?php $_taxvat = $this->getLayout()->createBlock('customer/widget_taxvat') ?> <?php if ($_taxvat->isEnabled()): ?> <li><?php echo $_taxvat->setTaxvat($this->getFormData()->getTaxvat())->toHtml() ?></li> <p> <p> <?php endif ?> <?php $_gender = $this->getLayout()->createBlock('customer/widget_gender') ?> <?php if ($_gender->isEnabled()): ?> <li><?php echo $_gender->setGender($this->getFormData()->getGender())->toHtml() ?></li> <p> <p> <?php endif ?> <li> <br /> <br /> <div class="field"> <label for="your_cusom_field" class="required"><em>*</em><?php echo $this->__('Your Custom Field');?></label> <br /> <br /> <div class="input-box"> <input type="text" name="your_cusom_field" id="your_cusom_field" value="" title="<?php echo $this->__('Your Custom Field');?>" class="input-text required-entry"> </div> <p> <p> </div> <p> <p> </li> <p> <p> </ul> <p> <p> </div> <p> <p> <?php if($this->getShowAddressFields()): ?> <br /> <br /> <div class="fieldset"> <input type="hidden" name="create_address" value="1" /> <br /> <br /> <h2 class="legend"><?php echo $this->__('Address Information') ?></h2> <p> <p> <br /> <br /> <ul class="form-list"> <br /> <br /> <li class="fields"> <br /> <br /> <div class="field"> <label for="company"><?php echo $this->__('Company') ?></label> <br /> <br /> <div class="input-box"> <input type="text" name="company" id="company" value="<?php echo $this->escapeHtml($this->getFormData()->getCompany()) ?>" title="<?php echo $this->__('Company') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company') ?>" /> </div> <p> <p> </div> <p> <p> <br /> <br /> <div class="field"> <label for="telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label> <br /> <br /> <div class="input-box"> <input type="text" name="telephone" id="telephone" value="<?php echo $this->escapeHtml($this->getFormData()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" /> </div> <p> <p> </div> <p> <p> </li> <p> <p> <?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?> <br /> <br /> <li class="wide"> <label for="street_1" class="required"><em>*</em><?php echo $this->__('Street Address') ?></label> <br /> <br /> <div class="input-box"> <input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet(1)) ?>" title="<?php echo $this->__('Street Address') ?>" id="street_1" class="input-text <?php echo $_streetValidationClass ?>" /> </div> <p> <p> </li> <p> <p> <?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?> <?php for ($_i = 2, $_n = $this->helper('customer/address')->getStreetLines(); $_i <= $_n; $_i++): ?> <br /> <br /> <li class="wide"> <br /> <br /> <div class="input-box"> <input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet($_i)) ?>" title="<?php echo $this->__('Street Address %s', $_i) ?>" id="street_<?php echo $_i ?>" class="input-text <?php echo $_streetValidationClass ?>" /> </div> <p> <p> </li> <p> <p> <?php endfor; ?> <br /> <br /> <li class="fields"> <br /> <br /> <div class="field"> <label for="city" class="required"><em>*</em><?php echo $this->__('City') ?></label> <br /> <br /> <div class="input-box"> <input type="text" name="city" value="<?php echo $this->escapeHtml($this->getFormData()->getCity()) ?>" title="<?php echo $this->__('City') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="city" /> </div> <p> <p> </div> <p> <p> <br /> <br /> <div class="field"> <label for="region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label> <br /> <br /> <div class="input-box"> <br /> <br /> <select id="region_id" name="region_id" title="<?php echo $this->__('State/Province') ?>" class="validate-select" style="display:none;"> <br /> <br /> <option value=""><?php echo $this->__('Please select region, state or province') ?></option> <p> <p> </select> <p> <p> <script type="text/javascript"> //<![CDATA[ $('region_id').setAttribute('defaultValue', "<?php echo $this->getFormData()->getRegionId() ?>"); //]]> </script> <input type="text" id="region" name="region" value="<?php echo $this->escapeHtml($this->getRegion()) ?>" title="<?php echo $this->__('State/Province') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" /> </div> <p> <p> </div> <p> <p> </li> <p> <p> <br /> <br /> <li class="fields"> <br /> <br /> <div class="field"> <label for="zip" class="required"><em>*</em><?php echo $this->__('Zip/Postal Code') ?></label> <br /> <br /> <div class="input-box"> <input type="text" name="postcode" value="<?php echo $this->escapeHtml($this->getFormData()->getPostcode()) ?>" title="<?php echo $this->__('Zip/Postal Code') ?>" id="zip" class="input-text validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" /> </div> <p> <p> </div> <p> <p> <br /> <br /> <div class="field"> <label for="country" class="required"><em>*</em><?php echo $this->__('Country') ?></label> <br /> <br /> <div class="input-box"> <?php echo $this->getCountryHtmlSelect() ?> </div> <p> <p> </div> <p> <p> </li> <p> <p> </ul> <p> <p> <input type="hidden" name="default_billing" value="1" /> <input type="hidden" name="default_shipping" value="1" /> </div> <p> <p> <?php endif; ?> <br /> <br /> <div class="fieldset"> <br /> <br /> <h2 class="legend"><?php echo $this->__('Login Information') ?></h2> <p> <p> <br /> <br /> <ul class="form-list"> <br /> <br /> <li class="fields"> <br /> <br /> <div class="field"> <label for="password" class="required"><em>*</em><?php echo $this->__('Password') ?></label> <br /> <br /> <div class="input-box"> <input type="password" name="password" id="password" title="<?php echo $this->__('Password') ?>" class="input-text required-entry validate-password" /> </div> <p> <p> </div> <p> <p> <br /> <br /> <div class="field"> <label for="confirmation" class="required"><em>*</em><?php echo $this->__('Confirm Password') ?></label> <br /> <br /> <div class="input-box"> <input type="password" name="confirmation" title="<?php echo $this->__('Confirm Password') ?>" id="confirmation" class="input-text required-entry validate-cpassword" /> </div> <p> <p> </div> <p> <p> </li> <p> <p> <?php echo $this->getChildHtml('form.additional.info'); ?> <?php echo $this->getChildHtml('persistent.remember.me'); ?> </ul> <p> <p> <?php echo $this->getChildHtml('persistent.remember.me.tooltip'); ?> </div> <p> <p> <br /> <br /> <div class="buttons-set"> <br /> <br /> <p class="required"><?php echo $this->__('* Required Fields') ?></p> <p> <p> <br /> <br /> <p class="back-link"><a href="<?php echo $this->escapeUrl($this->getBackUrl()) ?>" class="back-link"><small>« </small><?php echo $this->__('Back') ?></a></p> <p> <p> <button type="submit" title="<?php echo $this->__('Submit') ?>" class="button"><span><span><?php echo $this->__('Submit') ?></span></span></button> </div> <p> <p> <?php if (Mage::helper('checkout')->isContextCheckout()): ?> <input name="context" type="hidden" value="checkout" /> <?php endif; ?> </form> <p> <p> <script type="text/javascript"> //<![CDATA[ var dataForm = new VarienForm('form-validate', true); <?php if($this->getShowAddressFields()): ?> new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'zip'); <?php endif; ?> //]]> </script> </div> <p> <p> |
Step 2: How to Assign Group to wholesale or General customer?
Open register.phtml and add below code after line No. 51.
1 |
<input type="hidden" name="group_id" id="group_id" value="2" /> |
Note: Default value is give as above. but it May be different; put as per your wholesale group value.
How to Find Value:
=> Customer => Manage Customer.
=> in Group click on drop down then count number of Wholesale from first group.
=> whatever number you get it will be your value.
Now open AccountController.php
app/code/core/Mage/Customer/controllers/AccountController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public function createPostAction() { $customer = $this->_getCustomer(); try { /* Extra Content Strart for add assign group to customer and wholesale */ if($this->getRequest()->getPost('group_id')){ $customer->setGroupId($this->getRequest()->getPost('group_id')); } else { $customer->getGroupId(); } /* Extra Content End for add assign group to customer and wholesale */ ....... ....... } } |
Step 3: Add Customer Attribute and Custom field into form.
Add below Code inside register.phtml it will use to add field into your wholesale registration form.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<br /> <br /> <br /> <div class="field"> <label for="your_cusom_field" class="required"><em>*</em><?php echo $this->__('Your Custom Field');?></label> <br /> <br /> <br /> <div class="input-box"> <input type="text" name="your_cusom_field" id="your_cusom_field" value="" title="<?php echo $this->__('Your Custom Field');?>" class="input-text required-entry"> </div> <p> <p> <p> </div> <p> <p> <p> |
Now, We will see how to add attribute into store.
For that we need to install Customer Attribute Manager. you can download it from here.
Select Customer => Customer Attributes Manager.
Then click on “Add Attribute” button. then follow as per below image.
Now Save Attribute.
Step 4: How to Activate wholesaler through admin?
Install Customer Activation Module into your store.
For Activate Customer, Select Customer => Manage Customer admin side and select particular customer and
inside “Account information” select “Yes” for is Activated.
For module setting System => Configuration
inside “Customers” tab select “Customer Configuration” and then “Customer Activation” and Enable Module.
=>select Require Activation for Specific Groups only as “Yes” then
=>select “Wholesale” in Require Activation for Groups as per given below Image
Issue Fixed By: Punit K. Gadhiya
Guided By: Jignesh Thummar