Latest News: (loading..)

Archived

This topic is now archived and is closed to further replies.

burt

Useful Snippets of Code

50 posts in this topic

More PCI compliance for .htaccess:

 

<IfModule mod_rewrite.c>
 Options +FollowSymlinks
 RewriteEngine On
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^account.php https://example-domain-please-change-me.com/account.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^affiliate_affiliate.php https://example-domain-please-change-me.com/affiliate_affiliate.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^affiliate_summary.php https://example-domain-please-change-me.com/affiliate_summary.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^login.php https://example-domain-please-change-me.com/login.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^create_account.php https://example-domain-please-change-me.com/create_account.php [R=301,L]
#
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^checkout_confirmation.php https://example-domain-please-change-me.com/checkout_confirmation.php [R=301,L]
</IfModule>

Share this post


Link to post
Share on other sites

Heres a handy snippet for adding product to cart from an external URL, or just in plain HTML format:

<a href="http:/your-site.com/index.php/cPath/#/action/buy_now/products_id/#">Add To Cart</a>

Replace "#" with the category id and product id.

Share this post


Link to post
Share on other sites

PHPMYADMIN

 

Update weight of all products in a known category;

 

update products set products_weight = 'Y' where products_id in (select products_id from products_to_categories where categories_id = X)

 

Change Y to the desired weight and X to the ID of the category.

Share this post


Link to post
Share on other sites

I needed to ensure that stock does not reduce below zero, as part of my barcode system for shops that are both online and high street. This takes care of it;

 

tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = GREATEST(products_quantity-1, 0) where products_upc = '" . $scanned_upc . "'");

 

Now if the stock in the high street shop is sitting at 1 piece and it is scanned (making the stock go to zero), any mistaken scan would mean the stock stays at zero. Eg, if the assistant makes a mistake and scans the barcode twice. We need to ensure that stock does not go into negative numbers so that when stock is replenished (via barcode), the stock level is correct.

 

Example: stock sits at 1. Accidentally scanned twice. Stock now sits at -1 (assume there is no failsafe to keep stock at zero). High Street gets new stock comprising 3 items. These are scanned in to update stock. Stock now sits at 2. But there are actually 3 for sale;

 

-1 + 3 = 2

 

whereas, with the failsafe;

0 + 3 = 3

Share this post


Link to post
Share on other sites

PHPMYADMIN

 

Update weight of all products in a known category;

 

update products set products_weight = 'Y' where products_id in (select products_id from products_to_categories where categories_id = X)

 

Change Y to the desired weight and X to the ID of the category.

Not exactly a tip, but one of my customizations is the addition of a categories_weight field ( a field cloned from sort_order) and when a products_weight is missing, it defaults to the categories weight, if that is missing, it tels the customer to contact us as the item is missing data to provide an accurate shipping quote.

burt likes this

Share this post


Link to post
Share on other sites

w3c compliance

Replace all instances of 'hspace="5" vspace="5"' with ' class="image-margins"'

and add the class to your stylesheet.

 

Sara

Share this post


Link to post
Share on other sites

All of the 2 country ISO codes, which I needed to find but could not find (admittedly did not try very hard):

ALL ISO's

AF,AX,AL,DZ,AS,AD,AO,AI,AQ,AG,AR,AM,AW,AU,AT,AZ,
BS,BH,BD,BB,BY,BE,BZ,BJ,BM,BT,BO,BQ,BA,BW,BV,BR,IO,BN,BG,BF,BI,KH,
CM,CA,CV,KY,CF,TD,CL,CN,CX,CC,CO,KM,CG,CD,CK,CR,CI,HR,CU,CW,CY,CZ,
DK,DJ,DM,DO,
EC,EG,SV,GQ,ER,EE,ET,
FK,FO,FJ,FI,FR,
GF,PF,TF,GA,GM,GE,DE,GH,GI,GR,GL,GD,GP,GU,GT,GG,GN,GW,GY,
HT,HM,VA,HN,HK,HU,
IS,IN,ID,IR,IQ,IE,IM,IL,IT,
JM,JP,JE,JO,
KZ,KE,KI,KP,KR,KW,KG,
LA,LV,LB,LS,LR,LY,LI,LT,LU,
MO,MK,MG,MW,MY,MV,ML,MT,MH,MQ,MR,MU,YT,MX,FM,MD,MC,MN,ME,MS,MA,MZ,MM,
NA,NR,NP,NL,NC,NZ,NI,NE,NG,NU,NF,MP,NO,
OM,
PK,PW,PS,P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Y,UZ,
VU,VE,VN,VG,VI,
WF,EH,
YE,
ZM,ZW

