Ejemplo n.º 1
0
/**
 * add_to_cart function, used through ajax and in normal page loading.
 * No parameters, returns nothing
 *
 * @uses wpsc_get_product_id_from_variations()              Given array of variation selections returns the variation product id as int
 */
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'] = '';
    $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) $_REQUEST['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($_REQUEST['wpsc_quantity_update']) && is_array($_REQUEST['wpsc_quantity_update'])) {
        $_REQUEST['wpsc_quantity_update'] = $_REQUEST['wpsc_quantity_update'][$product_id];
    }
    if (isset($_REQUEST['variation'])) {
        $return_variation_params = wpsc_get_product_data_from_variations($_REQUEST['variation'], $product_id);
        $product_id = $return_variation_params['product_id'];
        $provided_parameters['variation_values'] = $return_variation_params['variation_values'];
    }
    if (isset($_REQUEST['quantity']) && $_REQUEST['quantity'] > 0 && !isset($_REQUEST['wpsc_quantity_update'])) {
        $provided_parameters['quantity'] = (int) $_REQUEST['quantity'];
    } else {
        if (isset($_REQUEST['wpsc_quantity_update'])) {
            $wpsc_cart->remove_item($_REQUEST['key']);
            $provided_parameters['quantity'] = (int) $_REQUEST['wpsc_quantity_update'];
        }
    }
    if (isset($_REQUEST['is_customisable']) && 'true' == $_REQUEST['is_customisable']) {
        $provided_parameters['is_customisable'] = true;
        if (isset($_REQUEST['custom_text'])) {
            $provided_parameters['custom_message'] = stripslashes($_REQUEST['custom_text']);
        }
        if (isset($_FILES['custom_file'])) {
            $provided_parameters['file_data'] = $_FILES['custom_file'];
        }
    }
    if (isset($_REQUEST['donation_price']) && (double) $_REQUEST['donation_price'] > 0) {
        $provided_parameters['provided_price'] = (double) $_REQUEST['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.', 'wp-e-commerce'));
    } else {
        if ($parameters['quantity'] <= 0) {
            $cart_messages[] = __('Sorry, but you cannot add zero items to your cart', 'wp-e-commerce');
        } 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.', 'wp-e-commerce'), 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, 'wp-e-commerce'), $quantity);
                } else {
                    $cart_messages[] = apply_filters('wpsc_add_to_cart_out_of_stock_message', __('Sorry, but this item is out of stock.', 'wp-e-commerce'), $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 = apply_filters('wpsc_add_to_cart_json_response', $json_response + $output);
        die(json_encode($json_response));
    }
}
Ejemplo n.º 2
0
/**
 * Wrapper for wpsc_get_product_data_from_variations that returns the variation values corresponding to the variation selections
 *
 * See wpsc_get_product_data_from_variations for a full description on what needs to be passed in the $variations params
 *
 * @since 4.0
 *
 * @uses wpsc_get_product_data_from_variations()                        Returns array of data pertaining to product variations
 * @return array      $variation_values                                 The variation values for the selected variation terms, so the term_ids as the array $values
 */
function wpsc_get_variation_values_from_variations($variations, $product_id)
{
    $values = wpsc_get_product_data_from_variations($variations, $product_id);
    /**
     * Allows users to filter the variation values based on the variation selections
     *
     * @since 4.0
     *
     * @param array $variations             The variation selections passed to the core function
     * @param int   $product_id             The default passed product_id
     * @param array $values{
     *      @param  int     product_id         The variation product_id
     *      @param  array   variation_values   The array of variation term_ids based on the selections sent through
     * }
     */
    $variation_values = apply_filters('wpsc_variation_values', $values['variation_values'], $product_id, $values);
    return (array) $variation_values;
}