/** * Set stock level of the product. * * @param mixed $amount (default: null) * @param string $mode can be set, add, or subtract * @return int Stock */ function set_stock($amount = null, $mode = 'set') { // Empty total stock so its refreshed $this->total_stock = ''; // Call parent set_stock return parent::set_stock($amount, $mode); }
/** * Set stock level of the product. * * @param mixed $amount (default: null) * @param string $mode can be set, add, or subtract * @return int Stock */ public function set_stock($amount = null, $mode = 'set') { $this->total_stock = ''; delete_transient('wc_product_total_stock_' . $this->id . WC_Cache_Helper::get_transient_version('product')); return parent::set_stock($amount, $mode); }
/** * Set stock level of the product variation. * @param int $amount * @param bool $force_variation_stock If true, the variation's stock will be updated and not the parents. * @return int * @todo Need to return 0 if is_null? Or something. Should not be just return. */ function set_stock($amount = null, $force_variation_stock = false) { if (is_null($amount)) { return; } if ($amount === '' && $force_variation_stock) { // If amount is an empty string, stock management is being turned off at variation level $this->variation_has_stock = false; $this->stock = ''; unset($this->manage_stock); // Update meta update_post_meta($this->variation_id, '_stock', ''); // Refresh parent prices WC_Product_Variable::sync($this->id); } elseif ($this->variation_has_stock || $force_variation_stock) { // Update stock amount $this->stock = intval($amount); $this->variation_has_stock = true; $this->manage_stock = 'yes'; // Update meta update_post_meta($this->variation_id, '_stock', $this->stock); // Clear total stock transient delete_transient('wc_product_total_stock_' . $this->id); // Check parents out of stock attribute if (!$this->is_in_stock()) { // Check parent $parent_product = get_product($this->id); // Only continue if the parent has backorders off and all children are stock managed and out of stock if (!$parent_product->backorders_allowed() && $parent_product->get_total_stock() <= get_option('woocommerce_notify_no_stock_amount')) { $all_managed = true; if (sizeof($parent_product->get_children()) > 0) { foreach ($parent_product->get_children() as $child_id) { $stock = get_post_meta($child_id, '_stock', true); if ($stock == '') { $all_managed = false; break; } } } if ($all_managed) { $this->set_stock_status('outofstock'); } } } elseif ($this->is_in_stock()) { $this->set_stock_status('instock'); } // Refresh parent prices WC_Product_Variable::sync($this->id); // Trigger action do_action('woocommerce_product_set_stock', $this); return $this->get_stock_quantity(); } else { return parent::set_stock($amount); } }
/** * Set stock level of the product. * * @access public * @param mixed $amount (default: null) * @return int Stock */ function set_stock($amount = null) { global $woocommerce; if ($this->variation_has_stock) { if ($this->managing_stock() && !is_null($amount)) { $this->stock = intval($amount); $this->total_stock = intval($amount); update_post_meta($this->variation_id, '_stock', $this->stock); $woocommerce->clear_product_transients($this->id); // Clear transient // Check parents out of stock attribute if (!$this->is_in_stock()) { // Check parent $parent_product = get_product($this->id); // Only continue if the parent has backorders off if (!$parent_product->backorders_allowed() && $parent_product->get_total_stock() <= 0) { $this->set_stock_status('outofstock'); } } elseif ($this->is_in_stock()) { $this->set_stock_status('instock'); } return apply_filters('woocommerce_stock_amount', $this->stock); } } else { return parent::set_stock($amount); } }
function setStockToOne() { if (!function_exists('wc_update_product_stock')) { require_once 'http://stage.hyzershop.com/wp-content/plugins/woocommerce/includes/wc-product-functions.php'; } $variations = get_posts(array('posts_per_page' => -1, 'post_type' => 'product_variation', 'post_status' => 'publish')); foreach ($variations as $product) { $product_id = $product->ID; $new_stock_level = 1; $product = new WC_Product($product_id); $product->set_stock($new_stock_level); //wc_update_product_stock( $product_id, $new_stock_level ); } }
/** * Set stock level of the product. * * @param mixed $amount (default: null) * @param string $mode can be set, add, or subtract * @return int Stock */ public function set_stock($amount = null, $mode = 'set') { $this->total_stock = ''; delete_transient('wc_product_total_stock_' . $this->id); return parent::set_stock($amount, $mode); }
function woo_insert_update_data($post) { global $wpdb, $woocommerce; $_POST = $post; // Fix: PHP 5.4 $editable_fields = array('_billing_first_name', '_billing_last_name', '_billing_email', '_billing_address_1', '_billing_address_2', '_billing_city', '_billing_state', '_billing_country', '_billing_postcode', '_billing_phone', '_shipping_first_name', '_shipping_last_name', '_shipping_address_1', '_shipping_address_2', '_shipping_city', '_shipping_state', '_shipping_country', '_shipping_postcode', 'order_status'); $new_product = json_decode($_POST['edited']); $edited_prod_ids = array(); $edited_prod_slug = array(); if (!empty($new_product)) { foreach ($new_product as $product) { $edited_prod_ids[] = $product->id; } } //Code for getting the product slugs if (!empty($edited_prod_ids)) { $query_prod_slug = "SELECT id, post_name\n FROM {$wpdb->prefix}posts\n WHERE id IN (" . implode(",", $edited_prod_ids) . ")"; $results_prod_slug = $wpdb->get_results($query_prod_slug, 'ARRAY_A'); $prod_slug_rows = $wpdb->num_rows; if ($prod_slug_rows > 0) { foreach ($results_prod_slug as $result_prod_slug) { $edited_prod_slug[$result_prod_slug['id']] = $result_prod_slug['post_name']; } } } $product_descrip = array(); if (!empty($edited_prod_ids)) { $query_descrip = "SELECT id, post_content, post_excerpt\n FROM {$wpdb->prefix}posts\n WHERE id IN (" . implode(",", $edited_prod_ids) . ")\n GROUP BY id"; $results_descrip = $wpdb->get_results($query_descrip, 'ARRAY_A'); $descrip_rows = $wpdb->num_rows; if ($descrip_rows > 0) { foreach ($results_descrip as $result_descrip) { $product_descrip[$result_descrip['id']] = array(); $product_descrip[$result_descrip['id']]['post_content'] = $result_descrip['post_content']; $product_descrip[$result_descrip['id']]['post_excerpt'] = $result_descrip['post_excerpt']; } } } $result = array('productId' => array()); $post_meta_info = array(); // To get distinct meta_key for Simple Products. => Executed only once $post_meta_info = $wpdb->get_col("SELECT distinct postmeta.meta_key FROM {$wpdb->prefix}postmeta AS postmeta INNER JOIN {$wpdb->prefix}posts AS posts on posts.ID = postmeta.post_id WHERE posts.post_type='product' AND posts.post_status IN ('publish', 'pending', 'draft')"); // To get distinct meta_key for Child Products i.e. Variations. => Executed only once $post_meta_info_variations = $wpdb->get_col("SELECT distinct postmeta.meta_key FROM {$wpdb->prefix}postmeta AS postmeta INNER JOIN {$wpdb->prefix}posts AS posts on posts.ID = postmeta.post_id WHERE posts.post_type='product_variation' AND posts.post_status IN ('publish', 'pending', 'draft') AND posts.post_parent > 0"); // meta_key required for new products, that are entered through Smart Manager // if (count($post_meta_info) <= 0 || count($post_meta_info) < 23) { $post_meta_reqd_keys = array('_edit_last', '_edit_lock', '_regular_price', '_sale_price', '_weight', '_length', '_width', '_height', '_tax_status', '_tax_class', '_stock_status', '_visibility', '_featured', '_sku', '_product_attributes', '_downloadable', '_virtual', '_sale_price_dates_from', '_sale_price_dates_to', '_price', '_stock', '_manage_stock', '_backorders'); // } $post_meta_info = array_unique(array_merge($post_meta_info, $post_meta_reqd_keys)); // for adding the meta_keys if not present if (is_foreachable($new_product)) { $woo_prod_obj = ''; // if ($_POST['SM_IS_WOO21'] == "true" || $_POST['SM_IS_WOO22'] == "true") { // $woo_prod_obj = new WC_Product_Variable(); // } foreach ($new_product as $obj) { if ($_POST['active_module'] == 'Products') { $price = get_price($obj->_regular_price, $obj->_sale_price, $obj->_sale_price_dates_from, $obj->_sale_price_dates_to); $post_content = $post_excerpt = ''; if (isset($obj->id) && $obj->id != '') { //Code for handling the description and addl. description $post_content = !empty($product_descrip[$obj->id]['post_content']) ? $product_descrip[$obj->id]['post_content'] : ''; $post_excerpt = !empty($product_descrip[$obj->id]['post_excerpt']) ? $product_descrip[$obj->id]['post_excerpt'] : ''; $product_custom_fields = get_post_custom($obj->id); // woocommerce uses this method to load product's details before creating WooCommerce Product Object $post = get_post($obj->id); // woocommerce load posts from cache $terms = wp_get_object_terms($obj->id, 'product_type', array('fields' => 'names')); // woocommerce gets product_type using this method $product_type = isset($terms[0]) ? sanitize_title($terms[0]) : 'simple'; if ($product_type == 'variable') { // To unset price fields for Parent Products having variations $obj->_regular_price = ''; $obj->_sale_price = ''; $obj->_price = $price = $product_custom_fields['_min_variation_sale_price'][0] === '' || $product_custom_fields['_min_variation_regular_price'][0] < $product_custom_fields['_min_variation_sale_price'][0] ? $product_custom_fields['_min_variation_regular_price'][0] : $product_custom_fields['_min_variation_sale_price'][0]; } } else { //to not include current date for sales price for new product $obj->_sale_price_dates_from = ''; $obj->_sale_price_dates_to = ''; $price = get_price($obj->_regular_price, $obj->_sale_price, $obj->_sale_price_dates_from, $obj->_sale_price_dates_to); } if (!(empty($obj->post_parent) || $obj->post_parent == '')) { $id = $obj->post_parent; $product_type_parent = wp_get_object_terms($id, 'product_type', array('fields' => 'slugs')); } if ($_POST['SM_IS_WOO16'] == "true") { if ($obj->post_parent > 0 && $product_type_parent[0] != "grouped") { $price = $obj->_regular_price; } } // create an array to be used for updating product's details. add modified value from Smart Manager & rest same as in original post $postarr = array('ID' => isset($obj->id) ? $obj->id : '', 'post_author' => isset($post->post_author) ? $post->post_author : '', 'post_content' => $post_content, 'post_title' => isset($obj->post_title) ? $obj->post_title : '', 'post_name' => !empty($obj->id) && !empty($edited_prod_slug[$obj->id]) ? $edited_prod_slug[$obj->id] : '', 'post_excerpt' => $post_excerpt, 'post_date' => isset($post->post_date) ? $post->post_date : '', 'post_date_gmt' => isset($post->post_date_gmt) ? $post->post_date_gmt : '', 'post_status' => isset($obj->post_status) ? $obj->post_status : '', 'comment_status' => isset($post->comment_status) ? $post->comment_status : 'open', 'ping_status' => isset($post->ping_status) ? $post->ping_status : 'open', 'post_parent' => isset($obj->post_parent) ? $obj->post_parent : $post->post_parent, 'guid' => isset($post->guid) ? $post->guid : site_url() . '/?post_type=product&p=' . $post->ID, 'menu_order' => isset($post->menu_order) ? $post->menu_order : 0, 'post_type' => isset($post->post_type) ? $post->post_type : 'product', 'comment_count' => isset($post->comment_count) ? $post->comment_count : 0, 'ancestors' => isset($post->ancestors) ? $post->ancestors : array(), 'filter' => isset($post->filter) ? $post->filter : 'raw', '_product_attributes' => isset($obj->_product_attributes) ? $obj->_product_attributes : serialize(array()), 'user_ID' => 1, 'action' => 'editpost', 'originalaction' => 'editpost', 'original_post_status' => 'auto-draft', 'auto_draft' => 1, 'post_ID' => $obj->id, 'hidden_post_status' => 'draft', 'hidden_post_visibility' => 'public', '_visibility' => isset($obj->_visibility) ? $obj->_visibility : 'visible', 'original_publish' => 'Publish', 'publish' => 'Publish', 'newproduct_cat' => 'New Product Category Name', 'newproduct_cat_parent' => -1, 'content' => $post_content, 'product-type' => isset($product_type) ? $product_type : 'simple', '_virtual' => isset($obj->_virtual) ? $obj->_virtual : 'no', '_downloadable' => isset($obj->_downloadable) ? $obj->_downloadable : 'no', '_featured' => isset($obj->_featured) ? $obj->_featured : 'no', '_sku' => isset($obj->_sku) ? $obj->_sku : '', '_price' => $price, '_regular_price' => isset($obj->_regular_price) ? $obj->_regular_price : '', '_sale_price' => isset($obj->_sale_price) ? $obj->_sale_price : '', '_sale_price_dates_from' => !empty($obj->_sale_price_dates_from) ? strtotime($obj->_sale_price_dates_from) : '', '_sale_price_dates_to' => !empty($obj->_sale_price_dates_to) ? strtotime($obj->_sale_price_dates_to) : '', '_weight' => isset($obj->_weight) ? $obj->_weight : '', '_length' => isset($obj->_length) ? $obj->_length : '', '_width' => isset($obj->_width) ? $obj->_width : '', '_height' => isset($obj->_height) ? $obj->_height : '', '_tax_status' => isset($obj->_tax_status) ? $obj->_tax_status : 'taxable', '_stock' => isset($obj->_stock) ? $obj->_stock : '', 'excerpt' => $post_excerpt, 'advanced_view' => 1); if (defined('SMPRO') && SMPRO === true || $obj->id == '') { $postarr['_tax_class'] = isset($obj->_tax_class) ? $obj->_tax_class : ''; $postarr['_stock_status'] = isset($obj->_stock_status) ? $obj->_stock_status : 'instock'; $postarr['_manage_stock'] = isset($obj->_manage_stock) ? $obj->_manage_stock : 'no'; $postarr['_backorders'] = isset($obj->_backorders) ? $obj->_backorders : 'no'; } //Code to handle inline editing for custom columns foreach ($obj as $key => $value) { if (!isset($postarr[$key])) { $postarr[$key] = !empty($value) ? $value : ''; } } if ($obj->post_parent == 0 && $obj->product_type != 'variable' || $product_type_parent[0] == "grouped") { $post_id = wp_insert_post($postarr); $post_meta_key = $post_meta_info; } else { $parent_id = $postarr['post_parent']; $post_id = $postarr['ID']; $post_meta_key = $post_meta_info_variations; } array_push($result['productId'], $post_id); foreach ($post_meta_key as $object) { // ================================================================================================ // Code for enabling negative values for inline editing // ================================================================================================ // if ( $object == '_sale_price' || $object == '_price' || $object == '_regular_price' ) { // update_post_meta($wpdb->_real_escape($post_id), $wpdb->_real_escape($object), $wpdb->_real_escape($postarr[$object]) ); // continue; // } // ================================================================================================ if (isset($postarr[$object]) && $postarr[$object] > -1) { // to skip query for blank value //Code to handle the condition for the attribute visibility on product page issue while save action if ($object == '_product_attributes' && isset($product_custom_fields['_product_attributes'][0])) { continue; } if (empty($obj->id) || $obj->id == '') { $query = "INSERT INTO {$wpdb->prefix}postmeta(post_id,meta_key,meta_value) values(" . $wpdb->_real_escape($post_id) . ", '" . $wpdb->_real_escape($object) . "', '" . $wpdb->_real_escape($postarr[$object]) . "')"; $var = $wpdb->query($query); wp_set_object_terms($post_id, 'simple', 'product_type'); } else { //$query = "UPDATE {$wpdb->prefix}postmeta SET meta_value = '".$wpdb->_real_escape($postarr[$object])."' WHERE post_id = " . $wpdb->_real_escape($post_id) . " AND meta_key = '" . $wpdb->_real_escape($object) . "'"; update_post_meta($wpdb->_real_escape($post_id), $wpdb->_real_escape($object), $wpdb->_real_escape($postarr[$object])); if ($object == '_stock') { if ($_POST['SM_IS_WOO21'] == "true" || $_POST['SM_IS_WOO22'] == "true") { $woo_version = defined('WOOCOMMERCE_VERSION') ? WOOCOMMERCE_VERSION : $woocommerce->version; if ($postarr['post_parent'] > 0) { $woo_prod_obj_stock_status = new WC_Product_Variation($post_id); } else { $woo_prod_obj_stock_status = new WC_Product($post_id); } if (version_compare($woo_version, '2.4', ">=")) { $woo_prod_obj_stock_status->check_stock_status(); } else { $woo_prod_obj_stock_status->set_stock($wpdb->_real_escape($postarr[$object])); } } } } } } //Code For updating the parent price of the product if ($parent_id > 0) { if ($_POST['SM_IS_WOO21'] == "true" || $_POST['SM_IS_WOO22'] == "true") { WC_Product_Variable::sync($parent_id); delete_transient('wc_product_children_' . $parent_id); //added in woo24 } else { variable_price_sync($parent_id); } } } elseif ($_POST['active_module'] == 'Orders') { foreach ($obj as $key => $value) { if (in_array($key, $editable_fields)) { if ($key == 'order_status') { // $term_taxonomy_id = get_term_taxonomy_id ( $value ); // $query = "UPDATE {$wpdb->prefix}term_relationships SET term_taxonomy_id = " . $wpdb->_real_escape($term_taxonomy_id) . " WHERE object_id = " . $wpdb->_real_escape($obj->id) . ";"; // if ( $value == 'processing' || $value == 'completed' ) { $order = new WC_Order($obj->id); $order->update_status($value); // } } else { $query = "UPDATE {$wpdb->prefix}postmeta SET meta_value = '" . $wpdb->_real_escape($value) . "' WHERE post_id = " . $wpdb->_real_escape($obj->id) . " AND meta_key = '" . $wpdb->_real_escape($key) . "';"; $wpdb->query($query); } } } } elseif ($_POST['active_module'] == 'Customers') { //Query to get the email and customer_user for all the selected ids $query_email = "SELECT DISTINCT(GROUP_CONCAT( meta_value\n ORDER BY meta_id SEPARATOR '###' ) )AS meta_value,\n GROUP_CONCAT(distinct meta_key\n ORDER BY meta_id SEPARATOR '###' ) AS meta_key\n FROM {$wpdb->prefix}postmeta \n WHERE meta_key in ('_billing_email','_customer_user') \n AND post_id={$wpdb->_real_escape}({$obj->id})"; $result_email = $wpdb->get_results($query_email, 'ARRAY_A'); $email = ""; $users = ""; for ($i = 0; $i < sizeof($result_email); $i++) { $meta_key = explode("###", $result_email[$i]['meta_key']); $meta_value = explode("###", $result_email[$i]['meta_value']); $postmeta[$i] = array_combine($meta_key, $meta_value); $email[$i] = $postmeta[$i]['_billing_email']; $users[$i] = $postmeta[$i]['_customer_user']; unset($meta_key); unset($meta_value); } $email = "'" . implode("','", $email) . "'"; $users = implode(",", $users); //Query for getting al the post ids using the email id if ($users == 0) { $query_ids = "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_value = {$email}"; $id = implode(", ", $wpdb->get_col($query_ids)); } foreach ($obj as $key => $value) { if (in_array($key, $editable_fields)) { if ($users == 0) { $query = "UPDATE {$wpdb->prefix}postmeta SET meta_value = '" . $wpdb->_real_escape($value) . "' WHERE post_id IN ({$id}) AND meta_key = '" . $wpdb->_real_escape($key) . "';"; } elseif ($users > 0) { $key = substr($key, 1); //removing the first underscore from the column name as the columns for p_usermeta are different from that of wp_postmeta //Code for updating the email of the Customer in the wp_users Table if ($key == "billing_email") { $query = "UPDATE {$wpdb->prefix}users SET user_email = '" . $wpdb->_real_escape($value) . "' WHERE id IN ({$users}) ;"; $wpdb->query($query); } $query = "UPDATE {$wpdb->prefix}usermeta SET meta_value = '" . $wpdb->_real_escape($value) . "' WHERE user_id IN ({$users}) AND meta_key = '" . $wpdb->_real_escape($key) . "';"; } $wpdb->query($query); } } } if (empty($obj->id) || $obj->id == '') { if (!isset($result['inserted'])) { $result['inserted'] = 1; $result['insertCnt'] = 1; } else { $result['insertCnt']++; } } else { if (!isset($result['updated'])) { $result['updated'] = 1; $result['updateCnt'] = 1; } else { $result['updateCnt']++; } } } } else { $result = array('inserted' => 0, 'insertCnt' => 0, 'updated' => 0, 'updateCnt' => 0); } //Clearing the transients to handle the proper functioning of the widgets if ($_POST['SM_IS_WOO21'] == "true" || $_POST['SM_IS_WOO22'] == "true") { wc_delete_product_transients(); } else { $woocommerce->clear_product_transients(); } return $result; }