/** * Helper method that updates all the post meta for a product based on it's settings in the WC_Product class. * * @param WC_Product * @param bool $force Force all props to be written even if not changed. This is used during creation. * @since 2.7.0 */ protected function update_post_meta(&$product, $force = false) { $updated_props = array(); $changed_props = array_keys($product->get_changes()); $meta_key_to_props = array('_sku' => 'sku', '_regular_price' => 'regular_price', '_sale_price' => 'sale_price', '_sale_price_dates_from' => 'date_on_sale_from', '_sale_price_dates_to' => 'date_on_sale_to', 'total_sales' => 'total_sales', '_tax_status' => 'tax_status', '_tax_class' => 'tax_class', '_manage_stock' => 'manage_stock', '_backorders' => 'backorders', '_sold_individually' => 'sold_individually', '_weight' => 'weight', '_length' => 'length', '_width' => 'width', '_height' => 'height', '_upsell_ids' => 'upsell_ids', '_crosssell_ids' => 'cross_sell_ids', '_purchase_note' => 'purchase_note', '_default_attributes' => 'default_attributes', '_virtual' => 'virtual', '_downloadable' => 'downloadable', '_product_image_gallery' => 'gallery_image_ids', '_download_limit' => 'download_limit', '_download_expiry' => 'download_expiry', '_thumbnail_id' => 'image_id', '_stock' => 'stock_quantity', '_stock_status' => 'stock_status', '_wc_average_rating' => 'average_rating', '_wc_rating_count' => 'rating_counts', '_wc_review_count' => 'review_count'); foreach ($meta_key_to_props as $meta_key => $prop) { if (!in_array($prop, $changed_props) && !$force) { continue; } $value = $product->{"get_{$prop}"}('edit'); switch ($prop) { case 'virtual': case 'downloadable': case 'manage_stock': case 'sold_individually': $updated = update_post_meta($product->get_id(), $meta_key, wc_bool_to_string($value)); break; case 'gallery_image_ids': $updated = update_post_meta($product->get_id(), $meta_key, implode(',', $value)); break; case 'image_id': if (!empty($value)) { set_post_thumbnail($product->get_id(), $value); } else { delete_post_meta($product->get_id(), '_thumbnail_id'); } $updated = true; break; default: $updated = update_post_meta($product->get_id(), $meta_key, $value); break; } if ($updated) { $updated_props[] = $prop; } } if (in_array('date_on_sale_from', $updated_props) || in_array('date_on_sale_to', $updated_props) || in_array('regular_price', $updated_props) || in_array('sale_price', $updated_props)) { if ($product->is_on_sale()) { update_post_meta($product->get_id(), '_price', $product->get_sale_price()); $product->set_price($product->get_sale_price()); } else { update_post_meta($product->get_id(), '_price', $product->get_regular_price()); $product->set_price($product->get_regular_price()); } } if (in_array('stock_quantity', $updated_props)) { do_action($product->is_type('variation') ? 'woocommerce_variation_set_stock' : 'woocommerce_product_set_stock', $product); } if (in_array('stock_status', $updated_props)) { do_action($product->is_type('variation') ? 'woocommerce_variation_set_stock_status' : 'woocommerce_product_set_stock_status', $product->get_id(), $product->get_stock_status(), $product); } // Update extra data associated with the product. // Like button text or product URL for external products. if (!$this->extra_data_saved) { foreach ($product->get_extra_data_keys() as $key) { $function = 'get_' . $key; if (($force || in_array($key, $changed_props)) && is_callable(array($product, $function))) { update_post_meta($product->get_id(), '_' . $key, $product->{$function}('edit')); } } } }
/** * Update A Single Shipping Zone Method. * * @param WP_REST_Request $request * @return WP_REST_Response|WP_Error */ public function update_item($request) { $gateway = $this->get_gateway($request); if (is_null($gateway)) { return new WP_Error('woocommerce_rest_payment_gateway_invalid', __('Resource does not exist.', 'woocommerce'), array('status' => 404)); } // Update settings if present if (isset($request['settings'])) { $gateway->init_form_fields(); $settings = $gateway->settings; $errors_found = false; foreach ($gateway->form_fields as $key => $field) { if (isset($request['settings'][$key])) { if (is_callable(array($this, 'validate_setting_' . $field['type'] . '_field'))) { $value = $this->{'validate_setting_' . $field['type'] . '_field'}($request['settings'][$key], $field); } else { $value = $this->validate_setting_text_field($request['settings'][$key], $field); } if (is_wp_error($value)) { $errors_found = true; break; } $settings[$key] = $value; } } if ($errors_found) { return new WP_Error('rest_setting_value_invalid', __('An invalid setting value was passed.', 'woocommerce'), array('status' => 400)); } $gateway->settings = $settings; update_option($gateway->get_option_key(), apply_filters('woocommerce_gateway_' . $gateway->id . '_settings_values', $settings, $gateway)); } // Update order if (isset($request['order'])) { $order = (array) get_option('woocommerce_gateway_order'); $order[$gateway->id] = $request['order']; update_option('woocommerce_gateway_order', $order); $gateway->order = absint($request['order']); } // Update if this method is enabled or not. if (isset($request['enabled'])) { $settings = $gateway->settings; $gateway->enabled = $settings['enabled'] = wc_bool_to_string($request['enabled']); update_option($gateway->get_option_key(), apply_filters('woocommerce_gateway_' . $gateway->id . '_settings_values', $settings, $gateway)); } // Update title. if (isset($request['title'])) { $settings = $gateway->settings; $gateway->title = $settings['title'] = $request['title']; update_option($gateway->get_option_key(), apply_filters('woocommerce_gateway_' . $gateway->id . '_settings_values', $settings, $gateway)); } // Update description. if (isset($request['description'])) { $settings = $gateway->settings; $gateway->description = $settings['description'] = $request['description']; update_option($gateway->get_option_key(), apply_filters('woocommerce_gateway_' . $gateway->id . '_settings_values', $settings, $gateway)); } $gateway = $this->prepare_item_for_response($gateway, $request); return rest_ensure_response($gateway); }
/** * Output product visibility options. */ public function product_data_visibility() { global $post, $thepostid, $product_object; if ('product' !== $post->post_type) { return; } $thepostid = $post->ID; $product_object = $thepostid ? wc_get_product($thepostid) : new WC_Product(); $current_visibility = $product_object->get_catalog_visibility(); $current_featured = wc_bool_to_string($product_object->get_featured()); $visibility_options = wc_get_product_visibility_options(); ?> <div class="misc-pub-section" id="catalog-visibility"> <?php _e('Catalog visibility:', 'woocommerce'); ?> <strong id="catalog-visibility-display"><?php echo isset($visibility_options[$current_visibility]) ? esc_html($visibility_options[$current_visibility]) : esc_html($current_visibility); if ('yes' === $current_featured) { echo ', ' . __('Featured', 'woocommerce'); } ?> </strong> <a href="#catalog-visibility" class="edit-catalog-visibility hide-if-no-js"><?php _e('Edit', 'woocommerce'); ?> </a> <div id="catalog-visibility-select" class="hide-if-js"> <input type="hidden" name="current_visibility" id="current_visibility" value="<?php echo esc_attr($current_visibility); ?> " /> <input type="hidden" name="current_featured" id="current_featured" value="<?php echo esc_attr($current_featured); ?> " /> <?php echo '<p>' . __('Choose where this product should be displayed in your catalog. The product will always be accessible directly.', 'woocommerce') . '</p>'; foreach ($visibility_options as $name => $label) { echo '<input type="radio" name="_visibility" id="_visibility_' . esc_attr($name) . '" value="' . esc_attr($name) . '" ' . checked($current_visibility, $name, false) . ' data-label="' . esc_attr($label) . '" /> <label for="_visibility_' . esc_attr($name) . '" class="selectit">' . esc_html($label) . '</label><br />'; } echo '<p>' . __('Enable this option to feature this product.', 'woocommerce') . '</p>'; echo '<input type="checkbox" name="_featured" id="_featured" ' . checked($current_featured, 'yes', false) . ' /> <label for="_featured">' . __('Featured product', 'woocommerce') . '</label><br />'; ?> <p> <a href="#catalog-visibility" class="save-post-visibility hide-if-no-js button"><?php _e('OK', 'woocommerce'); ?> </a> <a href="#catalog-visibility" class="cancel-post-visibility hide-if-no-js"><?php _e('Cancel', 'woocommerce'); ?> </a> </p> </div> </div> <?php }
/** * Helper method that updates all the post meta for a coupon based on it's settings in the WC_Coupon class. * * @param WC_Coupon * @param bool $force Force all props to be written even if not changed. This is used during creation. * @since 2.7.0 */ private function update_post_meta(&$coupon, $force = false) { $updated_props = array(); $changed_props = array_keys($coupon->get_changes()); $meta_key_to_props = array('discount_type' => 'discount_type', 'coupon_amount' => 'amount', 'individual_use' => 'individual_use', 'product_ids' => 'product_ids', 'exclude_product_ids' => 'excluded_product_ids', 'usage_limit' => 'usage_limit', 'usage_limit_per_user' => 'usage_limit_per_user', 'limit_usage_to_x_items' => 'limit_usage_to_x_items', 'usage_count' => 'usage_count', 'expiry_date' => 'date_expires', 'free_shipping' => 'free_shipping', 'product_categories' => 'product_categories', 'exclude_product_categories' => 'excluded_product_categories', 'exclude_sale_items' => 'exclude_sale_items', 'minimum_amount' => 'minimum_amount', 'maximum_amount' => 'maximum_amount', 'customer_email' => 'email_restrictions'); foreach ($meta_key_to_props as $meta_key => $prop) { if (!in_array($prop, $changed_props) && !$force) { continue; } $value = $coupon->{"get_{$prop}"}('edit'); switch ($prop) { case 'individual_use': case 'free_shipping': case 'exclude_sale_items': $updated = update_post_meta($coupon->get_id(), $meta_key, wc_bool_to_string($value)); break; case 'product_ids': case 'excluded_product_ids': $updated = update_post_meta($coupon->get_id(), $meta_key, implode(',', array_filter(array_map('intval', $value)))); break; case 'product_categories': case 'excluded_product_categories': $updated = update_post_meta($coupon->get_id(), $meta_key, array_filter(array_map('intval', $value))); break; case 'email_restrictions': $updated = update_post_meta($coupon->get_id(), $meta_key, array_filter(array_map('sanitize_email', $value))); break; default: $updated = update_post_meta($coupon->get_id(), $meta_key, $value); break; } if ($updated) { $updated_props[] = $prop; } } }
/** * Stores information about whether stock was reduced. * * @param WC_Order|int $order * @param bool $set */ public function set_stock_reduced($order, $set) { $order_id = WC_Order_Factory::get_order_id($order); update_post_meta($order_id, '_order_stock_reduced', wc_bool_to_string($set)); }