/** * Prepare a single coupon output for response. * * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $data */ public function prepare_item_for_response($post, $request) { global $wpdb; // Get the coupon code. $code = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $post->ID)); $coupon = new WC_Coupon($code); $data = array('id' => $coupon->id, 'code' => $coupon->code, 'date_created' => wc_rest_prepare_date_response($post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($post->post_modified_gmt), 'discount_type' => $coupon->type, 'description' => $post->post_excerpt, 'amount' => wc_format_decimal($coupon->coupon_amount, 2), 'expiry_date' => !empty($coupon->expiry_date) ? wc_rest_prepare_date_response($coupon->expiry_date) : null, 'usage_count' => (int) $coupon->usage_count, 'individual_use' => 'yes' === $coupon->individual_use, 'product_ids' => array_map('absint', (array) $coupon->product_ids), 'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids), 'usage_limit' => !empty($coupon->usage_limit) ? $coupon->usage_limit : null, 'usage_limit_per_user' => !empty($coupon->usage_limit_per_user) ? $coupon->usage_limit_per_user : null, 'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items, 'free_shipping' => $coupon->enable_free_shipping(), 'product_categories' => array_map('absint', (array) $coupon->product_categories), 'excluded_product_categories' => array_map('absint', (array) $coupon->exclude_product_categories), 'exclude_sale_items' => $coupon->exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2), 'maximum_amount' => wc_format_decimal($coupon->maximum_amount, 2), 'email_restrictions' => $coupon->customer_email, 'used_by' => $coupon->get_used_by()); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links($this->prepare_links($post)); /** * Filter the data for a response. * * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being * prepared for the response. * * @param WP_REST_Response $response The response object. * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. */ return apply_filters("woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request); }
/** * Get the coupon for the given ID * * @since 2.1 * @param int $id the coupon ID * @param string $fields fields to include in response * @return array|WP_Error */ public function get_coupon($id, $fields = null) { global $wpdb; $id = $this->validate_request($id, 'shop_coupon', 'read'); if (is_wp_error($id)) { return $id; } // get the coupon code $code = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $id)); if (is_null($code)) { return new WP_Error('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), array('status' => 404)); } $coupon = new WC_Coupon($code); $coupon_post = get_post($coupon->id); $coupon_data = array('id' => $coupon->id, 'code' => $coupon->code, 'type' => $coupon->type, 'created_at' => $this->server->format_datetime($coupon_post->post_date_gmt), 'updated_at' => $this->server->format_datetime($coupon_post->post_modified_gmt), 'amount' => wc_format_decimal($coupon->amount, 2), 'individual_use' => 'yes' === $coupon->individual_use, 'product_ids' => array_map('absint', (array) $coupon->product_ids), 'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids), 'usage_limit' => !empty($coupon->usage_limit) ? $coupon->usage_limit : null, 'usage_limit_per_user' => !empty($coupon->usage_limit_per_user) ? $coupon->usage_limit_per_user : null, 'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items, 'usage_count' => (int) $coupon->usage_count, 'expiry_date' => $this->server->format_datetime($coupon->expiry_date), 'apply_before_tax' => $coupon->apply_before_tax(), 'enable_free_shipping' => $coupon->enable_free_shipping(), 'product_category_ids' => array_map('absint', (array) $coupon->product_categories), 'exclude_product_category_ids' => array_map('absint', (array) $coupon->exclude_product_categories), 'exclude_sale_items' => $coupon->exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2), 'customer_emails' => $coupon->customer_email); return array('coupon' => apply_filters('woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server)); }
function is_available($package) { global $woocommerce; if ($this->enabled == "no") { return false; } $ship_to_countries = ''; if ($this->availability == 'specific') { $ship_to_countries = $this->countries; } else { if (get_option('woocommerce_allowed_countries') == 'specific') { $ship_to_countries = get_option('woocommerce_specific_allowed_countries'); } } if (is_array($ship_to_countries)) { if (!in_array($package['destination']['country'], $ship_to_countries)) { return false; } } // Enabled logic $is_available = true; if ($this->requires_coupon == "yes") { if ($woocommerce->cart->applied_coupons) { foreach ($woocommerce->cart->applied_coupons as $code) { $coupon = new WC_Coupon($code); if ($coupon->enable_free_shipping()) { return true; } } } // No coupon found, as it stands, free shipping is disabled $is_available = false; } if (isset($woocommerce->cart->cart_contents_total) && !empty($this->min_amount)) { if ($woocommerce->cart->prices_include_tax) { $total = $woocommerce->cart->tax_total + $woocommerce->cart->cart_contents_total; } else { $total = $woocommerce->cart->cart_contents_total; } if ($this->min_amount > $total) { $is_available = false; } else { $is_available = true; } } return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available); }
/** * is_available function. * * @access public * @param mixed $package * @return bool */ function is_available($package) { global $woocommerce; if ($this->enabled == "no") { return false; } $ship_to_countries = ''; if ($this->availability == 'specific') { $ship_to_countries = $this->countries; } else { if (get_option('woocommerce_allowed_countries') == 'specific') { $ship_to_countries = get_option('woocommerce_specific_allowed_countries'); } } if (is_array($ship_to_countries)) { if (!in_array($package['destination']['country'], $ship_to_countries)) { return false; } } // Enabled logic $is_available = false; $has_coupon = false; $has_met_min_amount = false; if (in_array($this->requires, array('coupon', 'either', 'both'))) { if ($woocommerce->cart->applied_coupons) { foreach ($woocommerce->cart->applied_coupons as $code) { $coupon = new WC_Coupon($code); if ($coupon->is_valid() && $coupon->enable_free_shipping()) { $has_coupon = true; } } } } if (in_array($this->requires, array('min_amount', 'either', 'both')) && isset($woocommerce->cart->cart_contents_total)) { if ($woocommerce->cart->prices_include_tax) { $total = $woocommerce->cart->cart_contents_total + array_sum($woocommerce->cart->taxes); } else { $total = $woocommerce->cart->cart_contents_total; } if ($total >= $this->min_amount) { $has_met_min_amount = true; } } switch ($this->requires) { case 'min_amount': if ($has_met_min_amount) { $is_available = true; } break; case 'coupon': if ($has_coupon) { $is_available = true; } break; case 'both': if ($has_met_min_amount && $has_coupon) { $is_available = true; } break; case 'either': if ($has_met_min_amount || $has_coupon) { $is_available = true; } break; default: $is_available = true; break; } return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available); }
/** * Does the coupon have a value? (autocoupon should not be applied if it has no value) * @param WC_Coupon $coupon The coupon data * @return bool True if it has a value (discount, free shipping, whatever) otherwise false) **/ function coupon_has_a_value($coupon) { $has_a_value = false; if ($coupon->enable_free_shipping()) { $has_a_value = true; } else { //Test whether discount > 0 //See WooCommerce: class-wc-cart.php function get_discounted_price global $woocommerce; foreach ($woocommerce->cart->get_cart() as $cart_item) { if ($coupon->is_valid_for_cart() || $coupon->is_valid_for_product($cart_item['data'], $cart_item)) { if ($coupon->get_discount_amount($cart_item['data']->price, $cart_item) > 0) { $has_a_value = true; break; } } } } return apply_filters('wjecf_coupon_has_a_value', $has_a_value, $coupon); }
function is_available() { global $woocommerce; if ($this->enabled == "no") { return false; } $ship_to_countries = ''; if ($this->availability == 'specific') { $ship_to_countries = $this->countries; } else { if (get_option('woocommerce_allowed_countries') == 'specific') { $ship_to_countries = get_option('woocommerce_specific_allowed_countries'); } } if (is_array($ship_to_countries)) { if (!in_array($woocommerce->customer->get_shipping_country(), $ship_to_countries)) { return false; } } if ($this->requires_coupon == "yes") { if ($woocommerce->cart->applied_coupons) { foreach ($woocommerce->cart->applied_coupons as $code) { $coupon = new WC_Coupon($code); if ($coupon->enable_free_shipping()) { return true; } } } return false; } if (isset($woocommerce->cart->cart_contents_total)) { if ($woocommerce->cart->prices_include_tax) { $total = $woocommerce->cart->tax_total + $woocommerce->cart->cart_contents_total; } else { $total = $woocommerce->cart->cart_contents_total; } if (isset($this->min_amount) && $this->min_amount && $this->min_amount > $total) { return false; } } return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', true); }