/**
* wpsc_item_reassign_file function
*
* @param integer product ID
* @param string the selected file name;
*/
function wpsc_item_reassign_file($product_id, $selected_files)
{
global $wpdb;
$product_file_list = array();
// initialise $idhash to null to prevent issues with undefined variables and error logs
$idhash = null;
/* if we are editing, grab the current file and ID hash */
if (!$selected_files) {
// unlikely that anyone will ever upload a file called .none., so its the value used to signify clearing the product association
$wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `file` = '0' WHERE `id` = '{$product_id}' LIMIT 1");
return null;
}
foreach ($selected_files as $selected_file) {
// if we already use this file, there is no point doing anything more.
$current_fileid = $wpdb->get_var("SELECT `file` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id` = '{$product_id}' LIMIT 1");
if ($current_fileid > 0) {
$current_file_data = $wpdb->get_row("SELECT `id`,`idhash` FROM `" . WPSC_TABLE_PRODUCT_FILES . "` WHERE `id` = '{$current_fileid}' LIMIT 1", ARRAY_A);
if (basename($selected_file) == $file_data['idhash']) {
//$product_file_list[] = $current_fileid;
//return $current_fileid;
}
}
$selected_file = basename($selected_file);
if (file_exists(WPSC_FILE_DIR . $selected_file)) {
$timestamp = time();
$file_data = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_PRODUCT_FILES . "` WHERE `idhash` IN('" . $wpdb->escape($selected_file) . "') LIMIT 1", ARRAY_A);
$fileid = (int) $file_data['id'];
// if the file does not have a database row, add one.
if ($fileid < 1) {
$mimetype = wpsc_get_mimetype(WPSC_FILE_DIR . $selected_file);
$filename = $idhash = $selected_file;
$timestamp = time();
$wpdb->query("INSERT INTO `" . WPSC_TABLE_PRODUCT_FILES . "` (`product_id`, `filename` , `mimetype` , `idhash` , `date` ) VALUES ('{$product_id}', '{$filename}', '{$mimetype}', '{$idhash}', '{$timestamp}');");
$fileid = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_PRODUCT_FILES . "` WHERE `date` = '{$timestamp}' AND `filename` IN ('{$filename}')");
}
// update the entry in the product table
$wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `file` = '{$fileid}' WHERE `id` = '{$product_id}' LIMIT 1");
$product_file_list[] = $fileid;
}
}
//exit('<pre>'.print_r($product_file_list, true).'</pre>');
update_product_meta($product_id, 'product_files', $product_file_list);
return $fileid;
}
/**
* @todo - Should probably refactor this at some point - very procedural,
* WAY too many foreach loops for my liking :) But it does the trick
*
* @param <type> $term_id
*/
function save_term_prices($term_id)
{
// First - Saves options from input
if (isset($_POST['variation_price']) || isset($_POST["apply_to_current"])) {
$term_prices = get_option('term_prices');
$term_prices[$term_id]["price"] = sanitize_text_field($_POST["variation_price"]);
$term_prices[$term_id]["checked"] = isset($_POST["apply_to_current"]) ? "checked" : "unchecked";
update_option('term_prices', $term_prices);
}
// Second - If box was checked, let's then check whether or not it was flat, differential, or percentile, then let's apply the pricing to every product appropriately
if (isset($_POST["apply_to_current"])) {
//Now, find all products with this term_id, update their pricing structure (terms returned include only parents at this point, we'll grab relevent children soon)
$products_to_mod = get_objects_in_term($term_id, "wpsc-variation");
$product_parents = array();
foreach ((array) $products_to_mod as $get_parent) {
$post = get_post($get_parent);
if (!$post->post_parent) {
$product_parents[] = $post->ID;
}
}
//Now that we have all parent IDs with this term, we can get the children (only the ones that are also in $products_to_mod, we don't want to apply pricing to ALL kids)
foreach ($product_parents as $parent) {
$args = array('post_parent' => $parent, 'post_type' => 'wpsc-product');
$children = get_children($args, ARRAY_A);
foreach ($children as $childrens) {
$parent = $childrens["post_parent"];
$children_ids[$parent][] = $childrens["ID"];
$children_ids[$parent] = array_intersect($children_ids[$parent], $products_to_mod);
}
}
//Got the right kids, let's grab their parent pricing and modify their pricing based on var_price_type
foreach ((array) $children_ids as $parents => $kids) {
$kids = array_values($kids);
foreach ($kids as $kiddos) {
$price = wpsc_determine_variation_price($kiddos);
update_product_meta($kiddos, 'price', $price);
}
}
}
}
/**
* wpsc_decrement_claimed_stock method
*
* @param float a price
* @return string a price with a currency sign
*/
function wpsc_decrement_claimed_stock($purchase_log_id)
{
global $wpdb;
//processed
$all_claimed_stock = $wpdb->get_results($wpdb->prepare("SELECT `cs`.`product_id`, `cs`.`stock_claimed`, `pl`.`id`, `pl`.`processed` FROM `" . WPSC_TABLE_CLAIMED_STOCK . "` `cs` JOIN `" . WPSC_TABLE_PURCHASE_LOGS . "` `pl` ON `cs`.`cart_id` = `pl`.`id` WHERE `cs`.`cart_id` = '%s'", $purchase_log_id));
if (!empty($all_claimed_stock)) {
switch ($all_claimed_stock[0]->processed) {
case 3:
case 4:
case 5:
foreach ((array) $all_claimed_stock as $claimed_stock) {
$product = get_post($claimed_stock->product_id);
$current_stock = get_post_meta($product->ID, '_wpsc_stock', true);
$remaining_stock = $current_stock - $claimed_stock->stock_claimed;
update_product_meta($product->ID, 'stock', $remaining_stock);
$product_meta = get_product_meta($product->ID, 'product_metadata', true);
if ($remaining_stock < 1) {
// this is to make sure after upgrading to 3.8.9, products will have
// "notify_when_none_left" enabled by default if "unpublish_when_none_left"
// is enabled.
if (!isset($product_meta['notify_when_none_left'])) {
$product_meta['unpublish_when_none_left'] = 0;
if (!empty($product_meta['unpublish_when_none_left'])) {
$product_meta['unpublish_when_none_left'] = 1;
update_product_meta($product->ID, 'product_metadata', $product_meta);
}
}
$email_message = sprintf(__('The product "%s" is out of stock.', 'wpsc'), $product->post_title);
if (!empty($product_meta["unpublish_when_none_left"])) {
$result = wp_update_post(array('ID' => $product->ID, 'post_status' => $draft));
if ($result) {
$email_message = __('The product "%s" is out of stock and has been unpublished.', 'wpsc');
}
}
if ($product_meta["notify_when_none_left"] == 1) {
wp_mail(get_option('purch_log_email'), sprintf(__('%s is out of stock', 'wpsc'), $product->post_title), $email_message);
}
}
}
case 6:
$wpdb->query($wpdb->prepare("DELETE FROM `" . WPSC_TABLE_CLAIMED_STOCK . "` WHERE `cart_id` IN (%s)", $purchase_log_id));
break;
}
}
}
/**
* @todo - Should probably refactor this at some point - very procedural,
* WAY too many foreach loops for my liking :) But it does the trick
*
* @param <type> $term_id
*/
function save_term_prices($term_id)
{
// First - Saves options from input
if (isset($_POST['variation_price']) || isset($_POST["apply_to_current"])) {
$term_prices = get_option('term_prices');
$term_prices[$term_id]["price"] = $_POST["variation_price"];
$term_prices[$term_id]["checked"] = isset($_POST["apply_to_current"]) ? "checked" : "unchecked";
update_option('term_prices', $term_prices);
}
// Second - If box was checked, let's then check whether or not it was flat, differential, or percentile, then let's apply the pricing to every product appropriately
if (isset($_POST["apply_to_current"])) {
//Check for flat, percentile or differential
$var_price_type = '';
if (flat_price($_POST["variation_price"])) {
$var_price_type = 'flat';
} elseif (differential_price($_POST["variation_price"])) {
$var_price_type = 'differential';
} elseif (percentile_price($_POST["variation_price"])) {
$var_price_type = 'percentile';
}
//Now, find all products with this term_id, update their pricing structure (terms returned include only parents at this point, we'll grab relevent children soon)
$products_to_mod = get_objects_in_term($term_id, "wpsc-variation");
$product_parents = array();
foreach ((array) $products_to_mod as $get_parent) {
$post = get_post($get_parent);
if (!$post->post_parent) {
$product_parents[] = $post->ID;
}
}
//Now that we have all parent IDs with this term, we can get the children (only the ones that are also in $products_to_mod, we don't want to apply pricing to ALL kids)
foreach ($product_parents as $parent) {
$args = array('post_parent' => $parent, 'post_type' => 'wpsc-product');
$children = get_children($args, ARRAY_A);
foreach ($children as $childrens) {
$parent = $childrens["post_parent"];
$children_ids[$parent][] = $childrens["ID"];
$children_ids[$parent] = array_intersect($children_ids[$parent], $products_to_mod);
}
}
//Got the right kids, let's grab their parent pricing and modify their pricing based on var_price_type
foreach ((array) $children_ids as $parents => $kids) {
$kids = array_values($kids);
$parent_pricing = get_product_meta($parents, "price", true);
foreach ($kids as $kiddos) {
$child_pricing = get_product_meta($kiddos, "price", true);
if ($var_price_type == 'flat') {
update_product_meta($kiddos, "price", floatval($_POST["variation_price"]));
} elseif ($var_price_type == 'percentile') {
//Are we decreasing or increasing the price?
if (strchr($_POST["variation_price"], '-')) {
$positive = false;
} else {
$positive = true;
}
//Now, let's get the parent product price, +/- by the percentage given
$percentage = absint($_POST["variation_price"]) / 100;
if ($positive) {
$price = $parent_pricing + $parent_pricing * $percentage;
} else {
$price = $parent_pricing - $parent_pricing * $percentage;
}
update_product_meta($kiddos, "price", $price);
} elseif ($var_price_type == 'differential') {
//Are we decreasing or increasing the price?
if (strchr($_POST["variation_price"], '-')) {
$positive = false;
} else {
$positive = true;
}
//Now, let's get the parent product price, +/- by the differential given
$differential = absint($_POST["variation_price"]);
if ($positive) {
$price = $parent_pricing + $differential;
} else {
$price = $parent_pricing - $differential;
}
update_product_meta($kiddos, "price", $price);
}
}
}
}
}
/**
* wpsc_decrement_claimed_stock method
*
* @param float a price
* @return string a price with a currency sign
*/
function wpsc_decrement_claimed_stock($purchase_log_id)
{
// Processed
$claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $purchase_log_id));
$all_claimed_stock = $claimed_query->get_purchase_log_claimed_stock();
do_action('wpsc_pre_decrement_claimed_stock', $purchase_log_id, $claimed_query);
if (!empty($all_claimed_stock)) {
do_action('wpsc_decrement_claimed_stock_' . $all_claimed_stock[0]->processed, $purchase_log_id, $claimed_query);
do_action('wpsc_decrement_claimed_stock', $purchase_log_id, $claimed_query);
switch ($all_claimed_stock[0]->processed) {
case 3:
case 4:
case 5:
foreach ((array) $all_claimed_stock as $claimed_stock) {
$product = get_post($claimed_stock->product_id);
$current_stock = get_post_meta($product->ID, '_wpsc_stock', true);
$remaining_stock = $current_stock - $claimed_stock->stock_claimed;
update_product_meta($product->ID, 'stock', $remaining_stock);
$product_meta = get_product_meta($product->ID, 'product_metadata', true);
if ($remaining_stock < 1) {
// this is to make sure after upgrading to 3.8.9, products will have
// "notify_when_none_left" enabled by default if "unpublish_when_none_left"
// is enabled.
if (!isset($product_meta['notify_when_none_left'])) {
$product_meta['unpublish_when_none_left'] = 0;
if (!empty($product_meta['unpublish_when_none_left'])) {
$product_meta['unpublish_when_none_left'] = 1;
update_product_meta($product->ID, 'product_metadata', $product_meta);
}
}
$email_message = sprintf(__('The product "%s" is out of stock.', 'wp-e-commerce'), $product->post_title);
if (!empty($product_meta["unpublish_when_none_left"])) {
$result = wp_update_post(array('ID' => $product->ID, 'post_status' => 'draft'));
if ($result) {
$email_message = sprintf(__('The product "%s" is out of stock and has been unpublished.', 'wp-e-commerce'), $product->post_title);
}
}
if ($product_meta["notify_when_none_left"] == 1) {
wp_mail(get_option('purch_log_email'), sprintf(__('%s is out of stock', 'wp-e-commerce'), $product->post_title), $email_message);
}
}
}
case 6:
$claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $purchase_log_id));
$claimed_query->clear_claimed_stock(0);
break;
}
}
}
请发表评论