/** * Main function for returning orders, uses the WC_Order_Factory class. * * @since 2.2 * @param mixed $the_order Post object or post ID of the order. * @return WC_Order|WC_Refund */ function wc_get_order($the_order = false) { if (!did_action('woocommerce_init')) { wc_doing_it_wrong(__FUNCTION__, __('wc_get_order should not be called before the woocommerce_init action.', 'woocommerce'), '2.5'); return false; } return WC()->order_factory->get_order($the_order); }
/** * __set function. * @param mixed $property * @param mixed $key */ public function __set($key, $value) { wc_doing_it_wrong($key, 'Customer properties should not be set directly.', '2.7'); $key = $this->filter_legacy_key($key); if (is_callable(array($this, "set_{$key}"))) { $this->{"set_{$key}"}($value); } }
/** * Main function for returning products, uses the WC_Product_Factory class. * * @since 2.2.0 * * @param mixed $the_product Post object or post ID of the product. * @param array $deprecated * @return WC_Product|null */ function wc_get_product($the_product = false, $deprecated = array()) { if (!did_action('woocommerce_init')) { wc_doing_it_wrong(__FUNCTION__, __('wc_get_product should not be called before the woocommerce_init action.', 'woocommerce'), '2.5'); return false; } return WC()->product_factory->get_product($the_product); }
/** * Returns the contents of the cart in an array. * * @return array contents of the cart */ public function get_cart() { if (!did_action('wp_loaded')) { wc_doing_it_wrong(__FUNCTION__, __('Get cart should not be called before the wp_loaded action.', 'woocommerce'), '2.3'); } if (!did_action('woocommerce_cart_loaded_from_session')) { $this->get_cart_from_session(); } return array_filter((array) $this->cart_contents); }
/** * Unserializing instances of this class is forbidden. */ public function __wakeup() { wc_doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'woocommerce'), '2.1'); }
/** * Magic __get method for backwards compatibility. Maps legacy vars to new getters. * * @param string $key Key name. * @return mixed */ public function __get($key) { if ('post_type' === $key) { return $this->post_type; } wc_doing_it_wrong($key, __('Product properties should not be accessed directly.', 'woocommerce'), '2.7'); switch ($key) { case 'id': $value = $this->is_type('variation') ? $this->get_parent_id() : $this->get_id(); break; case 'variation_id': $value = $this->get_id(); break; case 'product_attributes': $value = isset($this->data['attributes']) ? $this->data['attributes'] : ''; break; case 'visibility': $value = $this->get_catalog_visibility(); break; case 'sale_price_dates_from': $value = $this->get_date_on_sale_from(); break; case 'sale_price_dates_to': $value = $this->get_date_on_sale_to(); break; case 'post': $value = get_post($this->get_id()); break; case 'download_type': return 'standard'; break; case 'product_image_gallery': $value = $this->get_gallery_image_ids(); break; case 'variation_shipping_class': case 'shipping_class': $value = $this->get_shipping_class(); break; case 'total_stock': $value = $this->get_total_stock(); break; case 'downloadable': case 'virtual': case 'manage_stock': case 'featured': case 'sold_individually': $value = $this->{"get_{$key}"}() ? 'yes' : 'no'; break; case 'crosssell_ids': $value = $this->get_cross_sell_ids(); break; case 'upsell_ids': $value = $this->get_upsell_ids(); break; case 'parent': $value = wc_get_product($this->get_parent_id()); break; case 'variation_data': $value = wc_get_product_variation_attributes($this->get_id()); break; case 'variation_has_stock': $value = $this->managing_stock(); break; case 'variation_shipping_class_id': $value = $this->get_shipping_class_id(); break; default: if (in_array($key, array_keys($this->data))) { $value = $this->{"get_{$key}"}(); } else { $value = get_post_meta($this->id, '_' . $key, true); } break; } return $value; }
/** * Magic __get method for backwards compatibility. * * @param string $key * @return mixed */ public function __get($key) { wc_doing_it_wrong($key, 'Refund properties should not be accessed directly.', '2.7'); /** * Maps legacy vars to new getters. */ if ('reason' === $key) { return $this->get_reason(); } elseif ('refund_amount' === $key) { return $this->get_amount(); } return parent::__get($key); }
/** * Save product price. * * This is a private function (internal use ONLY) used until a data manipulation api is built. * * @deprecated 2.7.0 * @param int $product_id * @param float $regular_price * @param float $sale_price * @param string $date_from * @param string $date_to */ function _wc_save_product_price($product_id, $regular_price, $sale_price = '', $date_from = '', $date_to = '') { wc_doing_it_wrong('_wc_save_product_price()', 'This function is not for developer use and is deprecated.', '2.7'); $product_id = absint($product_id); $regular_price = wc_format_decimal($regular_price); $sale_price = '' === $sale_price ? '' : wc_format_decimal($sale_price); $date_from = wc_clean($date_from); $date_to = wc_clean($date_to); update_post_meta($product_id, '_regular_price', $regular_price); update_post_meta($product_id, '_sale_price', $sale_price); // Save Dates update_post_meta($product_id, '_sale_price_dates_from', $date_from ? strtotime($date_from) : ''); update_post_meta($product_id, '_sale_price_dates_to', $date_to ? strtotime($date_to) : ''); if ($date_to && !$date_from) { $date_from = strtotime('NOW', current_time('timestamp')); update_post_meta($product_id, '_sale_price_dates_from', $date_from); } // Update price if on sale if ('' !== $sale_price && '' === $date_to && '' === $date_from) { update_post_meta($product_id, '_price', $sale_price); } else { update_post_meta($product_id, '_price', $regular_price); } if ('' !== $sale_price && $date_from && strtotime($date_from) < strtotime('NOW', current_time('timestamp'))) { update_post_meta($product_id, '_price', $sale_price); } if ($date_to && strtotime($date_to) < strtotime('NOW', current_time('timestamp'))) { update_post_meta($product_id, '_price', $regular_price); update_post_meta($product_id, '_sale_price_dates_from', ''); update_post_meta($product_id, '_sale_price_dates_to', ''); } }
/** * Magic __get method for backwards compatibility. Maps legacy vars to new getters. * @param string $key * @return mixed */ public function __get($key) { wc_doing_it_wrong($key, 'Coupon properties should not be accessed directly.', '2.7'); switch ($key) { case 'id': $value = $this->get_id(); break; case 'exists': $value = $this->get_id() > 0 ? true : false; break; case 'coupon_custom_fields': $legacy_custom_fields = array(); $custom_fields = $this->get_id() ? $this->get_meta_data() : array(); if (!empty($custom_fields)) { foreach ($custom_fields as $cf_value) { // legacy only supports 1 key $legacy_custom_fields[$cf_value->key][0] = $cf_value->value; } } $value = $legacy_custom_fields; break; case 'type': case 'discount_type': $value = $this->get_discount_type(); break; case 'amount': $value = $this->get_amount(); break; case 'code': $value = $this->get_code(); break; case 'individual_use': $value = true === $this->get_individual_use() ? 'yes' : 'no'; break; case 'product_ids': $value = $this->get_product_ids(); break; case 'exclude_product_ids': $value = $this->get_excluded_product_ids(); break; case 'usage_limit': $value = $this->get_usage_limit(); break; case 'usage_limit_per_user': $value = $this->get_usage_limit_per_user(); break; case 'limit_usage_to_x_items': $value = $this->get_limit_usage_to_x_items(); break; case 'usage_count': $value = $this->get_usage_count(); break; case 'expiry_date': $value = $this->get_date_expires(); break; case 'product_categories': $value = $this->get_product_categories(); break; case 'exclude_product_categories': $value = $this->get_excluded_product_categories(); break; case 'minimum_amount': $value = $this->get_minimum_amount(); break; case 'maximum_amount': $value = $this->get_maximum_amount(); break; case 'customer_email': $value = $this->get_email_restrictions(); break; default: $value = ''; break; } return $value; }
/** * Magic __get method for backwards compatibility. * @param string $key * @return mixed */ public function __get($key) { wc_doing_it_wrong($key, 'Order properties should not be accessed directly.', '2.7'); if ('completed_date' === $key) { return $this->get_date_completed(); } elseif ('paid_date' === $key) { return $this->get_date_paid(); } elseif ('modified_date' === $key) { return $this->get_date_modified(); } elseif ('order_date' === $key) { return $this->get_date_created(); } elseif ('id' === $key) { return $this->get_id(); } elseif ('post' === $key) { return get_post($this->get_id()); } elseif ('status' === $key || 'post_status' === $key) { return 'wc-' . $this->get_status(); } elseif ('customer_message' === $key || 'customer_note' === $key) { return $this->get_customer_note(); } elseif (in_array($key, array('user_id', 'customer_user'))) { return $this->get_customer_id(); } elseif ('tax_display_cart' === $key) { return get_option('woocommerce_tax_display_cart'); } elseif ('display_totals_ex_tax' === $key) { return 'excl' === get_option('woocommerce_tax_display_cart'); } elseif ('display_cart_ex_tax' === $key) { return 'excl' === get_option('woocommerce_tax_display_cart'); } elseif ('cart_discount' === $key) { return $this->get_discount(); } elseif ('cart_discount_tax' === $key) { return $this->get_discount_tax(); } elseif ('order_tax' === $key) { return $this->get_cart_tax(); } elseif ('order_shipping_tax' === $key) { return $this->get_shipping_tax(); } elseif ('order_shipping' === $key) { return $this->get_shipping_total(); } elseif ('order_total' === $key) { return $this->get_total(); } elseif ('order_type' === $key) { return $this->get_type(); } elseif ('order_currency' === $key) { return $this->get_currency(); } elseif ('order_version' === $key) { return $this->get_version(); } elseif (is_callable(array($this, "get_{$key}"))) { return $this->{"get_{$key}"}(); } else { return get_post_meta($this->get_id(), '_' . $key, true); } }
/** * Get other templates (e.g. product attributes) passing attributes and including the file. * * @access public * @param string $template_name * @param array $args (default: array()) * @param string $template_path (default: '') * @param string $default_path (default: '') */ function wc_get_template($template_name, $args = array(), $template_path = '', $default_path = '') { if (!empty($args) && is_array($args)) { extract($args); } $located = wc_locate_template($template_name, $template_path, $default_path); if (!file_exists($located)) { wc_doing_it_wrong(__FUNCTION__, sprintf(__('%s does not exist.', 'woocommerce'), '<code>' . $located . '</code>'), '2.1'); return; } // Allow 3rd party plugin filter template file from their plugin. $located = apply_filters('wc_get_template', $located, $template_name, $args, $template_path, $default_path); do_action('woocommerce_before_template_part', $template_name, $template_path, $located, $args); include $located; do_action('woocommerce_after_template_part', $template_name, $template_path, $located, $args); }
/** * Returns all queued notices, optionally filtered by a notice type. * * @since 2.1 * @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] * @return array|mixed */ function wc_get_notices($notice_type = '') { if (!did_action('woocommerce_init')) { wc_doing_it_wrong(__FUNCTION__, __('This function should not be called before woocommerce_init.', 'woocommerce'), '2.3'); return; } $all_notices = WC()->session->get('wc_notices', array()); if (empty($notice_type)) { $notices = $all_notices; } elseif (isset($all_notices[$notice_type])) { $notices = $all_notices[$notice_type]; } else { $notices = array(); } return $notices; }
/** * Developers can programically return coupons. This function will read those values into our WC_Coupon class. * @since 2.7.0 * @param string $code Coupon code * @param array $coupon Array of coupon properties */ public function read_manual_coupon($code, $coupon) { foreach ($coupon as $key => $value) { switch ($key) { case 'excluded_product_ids': case 'exclude_product_ids': if (!is_array($coupon[$key])) { wc_doing_it_wrong($key, $key . ' should be an array instead of a string.', '2.7'); $coupon['excluded_product_ids'] = wc_string_to_array($value); } break; case 'exclude_product_categories': case 'excluded_product_categories': if (!is_array($coupon[$key])) { wc_doing_it_wrong($key, $key . ' should be an array instead of a string.', '2.7'); $coupon['excluded_product_categories'] = wc_string_to_array($value); } break; case 'product_ids': if (!is_array($coupon[$key])) { wc_doing_it_wrong($key, $key . ' should be an array instead of a string.', '2.7'); $coupon[$key] = wc_string_to_array($value); } break; case 'individual_use': case 'free_shipping': case 'exclude_sale_items': if (!is_bool($coupon[$key])) { wc_doing_it_wrong($key, $key . ' should be true or false instead of yes or no.', '2.7'); $coupon[$key] = wc_string_to_bool($value); } break; case 'expiry_date': $coupon['date_expires'] = $value; break; } } $this->set_code($code); $this->set_props($coupon); }
/** * Register the routes for the objects of the controller. */ public function register_routes() { wc_doing_it_wrong('WP_REST_Controller::register_routes', __('The register_routes() method must be overriden', 'woocommerce'), 'WPAPI-2.0'); }