Пример #1
0
/**
 * Update product stats when a purchase log containing it changes status
 *
 * @since 3.8.13
 *
 * @param int               $log_id     Purchase Log ID
 * @param int               $new_status New status
 * @param int               $old_status Old status
 * @param WPSC_Purchase_Log $log        Purchase Log
 */
function _wpsc_action_update_product_stats($log_id, $new_status, $old_status, $log)
{
    $cart_contents = $log->get_cart_contents();
    $new_status_completed = $log->is_transaction_completed();
    $old_status_completed = WPSC_Purchase_Log::is_order_status_completed($old_status);
    if ($new_status_completed && !$old_status_completed) {
        // if the order went through without any trouble, then it's a positive thing!
        $yay_or_boo = 1;
    } elseif (!$new_status_completed && $old_status_completed) {
        // if the order is declined or invalid, sad face :(
        $yay_or_boo = -1;
    } else {
        // Not one of the above options then we will be indifferent
        $yay_or_boo = 0;
    }
    // this dramatic mood swing affects the stats of each products in the cart
    foreach ($cart_contents as $cart_item) {
        $product = new WPSC_Product($cart_item->prodid);
        if ($product->exists()) {
            $diff_sales = $yay_or_boo * (int) $cart_item->quantity;
            $diff_earnings = $yay_or_boo * (int) $cart_item->price * (int) $cart_item->quantity;
            $product->sales += $diff_sales;
            $product->earnings += $diff_earnings;
            // if this product has parent, make the same changes to the parent
            if ($product->post->post_parent) {
                $parent = WPSC_Product::get_instance($product->post->post_parent);
                $parent->sales += $diff_sales;
                $parent->earnings += $diff_earnings;
            }
        }
    }
}
Пример #2
0
/**
 * Return the array containing arguments to generate the "Add to cart" form.
 *
 * The returned array can then be used with {@link wpsc_get_form_output()} to
 * generate the HTML output.
 *
 * @since  0.1
 * @uses   apply_filters() Applies 'wpsc_add_to_cart_button_icon' filter. Set the value to an empty value to disable the icon
 * @uses   apply_filters() Applies 'wpsc_add_to_cart_button_title' filter. The return value must be escaped already.
 * @uses   apply_filters() Applies 'wpsc_get_add_to_cart_form_args' filter. Use this filter to add more fields
 * @uses   wpsc_get_product_id() Get current product ID in the loop
 * @uses   wpsc_get_cart_url() Get cart URL for the form action
 * @uses   WPSC_Product::get_instance() get the WPSC_Product object to fetch variation sets and terms
 *
 * @param  int   $id Optional. Product ID. Defaults to the current product in the loop.
 * @return array     Form argument array
 */
function wpsc_get_add_to_cart_form_args($id = null)
{
    if (!$id) {
        $id = wpsc_get_product_id();
    }
    $args = array('class' => 'wpsc-form wpsc-form-horizontal wpsc-add-to-cart-form', 'action' => wpsc_get_cart_url("add/{$id}"), 'id' => "wpsc-add-to-cart-form-{$id}", 'fields' => array(array('name' => 'quantity', 'type' => 'textfield', 'title' => __('Quantity', 'wpsc'), 'value' => 1)));
    // generate the variation dropdown menus
    $product = WPSC_Product::get_instance($id);
    foreach ($product->variation_sets as $variation_set_id => $title) {
        $variation_terms = $product->variation_terms[$variation_set_id];
        $args['fields'][] = array('name' => "wpsc_product_variations[{$variation_set_id}]", 'type' => 'select', 'options' => $variation_terms, 'title' => $title);
    }
    // form action section contains the button and hidden values
    $args['form_actions'] = array(array('type' => 'button', 'primary' => true, 'icon' => apply_filters('wpsc_add_to_cart_button_icon', array('shopping-cart', 'white')), 'title' => apply_filters('wpsc_add_to_cart_button_title', __('Add to Cart', 'wpsc'))), array('type' => 'hidden', 'name' => '_wp_http_referer', 'value' => home_url($_SERVER['REQUEST_URI'])), array('type' => 'hidden', 'name' => '_wp_nonce', 'value' => wp_create_nonce("wpsc-add-to-cart-{$id}")));
    $args = apply_filters('wpsc_get_add_to_cart_form_args', $args);
    return $args;
}
Пример #3
0
function wpsc_product_has_stock($id = null)
{
    if (is_null($id)) {
        $id = wpsc_get_product_id();
    }
    $product = WPSC_Product::get_instance($id);
    return $product->has_stock;
}
 /**
  * Get overall sales and earning stats for just one product
  *
  * @since 3.8.14
  * @param  int $id ID of the product
  * @return array   Array containing 'sales' and 'earnings' stats
  */
 public static function get_stats_for_product($id, $args = '')
 {
     $product = WPSC_Product::get_instance($id);
     // if this product has variations
     if ($product->has_variations) {
         // get total stats of variations
         $args['products'] = $product->variations;
     } else {
         // otherwise, get stats of only this product
         $args['products'] = array($product);
     }
     return self::fetch_stats($args);
 }
