/** * get remaining quantity method * currently only checks remaining stock, in future will do claimed stock and quantity limits * will need to return errors, then, rather than true/false, maybe use the wp_error object? * @access public * * @param integer a product ID key * @param array variations on the product * @return boolean true on sucess, false on failure */ function get_remaining_quantity($product_id, $variations = array(), $quantity = 1) { return wpsc_get_remaining_quantity($product_id, $variations, $quantity); }
/** * Grabs the submitted front end tickets form and adds the products * to the cart */ public function process_front_end_tickets_form() { if (empty($_POST['wpec_tickets_quantity'])) { return; } if (empty($_POST['wpec_tickets_product_id'])) { return; } $products = (array) $_POST['wpec_tickets_product_id']; $quantities = (array) $_POST['wpec_tickets_quantity']; $shortfall = 0; global $wpsc_cart; // We're adding new products, not updating foreach ($products as $product_key => $product_id) { if (absint($quantities[$product_key]) == 0) { continue; } $parameters['quantity'] = absint($quantities[$product_key]); $parameters['variation_values'] = ''; $parameters['is_customisable'] = false; $parameters['custom_message'] = ''; // We *must* set this to avoid a db error @see wpsc_cart_item::save_to_db() // Check stock levels $stock_remaining = wpsc_get_remaining_quantity($product_id); $shortfall = -1 * ($stock_remaining - $parameters['quantity']); // Insufficient stock, but enough to partially fulfill the request? if ($shortfall > 0 && $stock_remaining > 0) { $parameters['quantity'] = $stock_remaining; } $wpsc_cart->set_item((int) $product_id, $parameters); } // If there was a shortfall we need to communicate this to the user if ($shortfall > 0) { add_action('wpsc_before_shopping_cart_page', array($this, 'fulfilment_error')); } }
function wpsc_stock_control_forms() { global $post, $wpdb, $variations_processor, $wpsc_product_defaults; $product_data = get_post_custom($post->ID); $product_data['meta'] = maybe_unserialize($product_data); foreach ($product_data['meta'] as $meta_key => $meta_value) { $product_data['meta'][$meta_key] = $meta_value[0]; } $product_meta = array(); if (!empty($product_data["_wpsc_product_metadata"])) { $product_meta = maybe_unserialize($product_data["_wpsc_product_metadata"][0]); } // 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['notify_when_none_left'] = 0; if (!empty($product_meta['unpublish_when_none_left'])) { $product_meta['notify_when_none_left'] = 1; } } if (!isset($product_meta['unpublish_when_none_left'])) { $product_meta['unpublish_when_none_left'] = ''; } if (!empty($product_meta['unpublish_when_none_left']) && !isset($product_meta['notify_when_none_left'])) { } ?> <label for="wpsc_sku"><abbr title="<?php esc_attr_e('Stock Keeping Unit', 'wpsc'); ?> "><?php esc_html_e('SKU:', 'wpsc'); ?> </abbr></label> <?php if (!isset($product_data['meta']['_wpsc_sku'])) { $product_data['meta']['_wpsc_sku'] = $wpsc_product_defaults['meta']['sku']; } ?> <br /> <input size='32' type='text' class='text' id="wpsc_sku" name='meta[_wpsc_sku]' value='<?php echo esc_html($product_data['meta']['_wpsc_sku']); ?> ' /> <br style="clear:both" /> <?php if (!isset($product_data['meta']['_wpsc_stock'])) { $product_data['meta']['_wpsc_stock'] = ''; } ?> <br /><input class='limited_stock_checkbox' id='add_form_quantity_limited' type='checkbox' value='yes' <?php if (is_numeric($product_data['meta']['_wpsc_stock'])) { echo 'checked="checked"'; } else { echo ''; } ?> name='meta[_wpsc_limited_stock]' /> <label for='add_form_quantity_limited' class='small'><?php esc_html_e('I have limited stock for this Product', 'wpsc'); ?> </label> <?php if ($post->ID > 0) { if (is_numeric($product_data['meta']['_wpsc_stock'])) { ?> <div class='edit_stock' style='display: block;'> <?php } else { ?> <div class='edit_stock' style='display: none;'><?php } ?> <?php if (wpsc_product_has_children($post->ID)) { ?> <?php $stock = wpsc_variations_stock_remaining($post->ID); ?> <p><?php esc_html_e('This Product has variations, to edit the quantity please use the Variation Controls below.', 'wpsc'); ?> </p> <p><?php printf(_n("%s variant item in stock.", "%s variant items in stock.", $stock, 'wpsc'), $stock); ?> </p> <?php } else { ?> <label for="stock_limit_quantity"><?php esc_html_e('Quantity:', 'wpsc'); ?> </label> <input type='text' id="stock_limit_quantity" name='meta[_wpsc_stock]' size='3' value='<?php echo $product_data['meta']['_wpsc_stock']; ?> ' class='stock_limit_quantity' /> <?php $remaining_quantity = wpsc_get_remaining_quantity($post->ID); $reserved_quantity = $product_data['meta']['_wpsc_stock'] - $remaining_quantity; if ($reserved_quantity) { ?> <p><em> <?php printf(_n('%s of them is reserved for pending or recently completed orders.', '%s of them are reserved for pending or recently completed orders.', $reserved_quantity, 'wpsc'), $reserved_quantity); ?> </em></p> <?php } ?> <?php } ?> <div class='notify_when_none_left'> <input type='checkbox' id="notify_when_oos" name='meta[_wpsc_product_metadata][notify_when_none_left]' class='notify_when_oos'<?php checked($product_meta['notify_when_none_left']); ?> /> <label for="notify_when_oos"><?php esc_html_e('Notify site owner if stock runs out', 'wpsc'); ?> </label> </div> <div class='unpublish_when_none_left'> <input type='checkbox' id="unpublish_when_oos" name='meta[_wpsc_product_metadata][unpublish_when_none_left]' class='unpublish_when_oos'<?php checked($product_meta['unpublish_when_none_left']); ?> /> <label for="unpublish_when_oos"><?php esc_html_e('Unpublish this Product if stock runs out', 'wpsc'); ?> </label> <p><em><?php esc_html_e('If stock runs out, this Product will not be available on the shop unless you untick this box or add more stock.', 'wpsc'); ?> </em></p> </div> </div> <?php } else { ?> <div style='display: none;' class='edit_stock'> <?php esc_html_e('Stock Qty', 'wpsc'); ?> <input type='text' name='meta[_wpsc_stock]' value='0' size='10' /> <div style='font-size:9px; padding:5px;'> <input type='checkbox' class='notify_when_oos' name='meta[_wpsc_product_metadata][notify_when_none_left]' /> <?php esc_html_e('Email site owner if this Product runs out of stock', 'wpsc'); ?> <input type='checkbox' class='unpublish_when_oos' name='meta[_wpsc_product_metadata][unpublish_when_none_left]' /> <?php esc_html_e('Set status to Unpublished if this Product runs out of stock', 'wpsc'); ?> </div> </div><?php } }
function wpsc_stock_control_forms() { global $post, $wpdb, $variations_processor, $wpsc_product_defaults; $product_data = get_post_custom($post->ID); $product_data['meta'] = maybe_unserialize($product_data); foreach ($product_data['meta'] as $meta_key => $meta_value) { $product_data['meta'][$meta_key] = $meta_value[0]; } $product_meta = array(); if (!empty($product_data['_wpsc_product_metadata'])) { $product_meta = maybe_unserialize($product_data["_wpsc_product_metadata"][0]); } // 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['notify_when_none_left'] = 0; if (!empty($product_meta['unpublish_when_none_left'])) { $product_meta['notify_when_none_left'] = 1; } } if (!isset($product_meta['unpublish_when_none_left'])) { $product_meta['unpublish_when_none_left'] = ''; } // Display live title if stock is set if (isset($product_data['meta']['_wpsc_stock']) && is_numeric($product_data['meta']['_wpsc_stock'])) { $live_title = '<em id="wpsc_product_stock_metabox_live_title" class="wpsc_metabox_live_title">'; $live_title .= sprintf('<p><span>%s</span> %s</p>', wpsc_format_number($product_data['meta']['_wpsc_stock'], 0), _x('left in stock', 'live preview of stock remaining in admin', 'wp-e-commerce')); $live_title .= '</em>'; echo $live_title; } if (!empty($product_meta['unpublish_when_none_left']) && !isset($product_meta['notify_when_none_left'])) { } ?> <label for="wpsc_sku"><abbr title="<?php esc_attr_e('Stock Keeping Unit', 'wp-e-commerce'); ?> "><?php esc_html_e('SKU:', 'wp-e-commerce'); ?> </abbr></label> <?php if (!isset($product_data['meta']['_wpsc_sku'])) { $product_data['meta']['_wpsc_sku'] = $wpsc_product_defaults['meta']['sku']; } ?> <br /> <input size='32' type='text' class='text' id="wpsc_sku" name='meta[_wpsc_sku]' value='<?php echo esc_html($product_data['meta']['_wpsc_sku']); ?> ' /> <br style="clear:both" /> <?php if (!isset($product_data['meta']['_wpsc_stock'])) { $product_data['meta']['_wpsc_stock'] = ''; } ?> <br /><input class='limited_stock_checkbox' id='add_form_quantity_limited' type='checkbox' value='yes' <?php if (is_numeric($product_data['meta']['_wpsc_stock'])) { echo 'checked="checked"'; } else { echo ''; } ?> name='meta[_wpsc_limited_stock]' /> <label for='add_form_quantity_limited' class='small'><?php esc_html_e('Product has limited stock', 'wp-e-commerce'); ?> </label> <?php if ($post->ID > 0) { if (is_numeric($product_data['meta']['_wpsc_stock'])) { ?> <div class='edit_stock' style='display: block;'> <?php } else { ?> <div class='edit_stock' style='display: none;'><?php } ?> <?php if (wpsc_product_has_children($post->ID)) { ?> <?php $stock = wpsc_variations_stock_remaining($post->ID); ?> <p><?php echo sprintf(__('This product has variations. To edit the quantity, please use the <a href="%s">Variation Controls</a> below.', 'wp-e-commerce'), '#wpsc_product_variation_forms'); ?> </p> <p><?php printf(_n("%s variant item in stock.", "%s variant items in stock.", $stock, 'wp-e-commerce'), $stock); ?> </p> <?php } else { ?> <div style="margin-bottom:20px;"> <label for="stock_limit_quantity"><?php esc_html_e('Quantity in stock', 'wp-e-commerce'); ?> </label> <input type='number' min="0" step="1" style="width:80px; margin-left:50px;" id="stock_limit_quantity" name='meta[_wpsc_stock]' size='3' value='<?php echo absint($product_data['meta']['_wpsc_stock']); ?> ' class='stock_limit_quantity' /> </div> <?php $remaining_quantity = wpsc_get_remaining_quantity($post->ID); $reserved_quantity = $product_data['meta']['_wpsc_stock'] - $remaining_quantity; if ($reserved_quantity) { echo '<p><em>'; printf(_n('%s of them is reserved for pending or recently completed orders.', '%s of them are reserved for pending or recently completed orders.', $reserved_quantity, 'wp-e-commerce'), $reserved_quantity); echo '</em></p>'; } ?> <?php } ?> <p><?php esc_html_e('When stock reduces to zero:', 'wp-e-commerce'); ?> </p> <div class='notify_when_none_left'> <input type='checkbox' id="notify_when_oos" name='meta[_wpsc_product_metadata][notify_when_none_left]' class='notify_when_oos'<?php checked($product_meta['notify_when_none_left']); ?> /> <label for="notify_when_oos"><?php esc_html_e('Notify site owner via email', 'wp-e-commerce'); ?> </label> </div> <div class='unpublish_when_none_left'> <input type='checkbox' id="unpublish_when_oos" name='meta[_wpsc_product_metadata][unpublish_when_none_left]' class='unpublish_when_oos'<?php checked($product_meta['unpublish_when_none_left']); ?> /> <label for="unpublish_when_oos"><?php esc_html_e('Unpublish product from website', 'wp-e-commerce'); ?> </label> </div> </div> <?php } else { ?> <div style='display: none;' class='edit_stock'> <?php esc_html_e('Stock Qty', 'wp-e-commerce'); ?> <input type='text' name='meta[_wpsc_stock]' value='0' size='10' /> <div style='font-size:9px; padding:5px;'> <input type='checkbox' class='notify_when_oos' name='meta[_wpsc_product_metadata][notify_when_none_left]' /> <?php esc_html_e('Email site owner if this Product runs out of stock', 'wp-e-commerce'); ?> <input type='checkbox' class='unpublish_when_oos' name='meta[_wpsc_product_metadata][unpublish_when_none_left]' /> <?php esc_html_e('Set status to Unpublished if this Product runs out of stock', 'wp-e-commerce'); ?> </div> </div> <?php } wp_nonce_field('update', 'wpsc_product_stock_nonce'); }
function wpsc_stock_control_forms() { global $post, $wpdb, $variations_processor, $wpsc_product_defaults; $product_data = get_post_custom($post->ID); $product_data['meta'] = maybe_unserialize($product_data); foreach ($product_data['meta'] as $meta_key => $meta_value) { $product_data['meta'][$meta_key] = $meta_value[0]; } $product_meta = array(); if (!empty($product_data["_wpsc_product_metadata"])) { $product_meta = maybe_unserialize($product_data["_wpsc_product_metadata"][0]); } if (!isset($product_meta['unpublish_when_none_left'])) { $product_meta['unpublish_when_none_left'] = ''; } ?> <label for="wpsc_sku"><abbr title="<?php _e('Stock Keeping Unit', 'wpsc'); ?> "><?php _e('SKU:', 'wpsc'); ?> </abbr></label> <?php if (!isset($product_data['meta']['_wpsc_sku'])) { $product_data['meta']['_wpsc_sku'] = $wpsc_product_defaults['meta']['sku']; } ?> <br /> <input size='32' type='text' class='text' id="wpsc_sku" name='meta[_wpsc_sku]' value='<?php echo htmlentities(stripslashes($product_data['meta']['_wpsc_sku']), ENT_QUOTES, 'UTF-8'); ?> ' /> <br style="clear:both" /> <?php if (!isset($product_data['meta']['_wpsc_stock'])) { $product_data['meta']['_wpsc_stock'] = ''; } ?> <br /><input class='limited_stock_checkbox' id='add_form_quantity_limited' type='checkbox' value='yes' <?php if (is_numeric($product_data['meta']['_wpsc_stock'])) { echo 'checked="checked"'; } else { echo ''; } ?> name='meta[_wpsc_limited_stock]' /> <label for='add_form_quantity_limited' class='small'><?php _e('Este produto tem estoque limitado', 'wpsc'); ?> </label> <?php if ($post->ID > 0) { if (is_numeric($product_data['meta']['_wpsc_stock'])) { ?> <div class='edit_stock' style='display: block;'> <?php } else { ?> <div class='edit_stock' style='display: none;'><?php } ?> <?php if (wpsc_product_has_children($post->ID)) { ?> <?php $stock = wpsc_variations_stock_remaining($post->ID); ?> <p><?php _e('Este produto possui variações, para editar as quantidades use o controle de variações ao lado.', 'wpsc'); ?> </p> <p><?php printf(_n("%s variant item in stock.", "%s ítens de variação em estoque.", $stock, 'wpsc'), $stock); ?> </p> <?php } else { ?> <label for="stock_limit_quantity"><?php _e('Quantity:', 'wpsc'); ?> </label> <input type='text' id="stock_limit_quantity" name='meta[_wpsc_stock]' size='3' value='<?php echo $product_data['meta']['_wpsc_stock']; ?> ' class='stock_limit_quantity' /> <?php $remaining_quantity = wpsc_get_remaining_quantity($post->ID); $reserved_quantity = $product_data['meta']['_wpsc_stock'] - $remaining_quantity; if ($reserved_quantity) { ?> <p><em> <?php printf(_n('%s of them is reserved for pending or recently completed orders.', '%s of them are reserved for pending or recently completed orders.', $reserved_quantity, 'wpsc'), $reserved_quantity); ?> </em></p> <?php } ?> <?php } ?> <div class='unpublish_when_none_left'> <input type='checkbox' id="inform_when_oos" name='meta[_wpsc_product_metadata][unpublish_when_none_left]' class='inform_when_oos'<?php if ($product_meta['unpublish_when_none_left'] == 1) { echo ' checked="checked"'; } ?> /> <label for="inform_when_oos"><?php _e('Notifique o administrador caso o produto fique fora de estoque', 'wpsc'); ?> </label> </div> <p><em><?php _e('', 'wpsc'); ?> </em></p> </div> <?php } else { ?> <div style='display: none;' class='edit_stock'> <?php _e('Stock Qty', 'wpsc'); ?> <input type='text' name='meta[_wpsc_stock]' value='0' size='10' /> <div style='font-size:9px; padding:5px;'> <input type='checkbox' class='inform_when_oos' name='meta[_wpsc_product_metadata][unpublish_when_none_left]' /> <?php _e('If this Product runs out of stock set status to Unpublished & email site owner', 'wpsc'); ?> </div> </div><?php } }