related links

   Bluekora

   Linkedin

   Facebook

   Chris Shiflett Blog

   CSS-Tricks

Coffee!!
Drag Me!

Welcome!

I’m a 23 year old lad from Portugal, Vila Nova de Gaia.
I’m a Software Engineer working, at the moment, at bluekora.
I can be found on LinkedIn at my profile.

March 30 2010

WP e-commerce SKU on variations

Tagged Under : , , , , ,

UPDATE – 01/09/2010

If you need additions that are not contemplated in this fix package you can email me for a specific quote.

UPDATE – 18/05/2010

There has been an update on wp-e-commerce a few days ago and if you have applied my add-on you’ll probably have problems with the plugin. Post a comment or email me if you’re having problems.

===============================

If you are using wp-ecommerce plugin for wordpress and you ran into some missing functionalities this might help you.

The specific mod I’ll show you today will enable you to specify SKU to any variation on any product on your system.

I don’t know how proficient your are on programming WordPress, but let me know if you run into any difficulty.

So, first of all you need to add a new field on your database on table wp_wpsc_variations_properties called “sku”. I added this field as varchar with a length of 64 characters, but do adapt this one to fit your needs.

Backup your e-commerce plugin folder.

Now, open file /wp-content/plugins/wp-e-commerce/wpsc-includes/variations.class.php.

After line,

$output .= "
".TXT_WPSC_VARIATION."
 
\n\r";

add,

$output .= "
".TXT_WPSC_SKU."
 
\n\r";

Change line,

$unindexed_variation_properties_data = $wpdb->get_results("SELECT `id`, `stock`, `price`, `weight`,`weight_unit`, `file` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` WHERE `id` IN({$priceandstock_id_string})", ARRAY_A);

to,

$unindexed_variation_properties_data = $wpdb->get_results("SELECT `id`, `stock`, `sku`, `price`, `weight`,`weight_unit`, `file` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` WHERE `id` IN({$priceandstock_id_string})", ARRAY_A);

After line,

$product_price = $variation_properties_data[$variation_set['priceandstock_id']]['price'];

add,

$product_sku = $variation_properties_data[$variation_set['priceandstock_id']]['sku'];

After line,

$product_price = $product_data['price'];

add,

$product_sku = "";

After line,

$output .= "".str_replace(" ", " ", (stripslashes( $variation_names )))."\n\r";

add,

$output .= "
<input name="variation_priceandstock[{$variation_ids}][sku]" size="8" type="text" value="$product_sku" />\n\r";

After line,

$variation_price = (float)str_replace(",","",$variation_data['price']);

add,

$variation_sku = (string)$variation_data['sku'];

After line,

$variation_sql = null; // return the sql array to null for each trip round the loop

add,

if(($variation_stock_data['sku'] != $variation_sku)) {
$variation_sql[] = "`sku` = '{$variation_sku}'";
}

This should enable you fill the sku field we added before. Now to get the info of the sku on the order page.

Open file /wp-content/plugins/wp-e-commerce/wpsc-includes/purchaselogs.class.php.

On line,

function wpsc_purchaselog_details_SKU(){

Change all function to,

purchitem-&gt;prodid, 'sku');
	$variation_name = stripslashes($purchlogitem-&gt;purchitem-&gt;name);
	$associated_variations = $wpdb-&gt;get_col("SELECT `variation_id` FROM `".WPSC_TABLE_VARIATION_ASSOC."` WHERE `type` IN ('product') AND `associated_id` = '{$purchlogitem-&gt;purchitem-&gt;prodid}' ORDER BY `id` ASC");
 
	$variation_count = count($associated_variations);
 
	asort($associated_variations);
	if($variation_count &gt; 0) {
		//stripslashes($purchlogitem-&gt;purchitem-&gt;name);
		foreach((array)$associated_variations as $variation) {
			$variation = (int)$variation;
 
			$excluded_value_sql = '';
			if($purchlogitem-&gt;purchitem-&gt;prodid &gt; 0 ) {
				$included_values = $wpdb-&gt;get_col("SELECT `value_id` FROM `".WPSC_TABLE_VARIATION_VALUES_ASSOC."` WHERE `product_id` IN('{$purchlogitem-&gt;purchitem-&gt;prodid}') AND `variation_id` IN ('{$variation}') AND `visible` IN ('1')");
				$included_values_sql = "AND `a{$variation}`.`id` IN('".implode("','", $included_values)."')";
			}
			//else if(count($variation_values) &gt; 0) {
			//$included_values_sql = "AND `a{$variation}`.`id` IN('".implode("','", $variation_values)."')";
			//}
 
			$join_selected_cols[] = "`a{$variation}`.`id` AS `id_{$variation}`, `a{$variation}`.`name` AS `name_{$variation}`";
			$join_subselect_cols[] = "`a{$variation}`.`id`";
			$join_tables[] = "`".WPSC_TABLE_VARIATION_VALUES."` AS `a{$variation}`";
			$join_conditions[] = "`a{$variation}`.`variation_id` = '{$variation}' $included_values_sql";
	}
 
	$variation_count = count($associated_variations);
	$all_variation_ids = implode(",", $associated_variations);
	$join_subselect_cols = implode(", ", $join_subselect_cols);
 
	$join_selected_cols[] = "(
SELECT `priceandstock_id`
FROM `".WPSC_TABLE_VARIATION_COMBINATIONS."`
WHERE `product_id` = '{$purchlogitem-&gt;purchitem-&gt;prodid}'
AND `value_id`
IN ( {$join_subselect_cols} )
AND `all_variation_ids`
IN ( '{$all_variation_ids}'	)
GROUP BY `priceandstock_id`
HAVING COUNT( `priceandstock_id` ) = '{$variation_count}'
LIMIT 1
) AS `priceandstock_id`";
 
	// implode the SQL statment segments into bigger segments
	$join_selected_cols = implode(", ", $join_selected_cols);
	$join_tables = implode("\nJOIN ", $join_tables);
	$join_conditions = implode("\nAND ", $join_conditions);
 
	// Assemble and execute the SQL query
	$associated_variation_values = $wpdb-&gt;get_results(
"SELECT {$join_selected_cols}
FROM {$join_tables}
WHERE {$join_conditions}
ORDER BY {$join_subselect_cols}"
	, ARRAY_A);
	//print_r($associated_variation_values);
	foreach((array)$associated_variation_values as $key =&gt; $variation_set) {
		$variation_id_array = array();
		$variation_names = array();
		foreach($associated_variations as $variation_id) {
			$variation_id_array[] = $variation_set["id_{$variation_id}"];
			$variation_names[] = $variation_set["name_{$variation_id}"];
		}
 
		$variation_names = implode(", ", $variation_names);
		$variation_ids = implode(",", $variation_id_array);
		if(strpos($variation_name, $variation_names)) {
 
			$variation_sku = $wpdb-&gt;get_row("SELECT `sku` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` WHERE `id` = '{$variation_set['priceandstock_id']}' LIMIT 1", ARRAY_A);
 
			if(!empty($variation_sku['sku']))
			$meta_value = $variation_sku['sku'];
		}
	}
}
 
if($meta_value ==''){
	return 'N/A';
}else{
	return $meta_value;
}
 
}
?&gt;

This should get you up and running. Let me know if you need anything.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • PDF

Comments:

(30) posted on WP e-commerce SKU on variations

Post a comment


scarletbits.com 125x125 Are My Sites Up? Get Chitika Premium Job Board for Web Designers and Web Developers

Tag Cloud