/** * add_to_cart function, used through ajax and in normal page loading. * No parameters, returns nothing */ function wpsc_add_to_cart() { global $wpdb, $wpsc_cart; /// default values $default_parameters['variation_values'] = null; $default_parameters['quantity'] = 1; $default_parameters['provided_price'] = null; $default_parameters['comment'] = null; $default_parameters['time_requested'] = null; $default_parameters['custom_message'] = null; $default_parameters['file_data'] = null; $default_parameters['is_customisable'] = false; $default_parameters['meta'] = null; $provided_parameters = array(); /// sanitise submitted values $product_id = (int) $_POST['product_id']; if (isset($_POST['variation'])) { foreach ((array) $_POST['variation'] as $key => $variation) { $provided_parameters['variation_values'][(int) $key] = (int) $variation; } if (count($provided_parameters['variation_values']) > 0) { $variation_product_id = wpsc_get_child_object_in_terms($product_id, $provided_parameters['variation_values'], 'wpsc-variation'); if ($variation_product_id > 0) { $product_id = $variation_product_id; } } } if (isset($_POST['quantity']) && $_POST['quantity'] > 0 && !isset($_POST['wpsc_quantity_update'])) { $provided_parameters['quantity'] = (int) $_POST['quantity']; } else { if (isset($_POST['wpsc_quantity_update'])) { $wpsc_cart->remove_item($_POST['key']); $provided_parameters['quantity'] = (int) $_POST['wpsc_quantity_update']; } } if (isset($_POST['is_customisable']) && $_POST['is_customisable'] == 'true') { $provided_parameters['is_customisable'] = true; if (isset($_POST['custom_text'])) { $provided_parameters['custom_message'] = $_POST['custom_text']; } if (isset($_FILES['custom_file'])) { $provided_parameters['file_data'] = $_FILES['custom_file']; } } if (isset($_POST['donation_price']) && (double) $_POST['donation_price'] > 0) { $provided_parameters['provided_price'] = (double) $_POST['donation_price']; } $parameters = array_merge($default_parameters, (array) $provided_parameters); $state = $wpsc_cart->set_item($product_id, $parameters); $product = get_post($product_id); if ($state == true) { $cart_messages[] = str_replace("[product_name]", stripslashes($product->post_title), __('<div class="cartSucess">Você adicionou o produto "[product_name]" ao seu carrinho de compras.</div>', 'wpsc')); } else { if ($parameters['quantity'] <= 0) { $cart_messages[] = __('<div class="cartError">Desculpe, mas você não pode adicionar zero itens no carrinho</div>', 'wpsc'); } else { if ($wpsc_cart->get_remaining_quantity($product_id, $parameters['variation_values'], $parameters['quantity']) > 0) { $quantity = $wpsc_cart->get_remaining_quantity($product_id, $parameters['variation_values'], $parameters['quantity']); $cart_messages[] = sprintf(_n('<div class="cartError">Desculpa, mas só restam %s deste produto em estoque.</div>', '<div class="cartError">Desculpe, mas só restam %s deste produto em estoque.</div>', $quantity, 'wpsc'), $quantity); } else { $cart_messages[] = sprintf(__('<div class="cartError">Desculpe, mas o produto "%s" está fora de estoque.</div>', 'wpsc'), $product->post_title); } } } if (isset($_GET['ajax']) && $_GET['ajax'] == 'true') { if ($product_id != null && get_option('fancy_notifications') == 1) { echo "if(jQuery('#fancy_notification_content')) {\n\r"; echo " jQuery('#fancy_notification_content').html(\"" . str_replace(array("\n", "\r"), array('\\n', '\\r'), addslashes(fancy_notification_content($cart_messages))) . "\");\n\r"; echo " jQuery('#loading_animation').css('display', 'none');\n\r"; echo " jQuery('#fancy_notification_content').css('display', 'block');\n\r"; echo "}\n\r"; $error_messages = array(); } ob_start(); include_once wpsc_get_template_file_path('wpsc-cart_widget.php'); $output = ob_get_contents(); ob_end_clean(); $output = str_replace(array("\n", "\r"), array("\\n", "\\r"), addslashes($output)); echo "jQuery('div.shopping-cart-wrapper').html('{$output}');\n"; if (get_option('show_sliding_cart') == 1) { if (wpsc_cart_item_count() > 0 || count($cart_messages) > 0) { $_SESSION['slider_state'] = 1; echo "\n jQuery('#sliding_cart').slideDown('fast',function(){\n jQuery('#fancy_collapser').attr('src', ('" . WPSC_CORE_IMAGES_URL . "/minus.png'));\n });\n "; } else { $_SESSION['slider_state'] = 0; echo "\n jQuery('#sliding_cart').slideUp('fast',function(){\n jQuery('#fancy_collapser').attr('src', ('" . WPSC_CORE_IMAGES_URL . "/plus.png'));\n });\n "; } } echo "jQuery('.cart_message').delay(3000).slideUp(500);"; do_action('wpsc_alternate_cart_html', $cart_messages); exit; } }
/** * wpsc_edit_product_variations function. * this is the function to make child products using variations * * @access public * @param mixed $product_id * @param mixed $post_data * @return void */ function wpsc_edit_product_variations($product_id, $post_data) { global $user_ID; $parent = get_post_field('post_parent', $product_id); if (!empty($parent)) { return; } $variations = array(); $product_children = array(); if (!isset($post_data['edit_var_val'])) { $post_data['edit_var_val'] = ''; } $variations = (array) $post_data['edit_var_val']; // Generate the arrays for variation sets, values and combinations $wpsc_combinator = new wpsc_variation_combinator($variations); // Retrieve the array containing the variation set IDs $variation_sets = $wpsc_combinator->return_variation_sets(); // Retrieve the array containing the combinations of each variation set to be associated with this product. $variation_values = $wpsc_combinator->return_variation_values(); // Retrieve the array containing the combinations of each variation set to be associated with this product. $combinations = $wpsc_combinator->return_combinations(); $product_terms = wp_get_object_terms($product_id, 'wpsc-variation'); $variation_sets_and_values = array_merge($variation_sets, $variation_values); $variation_sets_and_values = apply_filters('wpsc_edit_product_variation_sets_and_values', $variation_sets_and_values, $product_id); wp_set_object_terms($product_id, $variation_sets_and_values, 'wpsc-variation'); $child_product_template = array('post_author' => $user_ID, 'post_content' => $post_data['description'], 'post_excerpt' => $post_data['additional_description'], 'post_title' => $post_data['name'], 'post_status' => 'inherit', 'post_type' => "wpsc-product", 'post_name' => sanitize_title($post_data['name']), 'post_parent' => $product_id); $child_product_meta = get_post_custom($product_id); // here we loop through the combinations, get the term data and generate custom product names foreach ($combinations as $combination) { $term_names = array(); $term_ids = array(); $term_slugs = array(); $product_values = $child_product_template; $combination_terms = get_terms('wpsc-variation', array('hide_empty' => 0, 'include' => implode(",", $combination), 'orderby' => 'parent')); foreach ($combination_terms as $term) { $term_ids[] = $term->term_id; $term_slugs[] = $term->slug; $term_names[] = $term->name; } $product_values['post_title'] .= " (" . implode(", ", $term_names) . ")"; $product_values['post_name'] = sanitize_title($product_values['post_title']); $selected_post = get_posts(array('name' => $product_values['post_name'], 'post_parent' => $product_id, 'post_type' => "wpsc-product", 'post_status' => 'all', 'suppress_filters' => true)); $selected_post = array_shift($selected_post); $child_product_id = wpsc_get_child_object_in_terms($product_id, $term_ids, 'wpsc-variation'); $already_a_variation = true; if ($child_product_id == false) { $already_a_variation = false; if ($selected_post != null) { $child_product_id = $selected_post->ID; } else { $child_product_id = wp_insert_post($product_values); } } else { // sometimes there have been problems saving the variations, this gets the correct product ID if ($selected_post != null && $selected_post->ID != $child_product_id) { $child_product_id = $selected_post->ID; } } $product_children[] = $child_product_id; if ($child_product_id > 0) { wp_set_object_terms($child_product_id, $term_slugs, 'wpsc-variation'); } //JS - 7.9 - Adding loop to include meta data in child product. if (!$already_a_variation) { $this_child_product_meta = apply_filters('insert_child_product_meta', $child_product_meta, $product_id, $combination_terms); foreach ($this_child_product_meta as $meta_key => $meta_value) { if ($meta_key == "_wpsc_product_metadata") { update_post_meta($child_product_id, $meta_key, unserialize($meta_value[0])); } else { update_post_meta($child_product_id, $meta_key, $meta_value[0]); } } if (is_array($term_ids) && ($price = wpsc_determine_variation_price($child_product_id, $term_ids))) { update_product_meta($child_product_id, 'price', $price); } } } //For reasons unknown, this code did not previously deal with variation deletions. //Basically, we'll just check if any existing term associations are missing from the posted variables, delete if they are. //Get posted terms (multi-dimensional array, first level = parent var, second level = child var) $posted_term = $variations; //Get currently associated terms $currently_associated_var = $product_terms; foreach ($currently_associated_var as $current) { $currently_associated_vars[] = $current->term_id; } foreach ($posted_term as $term => $val) { $posted_terms[] = $term; if (is_array($val)) { foreach ($val as $term2 => $val2) { $posted_terms[] = $term2; } } } if (!empty($currently_associated_vars)) { $term_ids_to_delete = array(); $term_ids_to_delete = array_diff($currently_associated_vars, $posted_terms); } if (isset($_REQUEST["post_ID"])) { $post_id = $_REQUEST["post_ID"]; } elseif (isset($_REQUEST["product_id"])) { $post_id = $_REQUEST["product_id"]; } if (!empty($term_ids_to_delete) && (isset($_REQUEST["product_id"]) || isset($post_id))) { $post_ids_to_delete = array(); // Whatever remains, find child products of current product with that term, in the variation taxonomy, and delete $post_ids_to_delete = wpsc_get_child_object_in_terms_var($_REQUEST["product_id"], $term_ids_to_delete, 'wpsc-variation'); if (is_array($post_ids_to_delete) && !empty($post_ids_to_delete)) { foreach ($post_ids_to_delete as $object_ids) { foreach ($object_ids as $object_id) { wp_delete_post($object_id); } } } } $current_children = query_posts(array('post_parent' => $post_id, 'post_type' => "wpsc-product", 'post_status' => 'all')); foreach ((array) $current_children as $child_prod) { $childs[] = $child_prod->ID; } if (!empty($childs)) { $old_ids_to_delete = array_diff($childs, $product_children); $old_ids_to_delete = apply_filters('wpsc_edit_product_variations_deletion', $old_ids_to_delete); if (is_array($old_ids_to_delete) && !empty($old_ids_to_delete)) { foreach ($old_ids_to_delete as $object_ids) { wp_delete_post($object_ids); } } } }
/** * add_to_cart function, used through ajax and in normal page loading. * No parameters, returns nothing */ function wpsc_add_to_cart() { global $wpsc_cart; /// default values $default_parameters['variation_values'] = null; $default_parameters['quantity'] = 1; $default_parameters['provided_price'] = null; $default_parameters['comment'] = null; $default_parameters['time_requested'] = null; $default_parameters['custom_message'] = null; $default_parameters['file_data'] = null; $default_parameters['is_customisable'] = false; $default_parameters['meta'] = null; $provided_parameters = array(); /// sanitise submitted values $product_id = apply_filters('wpsc_add_to_cart_product_id', (int) $_POST['product_id']); // compatibility with older themes if (isset($_POST['wpsc_quantity_update']) && is_array($_POST['wpsc_quantity_update'])) { $_POST['wpsc_quantity_update'] = $_POST['wpsc_quantity_update'][$product_id]; } if (isset($_POST['variation'])) { foreach ((array) $_POST['variation'] as $key => $variation) { $provided_parameters['variation_values'][(int) $key] = (int) $variation; } if (count($provided_parameters['variation_values']) > 0) { $variation_product_id = wpsc_get_child_object_in_terms($product_id, $provided_parameters['variation_values'], 'wpsc-variation'); if ($variation_product_id > 0) { $product_id = $variation_product_id; } } } if (isset($_POST['quantity']) && $_POST['quantity'] > 0 && !isset($_POST['wpsc_quantity_update'])) { $provided_parameters['quantity'] = (int) $_POST['quantity']; } else { if (isset($_POST['wpsc_quantity_update'])) { $wpsc_cart->remove_item($_POST['key']); $provided_parameters['quantity'] = (int) $_POST['wpsc_quantity_update']; } } if (isset($_POST['is_customisable']) && $_POST['is_customisable'] == 'true') { $provided_parameters['is_customisable'] = true; if (isset($_POST['custom_text'])) { $provided_parameters['custom_message'] = $_POST['custom_text']; } if (isset($_FILES['custom_file'])) { $provided_parameters['file_data'] = $_FILES['custom_file']; } } if (isset($_POST['donation_price']) && (double) $_POST['donation_price'] > 0) { $provided_parameters['provided_price'] = (double) $_POST['donation_price']; } $parameters = array_merge($default_parameters, (array) $provided_parameters); $state = $wpsc_cart->set_item($product_id, $parameters); $product = get_post($product_id); if ($state == true) { $cart_messages[] = str_replace("[product_name]", stripslashes($product->post_title), __('You just added "[product_name]" to your cart.', 'wpsc')); } else { if ($parameters['quantity'] <= 0) { $cart_messages[] = __('Sorry, but you cannot add zero items to your cart', 'wpsc'); } else { if ($wpsc_cart->get_remaining_quantity($product_id, $parameters['variation_values'], $parameters['quantity']) > 0) { $quantity = $wpsc_cart->get_remaining_quantity($product_id, $parameters['variation_values'], $parameters['quantity']); $cart_messages[] = sprintf(_n('Sorry, but there is only %s of this item in stock.', 'Sorry, but there are only %s of this item in stock.', $quantity, 'wpsc'), $quantity); } else { $cart_messages[] = sprintf(__('Sorry, but the item "%s" is out of stock.', 'wpsc'), $product->post_title); } } } if (isset($_GET['ajax']) && $_GET['ajax'] == 'true') { if ($product_id != null && get_option('fancy_notifications') == 1) { echo "if(jQuery('#fancy_notification_content')) {\n\r"; echo " jQuery('#fancy_notification_content').html(\"" . str_replace(array("\n", "\r"), array('\\n', '\\r'), addslashes(fancy_notification_content($cart_messages))) . "\");\n\r"; echo " jQuery('#loading_animation').css('display', 'none');\n\r"; echo " jQuery('#fancy_notification_content').css('display', 'block');\n\r"; echo "}\n\r"; $error_messages = array(); } ob_start(); include_once wpsc_get_template_file_path('wpsc-cart_widget.php'); $output = ob_get_contents(); ob_end_clean(); $output = str_replace(array("\n", "\r"), array("\\n", "\\r"), addslashes($output)); echo "jQuery('div.shopping-cart-wrapper').html('{$output}');\n"; if (get_option('show_sliding_cart') == 1) { if (wpsc_cart_item_count() > 0 || count($cart_messages) > 0) { $_SESSION['slider_state'] = 1; echo "\n jQuery('#sliding_cart').slideDown('fast',function(){\n jQuery('#fancy_collapser').attr('src', ('" . WPSC_CORE_IMAGES_URL . "/minus.png'));\n });\n "; } else { $_SESSION['slider_state'] = 0; echo "\n jQuery('#sliding_cart').slideUp('fast',function(){\n jQuery('#fancy_collapser').attr('src', ('" . WPSC_CORE_IMAGES_URL . "/plus.png'));\n });\n "; } } echo "jQuery('.cart_message').delay(3000).slideUp(500);"; do_action('wpsc_alternate_cart_html', $cart_messages); exit; } }
function wpsc_calculate_price($product_id, $variations = false, $special = true) { global $wpdb; $p_id = $product_id; if (!empty($variations)) { $product_id = wpsc_get_child_object_in_terms($product_id, $variations, 'wpsc-variation'); } elseif (!$product_id) { $product_id = get_the_ID(); } if (!$product_id && !empty($variations)) { $product_ids = wpsc_get_child_object_in_select_terms($p_id, $variations, 'wpsc_variation'); $sql = "SELECT `post_id` FROM " . $wpdb->postmeta . " WHERE `meta_key` = '_wpsc_stock' AND `meta_value` != '0' AND `post_id` IN (" . implode(',', $product_ids) . ")"; $stock_available = $wpdb->get_col($sql); $sql = "SELECT `post_id` FROM " . $wpdb->postmeta . " WHERE `meta_key` = '_wpsc_price' AND `post_id` IN (" . implode(',', $stock_available) . ") ORDER BY `meta_value` ASC LIMIT 1"; $product_id = $wpdb->get_var($sql); } if ($special) { $full_price = get_post_meta($product_id, '_wpsc_price', true); $special_price = get_post_meta($product_id, '_wpsc_special_price', true); $price = $full_price; if ($full_price > $special_price && $special_price > 0) { $price = $special_price; } } else { $price = get_post_meta($product_id, '_wpsc_price', true); } $price = apply_filters('wpsc_price', $price, $product_id); return $price; }
/** * add_to_cart function, used through ajax and in normal page loading. * No parameters, returns nothing */ function wpsc_add_to_cart() { global $wpsc_cart; $default_parameters = $cart_messages = $provided_parameters = array(); /// default values $default_parameters['variation_values'] = null; $default_parameters['quantity'] = 1; $default_parameters['provided_price'] = null; $default_parameters['comment'] = null; $default_parameters['time_requested'] = null; $default_parameters['custom_message'] = null; $default_parameters['file_data'] = null; $default_parameters['is_customisable'] = false; $default_parameters['meta'] = null; $post_type_object = get_post_type_object('wpsc-product'); $permitted_post_statuses = current_user_can($post_type_object->cap->edit_posts) ? apply_filters('wpsc_product_display_status', array('publish')) : array('publish'); /// sanitise submitted values $product_id = apply_filters('wpsc_add_to_cart_product_id', (int) $_POST['product_id']); $product = apply_filters('wpsc_add_to_cart_product_object', get_post($product_id, OBJECT, 'display')); if (!in_array($product->post_status, $permitted_post_statuses) || 'wpsc-product' != $product->post_type) { return false; } // compatibility with older themes if (isset($_POST['wpsc_quantity_update']) && is_array($_POST['wpsc_quantity_update'])) { $_POST['wpsc_quantity_update'] = $_POST['wpsc_quantity_update'][$product_id]; } if (isset($_POST['variation'])) { foreach ((array) $_POST['variation'] as $key => $variation) { $provided_parameters['variation_values'][(int) $key] = (int) $variation; } if (count($provided_parameters['variation_values']) > 0) { $variation_product_id = wpsc_get_child_object_in_terms($product_id, $provided_parameters['variation_values'], 'wpsc-variation'); if ($variation_product_id > 0) { $product_id = $variation_product_id; } } } if (isset($_POST['quantity']) && $_POST['quantity'] > 0 && !isset($_POST['wpsc_quantity_update'])) { $provided_parameters['quantity'] = (int) $_POST['quantity']; } else { if (isset($_POST['wpsc_quantity_update'])) { $wpsc_cart->remove_item($_POST['key']); $provided_parameters['quantity'] = (int) $_POST['wpsc_quantity_update']; } } if (isset($_POST['is_customisable']) && 'true' == $_POST['is_customisable']) { $provided_parameters['is_customisable'] = true; if (isset($_POST['custom_text'])) { $provided_parameters['custom_message'] = stripslashes($_POST['custom_text']); } if (isset($_FILES['custom_file'])) { $provided_parameters['file_data'] = $_FILES['custom_file']; } } if (isset($_POST['donation_price']) && (double) $_POST['donation_price'] > 0) { $provided_parameters['provided_price'] = (double) $_POST['donation_price']; } $parameters = array_merge($default_parameters, (array) $provided_parameters); $cart_item = $wpsc_cart->set_item($product_id, $parameters); if (is_object($cart_item)) { do_action('wpsc_add_to_cart', $product, $cart_item); $cart_messages[] = str_replace("[product_name]", $cart_item->get_title(), __('You just added "[product_name]" to your cart.', 'wpsc')); } else { if ($parameters['quantity'] <= 0) { $cart_messages[] = __('Sorry, but you cannot add zero items to your cart', 'wpsc'); } else { if (wpsc_product_has_variations($product_id) && is_null($parameters['variation_values'])) { $cart_messages[] = apply_filters('wpsc_add_to_cart_variation_missing_message', sprintf(__('This product has several options to choose from.<br /><br /><a href="%s" style="display:inline; float:none; margin: 0; padding: 0;">Visit the product page</a> to select options.', 'wpsc'), esc_url(get_permalink($product_id))), $product_id); } else { if ($wpsc_cart->get_remaining_quantity($product_id, $parameters['variation_values'], $parameters['quantity']) > 0) { $quantity = $wpsc_cart->get_remaining_quantity($product_id, $parameters['variation_values'], $parameters['quantity']); $cart_messages[] = sprintf(_n('Sorry, but there is only %s of this item in stock.', 'Sorry, but there are only %s of this item in stock.', $quantity, 'wpsc'), $quantity); } else { $cart_messages[] = apply_filters('wpsc_add_to_cart_out_of_stock_message', __('Sorry, but this item is out of stock.', 'wpsc'), $product_id); } } } } if (defined('DOING_AJAX') && DOING_AJAX) { $json_response = array('cart_messages' => $cart_messages, 'product_id' => $product_id, 'cart_total' => wpsc_cart_total()); $output = _wpsc_ajax_get_cart(false, $cart_messages); $json_response = $json_response + $output; if (is_numeric($product_id) && 1 == get_option('fancy_notifications')) { $json_response['fancy_notification'] = str_replace(array("\n", "\r"), array('\\n', '\\r'), fancy_notification_content($cart_messages)); } $json_response = apply_filters('wpsc_add_to_cart_json_response', $json_response); die(json_encode($json_response)); } }
public function add($product_id) { global $wpsc_cart; if (!wp_verify_nonce($_REQUEST['_wp_nonce'], "wpsc-add-to-cart-{$product_id}")) { wp_die(__('Request expired. Please try adding the item to your cart again.', 'wpsc')); } extract($_REQUEST, EXTR_SKIP); $defaults = array('variation_values' => array(), 'quantity' => 1, 'provided_price' => null, 'comment' => null, 'time_requested' => null, 'custom_message' => null, 'file_data' => null, 'is_customisable' => false, 'meta' => null); $provided_parameters = array(); $product_id = apply_filters('wpsc_add_to_cart_product_id', (int) $product_id); if (!empty($wpsc_product_variations)) { foreach ($wpsc_product_variations as $key => $variation) { $provided_parameters['variation_values'][(int) $key] = (int) $variation; } $variation_product_id = wpsc_get_child_object_in_terms($product_id, $provided_parameters['variation_values'], 'wpsc-variation'); if ($variation_product_id > 0) { $product_id = $variation_product_id; } } if (!empty($quantity)) { $provided_parameters['quantity'] = (int) $quantity; } if (!empty($is_customisable)) { $provided_parameters['is_customisable'] = true; if (isset($custom_text)) { $provided_parameters['custom_message'] = $custom_text; } if (isset($_FILES['custom_file'])) { $provided_parameters['file_data'] = $_FILES['custom_file']; } } if (isset($donation_price) && (double) $donation_price > 0) { $provided_parameters['provided_price'] = (double) $donation_price; } $parameters = array_merge($defaults, $provided_parameters); if ($parameters['quantity'] <= 0) { $this->message_collection->add(__('Sorry, but the quantity you just entered is not valid. Please try again.', 'wpsc'), 'error', 'main', 'flash'); return; } $product = apply_filters('wpsc_add_to_cart_product_object', get_post($product_id, OBJECT, 'display')); $stock = get_post_meta($product_id, '_wpsc_stock', true); $remaining_quantity = $wpsc_cart->get_remaining_quantity($product_id, $parameters['variation_values']); if ($stock !== '' && $remaining_quantity !== true) { if ($remaining_quantity <= 0) { $message = apply_filters('wpsc_add_to_cart_out_of_stock_message', __('Sorry, the product "%s" is out of stock.', 'wpsc')); $this->message_collection->add(sprintf($message, $product->post_title), 'error', 'main', 'flash'); wp_safe_redirect(wp_get_referer()); exit; } elseif ($remaining_quantity < $parameters['quantity']) { $message = __('Sorry, but the quantity you just specified is larger than the available stock. There are only %d of the item in stock.', 'wpsc'); $this->message_collection->add(sprintf($message, $remaining_quantity), 'error', 'main', 'flash'); wp_safe_redirect(wp_get_referer()); exit; } } if (wpsc_product_has_variations($product_id) && is_null($parameters['variation_values'])) { $message = apply_filters('wpsc_add_to_cart_variation_missing_message', sprintf(__('This product has several options to choose from.<br /><br /><a href="%s" style="display:inline; float:none; margin: 0; padding: 0;">Visit the product page</a> to select options.', 'wpsc'), esc_url(get_permalink($product_id))), $product_id); $this->message_collection->add(sprintf($message, $product->post_title), 'error', 'main', 'flash'); wp_safe_redirect(wp_get_referer()); exit; } if ($wpsc_cart->set_item($product_id, $parameters)) { $message = sprintf(__('You just added %s to your cart.', 'wpsc'), $product->post_title); $this->message_collection->add($message, 'success', 'main', 'flash'); wp_safe_redirect(wpsc_get_cart_url()); exit; } else { $this->message_collection->add(__('An unknown error just occured. Please contact the shop administrator.', 'wpsc'), 'error', 'main', 'flash'); wp_safe_redirect(wp_get_referer()); exit; } }
function wpsc_convert_variation_combinations() { global $wpdb, $user_ID, $current_version_number; // get the posts // I use a direct SQL query here because the get_posts function sometimes does not function for a reason that is not clear. $posts = $wpdb->get_results("SELECT * FROM `{$wpdb->posts}` WHERE `post_type` IN('wpsc-product')"); $posts = get_posts(array('post_type' => 'wpsc-product', 'post_status' => 'all', 'numberposts' => -1)); foreach ((array) $posts as $post) { $base_product_terms = array(); //create a post template $child_product_template = array('post_author' => $user_ID, 'post_content' => $post->post_content, 'post_excerpt' => $post->post_excerpt, 'post_title' => $post->post_title, 'post_status' => 'inherit', 'post_type' => "wpsc-product", 'post_name' => sanitize_title($post->post_title), 'post_parent' => $post->ID); // select the original product ID $original_id = get_post_meta($post->ID, '_wpsc_original_id', true); $parent_stock = get_post_meta($post->ID, '_wpsc_stock', true); // select the variation set associations $variation_set_associations = $wpdb->get_col("SELECT `variation_id` FROM " . WPSC_TABLE_VARIATION_ASSOC . " WHERE `associated_id` = '{$original_id}'"); // select the variation associations if the count of variation sets is greater than zero if ($original_id > 0 && count($variation_set_associations) > 0) { $variation_associations = $wpdb->get_col("SELECT `value_id` FROM " . WPSC_TABLE_VARIATION_VALUES_ASSOC . " WHERE `product_id` = '{$original_id}' AND `variation_id` IN(" . implode(", ", $variation_set_associations) . ") AND `visible` IN ('1')"); } else { // otherwise, we have no active variations, skip to the next product continue; } foreach ($variation_set_associations as $variation_set_id) { $base_product_terms[] = wpsc_get_meta($variation_set_id, 'variation_set_id', 'wpsc_variation_set'); } foreach ($variation_associations as $variation_association_id) { $base_product_terms[] = wpsc_get_meta($variation_association_id, 'variation_id', 'wpsc_variation'); } // Now that we have the term IDs, we need to retrieve the slugs, as wp_set_object_terms will not use IDs in the way we want // If we pass IDs into wp_set_object_terms, it creates terms using the ID as the name. $parent_product_terms = get_terms('wpsc-variation', array('hide_empty' => 0, 'include' => implode(",", $base_product_terms), 'orderby' => 'parent')); $base_product_term_slugs = array(); foreach ($parent_product_terms as $parent_product_term) { $base_product_term_slugs[] = $parent_product_term->slug; } wp_set_object_terms($post->ID, $base_product_term_slugs, 'wpsc-variation'); // select all variation "products" $variation_items = $wpdb->get_results("SELECT * FROM " . WPSC_TABLE_VARIATION_PROPERTIES . " WHERE `product_id` = '{$original_id}'"); foreach ((array) $variation_items as $variation_item) { // initialize the requisite arrays to empty $variation_ids = array(); $term_data = array(); // make a temporary copy of the product teplate $product_values = $child_product_template; // select all values this "product" is associated with, then loop through them, getting the term id of the variation using the value ID $variation_associations_combinations = $wpdb->get_results("SELECT * FROM " . WPSC_TABLE_VARIATION_COMBINATIONS . " WHERE `priceandstock_id` = '{$variation_item->id}'"); foreach ((array) $variation_associations_combinations as $association) { $variation_id = (int) wpsc_get_meta($association->value_id, 'variation_id', 'wpsc_variation'); // discard any values that are null, as they break the selecting of the terms if ($variation_id > 0 && in_array($association->value_id, $variation_associations)) { $variation_ids[] = $variation_id; } } // if we have more than zero remaining terms, get the term data, then loop through it to convert it to a more useful set of arrays. if (count($variation_ids) > 0 && count($variation_set_associations) == count($variation_ids)) { $combination_terms = get_terms('wpsc-variation', array('hide_empty' => 0, 'include' => implode(",", $variation_ids), 'orderby' => 'parent')); foreach ($combination_terms as $term) { $term_data['ids'][] = $term->term_id; $term_data['slugs'][] = $term->slug; $term_data['names'][] = $term->name; } $product_values['post_title'] .= " (" . implode(", ", $term_data['names']) . ")"; $product_values['post_name'] = sanitize_title($product_values['post_title']); $selected_post = get_posts(array('name' => $product_values['post_name'], 'post_parent' => $post->ID, 'post_type' => "wpsc-product", 'post_status' => 'all', 'suppress_filters' => true)); $selected_post = array_shift($selected_post); $child_product_id = wpsc_get_child_object_in_terms($post->ID, $term_data['ids'], 'wpsc-variation'); $post_data = array(); $post_data['_wpsc_price'] = (double) $variation_item->price; $post_data['_wpsc_stock'] = (double) $variation_item->stock; if (!is_numeric($parent_stock)) { $post_data['_wpsc_stock'] = false; } $post_data['_wpsc_original_variation_id'] = (double) $variation_item->id; // Product Weight $post_data['_wpsc_product_metadata']['weight'] = wpsc_convert_weight($variation_item->weight, $variation_item->weight_unit, "pound", true); $post_data['_wpsc_product_metadata']['display_weight_as'] = $variation_item->weight_unit; $post_data['_wpsc_product_metadata']['weight_unit'] = $variation_item->weight_unit; //file if ($child_product_id == false) { if ($selected_post != null) { $child_product_id = $selected_post->ID; } else { $child_product_id = wp_update_post($product_values); } } else { // sometimes there have been problems saving the variations, this gets the correct product ID if ($selected_post != null && $selected_post->ID != $child_product_id) { $child_product_id = $selected_post->ID; } } if ($child_product_id > 0) { foreach ($post_data as $meta_key => $meta_value) { // prefix all meta keys with _wpsc_ update_post_meta($child_product_id, $meta_key, $meta_value); } wp_set_object_terms($child_product_id, $term_data['slugs'], 'wpsc-variation'); } unset($term_data); } } } delete_option("wpsc-variation_children"); _get_term_hierarchy('wpsc-variation'); delete_option("wpsc_product_category_children"); _get_term_hierarchy('wpsc_product_category'); }
/** * wpsc_edit_product_variations function. * this is the function to make child products using variations * * @access public * @param mixed $product_id * @param mixed $post_data * @return void */ function data_feed_variants_manage($product_id, $post_data) { global $wpdb, $user_ID; $variations = array(); if (!isset($post_data['edit_var_val'])) { $post_data['edit_var_val'] = ''; } $variations = (array) $post_data['edit_var_val']; // bail if the array is empty if (count($variations) < 1) { return false; } // Generate the arrays for variation sets, values and combinations $wpsc_combinator = new wpsc_variation_combinator($variations); // Retrieve the array containing the variation set IDs $variation_sets = $wpsc_combinator->return_variation_sets(); // Retrieve the array containing the combinations of each variation set to be associated with this product. $variation_values = $wpsc_combinator->return_variation_values(); // Retrieve the array containing the combinations of each variation set to be associated with this product. $combinations = $wpsc_combinator->return_combinations(); $variation_sets_and_values = array_merge($variation_sets, $variation_values); wp_set_object_terms($product_id, $variation_sets_and_values, 'wpsc-variation', true); $child_product_template = array('post_author' => $user_ID, 'post_content' => isset($post_data['description']) ? $post_data['description'] : '', 'post_excerpt' => isset($post_data['additional_description']) ? $post_data['additional_description'] : '', 'post_title' => isset($post_data['name']) ? $post_data['name'] : '', 'post_status' => 'inherit', 'post_type' => 'wpsc-product', 'post_name' => isset($post_data['name']) ? sanitize_title($post_data['name']) : '', 'post_parent' => $product_id); $child_product_meta = get_post_custom($product_id); if ($this->isGood($post_data['meta'])) { if ($this->isGood($post_data['meta']['_wpsc_price']) && $child_product_meta['_wpsc_price'] != $post_data['meta']['_wpsc_price']) { $child_product_meta['_wpsc_price'] = array($post_data['meta']['_wpsc_price']); } if ($this->isGood($post_data['meta']['_wpsc_stock']) && $this->isGood($child_product_meta['_wpsc_stock']) && $child_product_meta['_wpsc_stock'] != $post_data['meta']['_wpsc_stock']) { $child_product_meta['_wpsc_stock'] = array($post_data['meta']['_wpsc_stock']); } if ($this->isGood($post_data['meta']['_wpsc_product_metadata']['weight'])) { $tempMeta = unserialize($child_product_meta['_wpsc_product_metadata'][0]); $tempMeta['weight'] = $post_data['meta']['_wpsc_product_metadata']['weight']; $child_product_meta['_wpsc_product_metadata'] = array(serialize($tempMeta)); } } // here we loop through the combinations, get the term data and generate custom product names $child_ids = array(); foreach ($combinations as $combination) { $term_names = array(); $term_ids = array(); $term_slugs = array(); $product_values = $child_product_template; $combination_terms = get_terms('wpsc-variation', array('hide_empty' => 0, 'include' => implode(",", $combination), 'orderby' => 'parent')); foreach ($combination_terms as $term) { $term_ids[] = $term->term_id; $term_slugs[] = $term->slug; $term_names[] = $term->name; } $product_values['post_title'] .= " (" . implode(", ", $term_names) . ")"; $product_values['post_name'] = sanitize_title($product_values['post_title']); /* * wp_set_object_terms($product_id, $variation_sets_and_values, 'wpsc-variation'); * Once we create a new variant, store it's variantion sets and values to a global * array that keeps track by style, at the end of the total update, send the list to * wp_set_object_terms, this should will other variantions. */ if (!isset($this->itemsAdded[$product_id])) { $this->itemsAdded[$product_id] = array(); } $this->itemsAdded[$product_id] = array_merge($this->itemsAdded[$product_id], array_merge($variation_sets, $variation_values)); //This clears all previous variants wp_set_object_terms($product_id, $this->itemsAdded[$product_id], 'wpsc-variation', true); $selected_post = get_posts(array('name' => $product_values['post_name'], 'post_parent' => $product_id, 'post_type' => "wpsc-product", 'post_status' => 'all', 'suppress_filters' => true)); $selected_post = array_shift($selected_post); $child_product_id = wpsc_get_child_object_in_terms($product_id, $term_ids, 'wpsc-variation'); $already_a_variation = true; if ($child_product_id == false) { $already_a_variation = false; if ($selected_post != null) { $child_product_id = $selected_post->ID; } else { $child_product_id = wp_update_post($product_values); } } else { // sometimes there have been problems saving the variations, this gets the correct product ID if ($selected_post != null && $selected_post->ID != $child_product_id) { $child_product_id = $selected_post->ID; } } if ($child_product_id > 0) { array_push($child_ids, $child_product_id); wp_set_object_terms($child_product_id, $term_slugs, 'wpsc-variation'); } //JS - 7.9 - Adding loop to include meta data in child product. foreach ($child_product_meta as $meta_key => $meta_value) { if ($meta_key == "_wpsc_product_metadata") { update_post_meta($child_product_id, $meta_key, unserialize($meta_value[0])); } else { update_post_meta($child_product_id, $meta_key, $meta_value[0]); } } //Adding this to check for a price on variations. Applying the highest price, seems to make the most sense. if (is_array($term_ids)) { $price = array(); foreach ($term_ids as $term_id_price) { $price[] = term_id_price($term_id_price, $child_product_meta["_wpsc_price"][0]); //$price[] = $term_id_price; } rsort($price); $price = $price[0]; if ($price > 0) { update_post_meta($child_product_id, "_wpsc_price", $price); } } } return $child_ids; }
/** * Given array of variation selections this works through the terms and returns the product_id for the matching variation * * For the `$variations` parameter we expect to get two 'term_ids' which correspond to the selections in the variation. * So if we have a blue large shirt and the blue term_id is 8 and the large term_id is 12 we would get an array that looks like. * They keys in the array ( 2 and 9 below ) don't actually matter and are a result of the array items on the frontend in a typical * 'add to cart' action from the frontend of a theme. * * array( * '2' => '8', // this is the blue term_id * '9' => '12', // this is the large term_id * ); * * Here the keys are captured when someone clicks the 'add to cart' button and correspond with ... whatever. They don't really matter for our function. * Really you could pass an array of 2 term_ids here and a $product_id that has variations to match those terms and you'd get back the expected array. * * @since 4.0 * * @param array $variations required The array of variation selections * @param int $product_id required The default product_id * @uses wpsc_get_child_objects_in_terms() Given $product_id and product params this returns the variation product id * @return array $args { * @param int product_id The variation product_id * @param array variation_values The array of variation_values that wpsc_add_to_cart needs to populate $provided_parameters['variation_values'] * } */ function wpsc_get_product_data_from_variations($variations, $product_id) { $variation_values = array(); $original_product_id = $product_id; foreach ((array) $variations as $key => $variation) { $variation_values[(int) $key] = (int) $variation; } if (count($variation_values) > 0) { $variation_product_id = wpsc_get_child_object_in_terms($product_id, $variation_values, 'wpsc-variation'); if ($variation_product_id > 0) { $product_id = $variation_product_id; } } /** This filter is documented in wpsc_get_product_id_from_variations */ $product_id = apply_filters('wpsc_variation_product_id', absint($product_id), $variations, absint($original_product_id)); $return_args = array('product_id' => absint($product_id), 'variation_values' => $variation_values); return $return_args; }