Remove up-sells, cross-sells, related products

Data about up-sells, cross-sells and related products in Magento is stored in catalog_product_link table. The link_type_id column specifies the relation between product_id and linked_product_id. Possible relations are:

1 – Related products
2 – Bundle products
3 – Grouped products
4 – Up-sell
5 – Cross-sell

So if you want to remove all up-sells for all products you can execute:

DELETE FROM catalog_product_link WHERE link_type_id = 4

Analogously you can remove all cross-sells and related products.

Remove test orders from Magento, preserve the real ones

You cannot remove orders from Magento using the admin panel. You can truncate several MySQL tables but this will remove all orders. What if you want to preserve some of them? Here is a small script that removes all orders except these which IDs are in $order_iids.

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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
$mageFilename = '../app/Mage.php';
require_once $mageFilename;
umask(0);
 
//Initialize Magento
Mage::app("default");
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 
//Get sales collection
$collection = Mage::getModel('sales/order')->getCollection();
 
//Keep these orders, remove others
$order_iids = array(1, 2, 3, 10, 20);
 
foreach ($collection as $collection_element) {
 
  $order = Mage::getModel('sales/order')->load($collection_element->getId());
  $iid = ((int) ($order->getIncrementId())) - 100000000;
 
  if(in_array($iid, $order_iids)) {
    continue;
  }
 
  //Get and remove order invoices
  $invoices = $order->getInvoiceCollection();
  foreach ($invoices as $invoice){
    $invoice->delete();
  }
 
  //Get and remove order credit memos
  $creditnotes = $order->getCreditmemosCollection();
  foreach ($creditnotes as $creditnote){
    $creditnote->delete();
  }
 
  //Get and remove order shipment data
  $shipments = $order->getShipmentsCollection();
  foreach ($shipments as $shipment){
    $shipment->delete();
  }
 
  $order->delete();
}
?>

Fix theme paging in Magento 1.4

Using 1.3 version theme in a 1.4 installation you can not run the pager in the product list correctly. The paging is missing.

The solution is simple. We will put the new pager method in our 1.3 version theme:

Replace the toolbar.phtml located in

app/design/frontend/default/YOUR_THEME/template/catalog/product/list

with the one in

app/design/frontend/base/default/template/catalog/product/list

In the catalog.xml look for the line

<block type=”catalog/product_list_toolbar” name=”product_list_toolbar” template=”catalog/product/list/toolbar.phtml”>

Then add the line just below:

<block type=”page/html_pager” name=”product_list_toolbar_pager”/>

Replace the pager.phtml located in

app/desin/frontend/default/YOUR_THEME/template/page/html

with the one in

app/design/frontend/base/default/template/page/html

Now it should not give you any error and the product list should work fine.

Javascript String truncate function

Javascript String truncate function, This function helps us to truncate the given string to the particular given character length.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function truncate(text, length, ellipsis) {
 
// Set length and ellipsis to defaults if not defined
if (typeof length == 'undefined') var length = 100;
if (typeof ellipsis == 'undefined') var ellipsis = '...';
 
// Return if the text is already lower than the cutoff
if (text.length < length) return text;
 
// Otherwise, check if the last character is a space.
// If not, keep counting down from the last character
// until we find a character that is a space
for (var i = length-1; text.charAt(i) != ' '; i--) {
length--;
}
 
// The for() loop ends when it finds a space, and the length var
// has been updated so it doesn't cut in the middle of a word.
return text.substr(0, length) + ellipsis;
}
 
var some_text = 'This is example text This is example text This is example text This is example text This is example text ';

Call to the above function is,

truncate(some_text, 10);

Output,

This is ex…

Function to check file exists in javascript

This function returns true if the file exists in the given URL. The below mentioned code is optimized to be used with Google Chrome also.
Instead of “return true, return false”, “return 1, return 0″ is used – this is because Google chrome avoids false and true statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
function file_exists (url) {
// Returns true if filename exists
 
var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
if (!req) {throw new Error('XMLHttpRequest not supported');}
// HEAD Results are usually shorter (faster) than GET
req.open('HEAD', url, false);
req.send(null);
if (req.status == 200){
return 1;
}
return 0;
}