/**
 * wpsc product has stock function
 * @return boolean - true if the product has stock or does not use stock, false if it does not
 */
function wpsc_product_has_stock($id = null)
{
    // maybe do wpsc_clear_stock_claims first?
    if (is_numeric($id) && $id > 0) {
        $id = absint($id);
    } else {
        $id = get_the_ID();
    }
    $stock = get_post_meta($id, '_wpsc_stock', true);
    if ($stock === '') {
        return true;
    }
    $variations = get_children(array("post_type" => "wpsc-product", "post_parent" => $id));
    $filter_name = empty($variations) ? 'wpsc_product_stock' : 'wpsc_product_variation_stock';
    $stock = apply_filters($filter_name, (int) $stock, $id);
    if (!empty($variations)) {
        foreach ($variations as $variation) {
            if (wpsc_product_has_stock($variation->ID)) {
                return true;
            }
        }
    } elseif ($stock > 0) {
        $claimed_query = new WPSC_Claimed_Stock(array('product_id' => $id));
        $claimed_stock = $claimed_query->get_claimed_stock_count();
        if ($stock - $claimed_stock > 0) {
            return true;
        }
    }
    return false;
}
 /**
  * cleanup method, cleans up the cart just before final destruction
  *
  * @access public
  *
  *         No parameters, nothing returned
  */
 function cleanup()
 {
     $claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $this->unique_id));
     $claimed_query->clear_claimed_stock(0);
 }
 /**
  * cleanup method, cleans up the cart just before final destruction
  *
  * @access public
  *
  *         No parameters, nothing returned
  */
 function cleanup()
 {
     wpsc_delete_customer_meta('coupon');
     $claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $this->unique_id));
     $claimed_query->clear_claimed_stock(0);
 }
Exemple #4
0
function wpsc_delete_purchlog($purchlog_id = '')
{
    global $wpdb;
    $deleted = 0;
    if ($purchlog_id == '') {
        $purchlog_id = absint($_GET['purchlog_id']);
        check_admin_referer('delete_purchlog_' . $purchlog_id);
    }
    $purchlog_status = $wpdb->get_var($wpdb->prepare("SELECT `processed` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id`= %d", $purchlog_id));
    if ($purchlog_status == 5 || $purchlog_status == 1) {
        $claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $purchlog_id, 'cart_submitted' => 1));
        $claimed_query->clear_claimed_stock(0);
    }
    $wpdb->query($wpdb->prepare("DELETE FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid` = %d", $purchlog_id));
    $wpdb->query($wpdb->prepare("DELETE FROM `" . WPSC_TABLE_SUBMITTED_FORM_DATA . "` WHERE `log_id` IN (%d)", $purchlog_id));
    $wpdb->query($wpdb->prepare("DELETE FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id` = %d LIMIT 1", $purchlog_id));
    $deleted = 1;
    if (is_numeric($_GET['purchlog_id'])) {
        $sendback = wp_get_referer();
        $sendback = remove_query_arg(array('c', 'id'), $sendback);
        if (isset($deleted)) {
            $sendback = add_query_arg('deleted', $deleted, $sendback);
        }
        wp_redirect($sendback);
        exit;
    }
}
 /**
  * update_claimed_stock method
  * Updates the claimed stock table, to prevent people from having more than the existing stock in their carts
  * @access public
  *
  * no parameters, nothing returned
  */
 function update_claimed_stock()
 {
     global $wpdb;
     if ($this->has_limited_stock == true) {
         $claimed_query = new WPSC_Claimed_Stock(array('product_id' => $this->product_id, 'cart_id' => $this->cart->unique_id));
         $claimed_query->update_claimed_stock($this->quantity);
     }
 }
 public function process_bulk_action()
 {
     global $wpdb;
     $current_action = $this->list_table->current_action();
     do_action('wpsc_sales_log_process_bulk_action', $current_action);
     if (!$current_action || 'download_csv' != $current_action && empty($_REQUEST['post'])) {
         if (!empty($_REQUEST['_wp_http_referer'])) {
             wp_redirect(remove_query_arg(array('_wp_http_referer', '_wpnonce', 'action', 'action2'), stripslashes($_SERVER['REQUEST_URI'])));
             exit;
         }
         unset($_REQUEST['post']);
         return;
     }
     if ('download_csv' == $current_action) {
         $this->download_csv();
         exit;
     }
     $sendback = remove_query_arg(array('_wpnonce', '_wp_http_referer', 'action', 'action2', 'confirm', 'post', 'last_paged'));
     if ('delete' == $current_action) {
         // delete action
         if (empty($_REQUEST['confirm'])) {
             $this->list_table->disable_search_box();
             $this->list_table->disable_bulk_actions();
             $this->list_table->disable_sortable();
             $this->list_table->disable_month_filter();
             $this->list_table->disable_views();
             $this->list_table->set_per_page(0);
             add_action('wpsc_purchase_logs_list_table_before', array($this, 'action_list_table_before'));
             return;
         } else {
             if (empty($_REQUEST['post'])) {
                 return;
             }
             $ids = array_map('intval', $_REQUEST['post']);
             $in = implode(', ', $ids);
             $wpdb->query("DELETE FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE id IN ({$in})");
             $wpdb->query("DELETE FROM " . WPSC_TABLE_CART_CONTENTS . " WHERE purchaseid IN ({$in})");
             $wpdb->query("DELETE FROM " . WPSC_TABLE_SUBMITTED_FORM_DATA . " WHERE log_id IN ({$in})");
             $claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $in));
             $claimed_query->clear_claimed_stock(0);
             $sendback = add_query_arg(array('paged' => $_REQUEST['last_paged'], 'deleted' => count($_REQUEST['post'])), $sendback);
         }
     }
     // change status actions
     if (is_numeric($current_action) && $current_action < 7 && !empty($_REQUEST['post'])) {
         foreach ($_REQUEST['post'] as $id) {
             wpsc_purchlog_edit_status($id, $current_action);
         }
         $sendback = add_query_arg(array('updated' => count($_REQUEST['post'])), $sendback);
     }
     wp_redirect($sendback);
     exit;
 }