Share this post


Link to post
Share on other sites

@@burt

 

Not quite sure how that is a useful code snippet...

 

But on another note, you can use GEOIP to ban countries countries IPs from going to your site. Just add this to your htaccess file:

 

 

<IfModule mod_geoip.c>
 GeoIPEnable ON
 # add one line for each country you wish to block
 #Vietanm
 SetEnvIF GEOIP_COUNTRY_CODE VN BlockThese
 #russia
 SetEnvIF GEOIP_COUNTRY_CODE RU BlockThese
 #romania
 SetEnvIF GEOIP_COUNTRY_CODE RO BlockThese
 #turkey
 SetEnvIF GEOIP_COUNTRY_CODE TR BlockThese
 #China
 SetEnvIF GEOIP_COUNTRY_CODE CN BlockThese
 #Nigeria
 SetEnvIF GEOIP_COUNTRY_CODE NG BlockThese
 #Iran
 SetEnvIF GEOIP_COUNTRY_CODE IR BlockThese
 Deny from env=BlockThese
</IfModule>


imop likes this

Share this post


Link to post
Share on other sites

update products set products_price = (products_price*Y) where products_id in (select products_id from products_to_categories where categories_id = X)

 

increase or decrease prices by a percentage, of all products in a given category.

 

Change y to the increase/decrease (eg: make prices 20% more = 1.2), change x to the id of the category to change.

Share this post


Link to post
Share on other sites

Update price of products by a given manufacturer.

 

update `products` set `products_price` = (`products_price` * 'x.yz') WHERE `manufacturers_id` = 'w';

 

Change x.yz to the increase amount, eg increase prices by 15% would be 1.15.

Change w to the ID of the manufacturer.

Share this post


Link to post
Share on other sites

Before MySql update training in Workbrench

 

SET SQL_SAFE_UPDATES=0;

Share this post


Link to post
Share on other sites

Somewhere I heard it might hurt you to ignore ip canonicalization, This addon to .htaccess is supposed to fix that. Any comments from you SEO folks?

# Eliminates www vs. non-www
RewriteCond %{HTTP_HOST} ^domain\.com [OR]
RewriteCond %{HTTP_HOST} ^123\.45\.67\.89
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]

 

 

edit; I guess you really need the [OR] to get this working!

Share this post


Link to post
Share on other sites

Here's a different one. Directly backing up a table from a table in phpmyadmin / sql

UPDATE products p, 0214products p2
SET p.products_price = p2.products_price,
p.products_weight = p2.products_weight,
p.products_length = p2.products_length,
p.products_width = p2.products_width,
p.products_height = p2.products_height,
p.suggested_retail = p.suggested_retail
WHERE p.products_id = p2.products_id
AND p.products_ready_to_ship = 1
AND p.products_id = 1

The key here is understanding I'm using "p.products_ready_to_ship = 1" as a switch. I sorted the database and those products with corrupted data became "p.products_ready_to_ship = 1" first.

The second thing is the last part of the where; "AND p.products_id = 1" this is just a test row I'm watching as I run the query. If it doesn't work right I only have one row (I hopefully jotted down the undisturbed details prior to running the query - should go without saying right?).

No doubt this is dangerous. Create test tables by copying real ones. Test those first.

Share this post


Link to post
Share on other sites
UPDATE `orders` SET `customers_postcode` = UPPER(`customers_postcode`);

 

This will update all your postcodes to upper case.

There are a few other places where postcodes are stored.

 

You might also want to enforce a strtoupper($postcode) on the DB inserts in create_account and address_book_process