Пример #5
0
function wpsc_get_product_you_save($product_id = null, $format = false, $from_text = true)
{
    if (empty($product_id)) {
        $product_id = wpsc_get_product_id();
    }
    if (!$format) {
        /* translators: %1$s: saving amount, %2$s: saving percent */
        $format = _x('%1$s (%2$s)', 'product saving format', 'wpsc');
    }
    $product = WPSC_Product::get_instance($product_id);
    $saving = wpsc_format_currency($product->saving);
    $saving_percent = sprintf(_x('%1$s%%', 'product saving percent', 'wpsc'), $product->saving_percent);
    $saving_text = sprintf($format, $saving, $saving_percent);
    if ($from_text && $product->has_various_savings) {
        $saving_text = _wpsc_get_from_text($saving_text);
    }
    return apply_filters('wpsc_get_product_you_save', $saving_text, $product_id, $format, $from_text);
}
Пример #6
0
 public function _callback_update_quantity()
 {
     global $wpsc_cart;
     if (!wp_verify_nonce($_REQUEST['_wp_nonce'], 'wpsc-cart-update')) {
         wp_die(__('Request expired. Please try updating the items in your cart again.', 'wpsc'));
     }
     $changed = 0;
     $has_errors = false;
     extract($_REQUEST, EXTR_SKIP);
     foreach ($wpsc_cart->cart_items as $key => &$item) {
         if (isset($quantity[$key]) && $quantity[$key] != $item->quantity) {
             $product = get_post($item->product_id);
             if (!is_numeric($quantity[$key])) {
                 $message = sprintf(__('Invalid quantity for %s.', 'wpsc'), $product->post_title);
                 $this->message_collection->add($message, 'error');
                 continue;
             }
             if ($quantity[$key] > $item->quantity) {
                 $product = WPSC_Product::get_instance($item->product_id);
                 if (!$product->has_stock) {
                     $message = __("Sorry, all the remaining stock of %s has been claimed. Now you can only checkout with the current number of that item in your cart.", 'wpsc');
                     $this->message_collection->add(sprintf($message, $product->post_title), 'error');
                     $has_errors = true;
                     continue;
                 } elseif ($product->has_limited_stock && $product->stock < $item->quantity) {
                     $message = __('Sorry, but the quantity you just specified is larger than the available stock of %s. Besides the current number of that product in your cart, you can only add %d more.', 'wpsc');
                     $this->message_collection->add(sprintf($message, $product->post_title, $product->stock), 'error');
                     $has_errors = true;
                     continue;
                 }
             }
             $item->quantity = $quantity[$key];
             $item->refresh_item();
             $changed++;
         }
     }
     $wpsc_cart->clear_cache();
     if (!isset($_POST['update_quantity']) && !$has_errors) {
         wp_redirect(wpsc_get_checkout_url());
         exit;
     }
     if ($changed) {
         $message = _n('You just successfully updated the quantity for %d item.', 'You just successfully updated the quantity for %d items.', $changed, 'wpsc');
         $this->message_collection->add(sprintf($message, $changed), 'success');
     }
 }