/**
 * Get remaining product quantity
 *
 * @since
 * @access public
 *
 * @param int     $product_id                    Cart product ID.
 * @param array   $variations                    Cart item parameters.
 * @param int     $quantity                      Cart object.
 *
 * @uses  wpsc_product_stock    Filters and restricts the product cart quantity.
 */
function wpsc_get_remaining_quantity($product_id, $variations = array(), $quantity = 1)
{
    $stock = get_post_meta($product_id, '_wpsc_stock', true);
    $stock = apply_filters('wpsc_product_stock', $stock, $product_id);
    $output = 0;
    // check to see if the product uses stock
    if (is_numeric($stock)) {
        if ($stock > 0) {
            $claimed_query = new WPSC_Claimed_Stock(array('product_id' => $product_id));
            $claimed_stock = $claimed_query->get_claimed_stock_count();
            $output = $stock - $claimed_stock;
        }
    }
    return $output;
}
 /**
  * Deletes a log from the database.
  *
  * @access  public
  * @since   3.8.9
  *
  * @uses  $wpdb                              Global database instance.
  * @uses  wpsc_is_store_admin()              Check user has admin capabilities.
  * @uses  WPSC_Purchase_Log::delete_cache()  Delete purchaselog cache.
  * @uses  WPSC_Claimed_Stock                 Claimed Stock class.
  *
  * @param   string   $log_id   ID of the log.
  * @return  boolean            Deleted successfully.
  */
 public function delete($log_id = false)
 {
     global $wpdb;
     if (!(isset($this) && get_class($this) == __CLASS__)) {
         _wpsc_doing_it_wrong('WPSC_Purchase_Log::delete', __('WPSC_Purchase_Log::delete() is no longer a static method and should not be called statically.', 'wpsc'), '3.9.0');
     }
     if (false !== $log_id) {
         _wpsc_deprecated_argument(__FUNCTION__, '3.9.0', 'The $log_id param is not used. You must first create an instance of WPSC_Purchase_Log before calling this method.');
     }
     if (!wpsc_is_store_admin()) {
         return false;
     }
     $log_id = $this->get('id');
     if ($log_id > 0) {
         do_action('wpsc_purchase_log_before_delete', $log_id);
         self::delete_cache($log_id);
         // Delete claimed stock
         $purchlog_status = $wpdb->get_var($wpdb->prepare("SELECT `processed` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id`= %d", $log_id));
         if ($purchlog_status == WPSC_Purchase_Log::CLOSED_ORDER || $purchlog_status == WPSC_Purchase_Log::INCOMPLETE_SALE) {
             $claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $log_id, 'cart_submitted' => 1));
             $claimed_query->clear_claimed_stock(0);
         }
         // Delete cart content, submitted data, then purchase log
         $wpdb->query($wpdb->prepare("DELETE FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid` = %d", $log_id));
         $wpdb->query($wpdb->prepare("DELETE FROM `" . WPSC_TABLE_SUBMITTED_FORM_DATA . "` WHERE `log_id` IN (%d)", $log_id));
         $wpdb->query($wpdb->prepare("DELETE FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id` = %d LIMIT 1", $log_id));
         do_action('wpsc_purchase_log_delete', $log_id);
         return true;
     }
     return false;
 }
/**
 * 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;
        }
    }
}