Share this post


Link to post
Share on other sites

Pages loading slow under your Windows development environment? I just experienced load times of around 2-3 seconds and discovered changing the database server address from:

 

localhost

 

to:

 

127.0.0.1

 

fixes the problem. Pages now load lightning fast!

 

This can be done in includes/configure.php and admin/includes/configure.php, for the DB_SERVER value.

Tsimi likes this

Share this post


Link to post
Share on other sites

for System & Network which support IPV6 protocol, add in your [Drive Letter]:\Windows\System32\Drivers\etc\hosts* :

::1			 localhost

 

* administrator rights required.

Share this post


Link to post
Share on other sites

Ive found some older databases may have orphaned address_book entries due to not properly removing customers or whatever reasons - here are some snippets to clean up the address_book table.

 

First, run this query and if you have any records returned, create a view

SELECT * FROM address_book a WHERE NOT EXISTS (SELECT 1 FROM customers c WHERE c.customers_id = a.customers_id)
ORDER BY a.customers_id;

 

~Do a google for creating a view in phpMyAdmin ~ lots of info exist, but you'll find the create view button at bottom of the page; name it something like orphaned_address_books.

 

It is now up to you to confirm the integrity of the data returned, look at some of the customers_id's in the view, check to see if the same customers_id exist in the customers table - if they DO NOT exist in the customers table then you may proceed. You don't really want to waste time checking each of them if you have a high return, just randomly select a handful to ensure the query returned as expected.

 

Now, using the view you should be able to delete the orphaned rows.

 

By the way ~ YOU did backup the address_book table, right?

Share this post


Link to post
Share on other sites

When feeling myself in a development enviroment as a blind man running in the city.

 

I use this minimal function in catalog/iincludes/local/configure.php so no conflict with github
 

 // my debug functions
 // display values & exit
 //
 function de($v = array('exit')) { echo '<pre>'; print_r($v); echo '</pre>'; exit; }
 
 // only display values
 function d($v = array('exit')) { echo '<pre>'; print_r($v); echo '</pre>'; }
bruyndoncx likes this

Share this post


Link to post
Share on other sites

I am using Cache, and notice that my bestsellers boxes disappear every once in a while. When the cache files are deleted, the pages work fine again. Came across this useful piece of code in the add-on section. Create a script in the root directory with the following contents and then create a cron job to execute every few hours.

<?php

	$files = glob("includes/work/bm_best_sellers_box-*");
	
	if (!count($files)) { die('No files in cache'); }
	
	foreach ($files as $filename) {
	
		unlink($filename);
	
	}
	
	echo 'Cleared cache';
	
?>

Share this post


Link to post
Share on other sites

Bootstrap Core Memory Usage

add on the top in application_top:
 

  // ini_set('memory_limit', '4M');
  $memory_start = memory_get_usage(false);

Find at the bottom of application_bottom:

?>


Change to:

  $memory_end = memory_get_peak_usage(false);
  $valuemax = (int)ini_get('memory_limit');
  $valuenow = round(($memory_end-$memory_start)/1024/1024/$valuemax, 3)*100;

?>
<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $valuenow; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $valuenow; ?>%;">
    <?php echo $valuenow; ?>%
  </div>
</div>

 

Share this post


Link to post
Share on other sites
I was looking for a way to inspect the microdata using some javascript on page.

This page shows a solution in action, that you can add to your codebase,
http://krofdrakula.github.io/microdata-tool/

but even better, it is also avalable as a bookmarklet
https://gist.github.com/1397528
for your use without having to modify your page's source code!
Simply visit this page
http://jsfiddle.net/peterhost/ZJfpL/14/embedded/result/
and drag the link to your bookmark bar and you're set to go.

Share this post


Link to post
Share on other sites

Thank's Carine,

 

For the link #2, remove the  )f  at the end and it will work fine ;)

Share this post


Link to post
Share on other sites

Thank's Carine,

 

For the link #2, remove the  )f  at the end and it will work fine ;)

Thanks for the heads up, burt corrected the original post for me.

Share this post


Link to post
Share on other sites