wpsc_save_cart_item

Located in: cart.class.php

Functions that call this hook: save_to_db
Arguments Accepted: $cart_id, $product_id
Version Introduced: 3.8

Description: Called at the end of the save to db function once WP-e-Commerce has added the cart item to the database – This will all happen upon the checkout been submitted.

You could use this hook to save extra product information to the database for manipulation after checkout.

Code in context:
[php firstline=”1828″ highlight=”1914″]
/**
* save to database method
* @access public
*
* @param integer purchase log id
*/
function save_to_db($purchase_log_id) {
global $wpdb, $wpsc_shipping_modules;

$method = $this->cart->selected_shipping_method;
$shipping = 0;
if( !empty($method) && method_exists( $wpsc_shipping_modules[$method], "get_item_shipping" )) {
$shipping = $wpsc_shipping_modules[$this->cart->selected_shipping_method]->get_item_shipping($this);
}
if($this->cart->has_total_shipping_discount()) {
$shipping = 0;
}

//initialize tax variables
$tax = 0;
$tax_rate = 0;

//wpec_taxes – calculate product tax and add to total price
$wpec_taxes_controller = new wpec_taxes_controller();

if($wpec_taxes_controller->wpec_taxes_isincluded() && $wpec_taxes_controller->wpec_taxes_isenabled()){
$taxes = $wpec_taxes_controller->wpec_taxes_calculate_included_tax($this);
$tax_rate = $taxes[‘rate’];
$tax = $taxes[‘tax’];
}

$wpdb->query($wpdb->prepare(
"INSERT INTO `".WPSC_TABLE_CART_CONTENTS."` (
`prodid`, `name`, `purchaseid`, `price`, `pnp`,
`tax_charged`, `gst`, `quantity`, `donation`,
`no_shipping`, `custom_message`, `files`, `meta`
) VALUES (‘%d’, ‘%s’, ‘%d’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%d’, ‘0’, ‘%s’, ‘%s’, NULL)",
$this->product_id,
$this->product_name,
$purchase_log_id,
$this->unit_price,
(float)$shipping,
(float)$tax,
(float)$tax_rate,
$this->quantity,
$this->is_donation,
$this->custom_message,
serialize($this->custom_file)
));
$cart_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `".WPSC_TABLE_CART_CONTENTS."` LIMIT 1");

wpsc_update_cartmeta($cart_id, ‘sku’, $this->sku);

$downloads = get_option(‘max_downloads’);
if($this->is_downloadable == true) {

$product_files = (array)get_posts(array(
‘post_type’ => ‘wpsc-product-file’,
‘post_parent’ => $this->product_id,
‘numberposts’ => -1,
‘post_status’ => ‘inherit’
));
foreach($product_files as $file){
// if the file is downloadable, check that the file is real
$unique_id = sha1(uniqid(mt_rand(), true));
$wpdb->query("INSERT INTO `".WPSC_TABLE_DOWNLOAD_STATUS."` (
`product_id` , `fileid` ,
`purchid` , `cartid`,
`uniqueid`, `downloads`,
`active` , `datetime`
) VALUES (
‘{$this->product_id}’, ‘{$file->ID}’,
‘{$purchase_log_id}’, ‘{$cart_id}’,
‘{$unique_id}’, ‘$downloads’,
‘0’, NOW()
);");

$download_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `".WPSC_TABLE_DOWNLOAD_STATUS."` LIMIT 1");
wpsc_update_meta($download_id, ‘_is_legacy’, ‘false’, ‘wpsc_downloads’);
}

}

do_action(‘wpsc_save_cart_item’, $cart_id, $this->product_id);
}

}[/php]