/** * Returns the price including or excluding tax, based on the 'woocommerce_tax_display_shop' setting. * Should be safe to remove when we drop WC 2.2 compatibility * * @param WC_Product $product the product object * @param string $price to calculate, left blank to just use get_price() * @param integer $qty passed on to get_price_including_tax() or get_price_excluding_tax() * @return string */ public static function get_product_display_price($product, $price = '', $qty = 1) { if (SV_WC_Plugin_Compatibility::is_wc_version_gte_2_3()) { return $product->get_display_price($price, $qty); } else { if ($price === '') { $price = $product->get_price(); } $tax_display_mode = get_option('woocommerce_tax_display_shop'); $display_price = $tax_display_mode == 'incl' ? $product->get_price_including_tax($qty, $price) : $product->get_price_excluding_tax($qty, $price); return $display_price; } }
function woocommerce_gravityforms_get_updated_price() { global $woocommerce; header('Cache-Control: no-cache, must-revalidate'); header('Content-type: application/json'); $variation_id = isset($_POST['variation_id']) ? $_POST['variation_id'] : ''; $product_id = isset($_POST['product_id']) ? $_POST['product_id'] : 0; $gform_total = isset($_POST['gform_total']) ? $_POST['gform_total'] : 0; $product_data = null; if (function_exists('get_product')) { $product_data = get_product($variation_id > 0 ? $variation_id : $product_id); } else { if ($variation_id > 0) { $product_data = new WC_Product_Variation($variation_id); } else { $product_data = new WC_Product($product_id); } } $discount_price = false; $gforms_discount_price = false; $base_price = $product_data->get_display_price(); if (class_exists('WC_Dynamic_Pricing')) { $working_price = $base_price; $dynamic_pricing = WC_Dynamic_Pricing::instance(); foreach ($dynamic_pricing->modules as $module) { if ($module->module_type == 'simple') { //Make sure we are using the price that was just discounted. $working_price = $discount_price ? $discount_price : $base_price; $working_price = $module->get_product_working_price($working_price, $product_data); if (floatval($working_price)) { $discount_price = $module->get_discounted_price_for_shop($product_data, $working_price); } } } $gforms_base_price = $base_price + $gform_total; $gforms_working_price = $base_price + $gform_total; foreach ($dynamic_pricing->modules as $module) { if ($module->module_type == 'simple') { //Make sure we are using the price that was just discounted. $gforms_working_price = $gforms_discount_price ? $gforms_discount_price : $gforms_base_price; $gforms_working_price = $module->get_product_working_price($gforms_working_price, $product_data); if (floatval($gforms_working_price)) { $gforms_discount_price = $module->get_discounted_price_for_shop($product_data, $gforms_working_price); } } } } $price = $discount_price ? $discount_price : $base_price; $gform_final_total = $gforms_discount_price ? $gforms_discount_price : $price + $gform_total; $result = array('formattedBasePrice' => apply_filters('woocommerce_gform_base_price', woocommerce_price($price), $product_data), 'formattedTotalPrice' => apply_filters('woocommerce_gform_total_price', woocommerce_price($gform_final_total), $product_data), 'formattedVariationTotal' => apply_filters('woocommerce_gform_variation_total_price', woocommerce_price($gform_total), $product_data)); echo json_encode($result); die; }
/** * Convert a WC Product or Variation into a Square ItemVariation * See: https://docs.connect.squareup.com/api/connect/v1/#datatype-itemvariation * * @param WC_Product|WC_Product_Variation $variation * @param bool $include_inventory * @return array Formatted as a Square ItemVariation */ public static function format_wc_variation_for_square_api($variation, $include_inventory = false) { $formatted = array('name' => null, 'pricing_type' => null, 'price_money' => null, 'sku' => null, 'track_inventory' => null, 'inventory_alert_type' => null, 'inventory_alert_threshold' => null, 'user_data' => null); if ($variation instanceof WC_Product) { $formatted['name'] = __('Regular', 'woocommerce-square'); $formatted['price_money'] = array('currency_code' => apply_filters('woocommerce_square_currency', get_woocommerce_currency()), 'amount' => $variation->get_display_price() * 100); $formatted['sku'] = $variation->get_sku(); if ($include_inventory && $variation->managing_stock()) { $formatted['track_inventory'] = true; } } if ($variation instanceof WC_Product_Variation) { $formatted['name'] = implode(', ', $variation->get_variation_attributes()); } return array_filter($formatted); }
/** * Get min/max composite regular price. * * @param string $min_or_max * @return double */ public function get_composite_regular_price($min_or_max = 'min', $display = false) { if ($this->is_priced_per_product()) { if (!$this->is_synced()) { $this->sync_composite(); } $price = $min_or_max === 'min' ? $this->min_composite_regular_price : $this->max_composite_regular_price; if ($price && $display) { $display_price = WC_CP()->api->get_composited_product_price($this, $this->get_base_regular_price()); foreach ($this->price_index['regular_price'][$min_or_max] as $component_id => $product_id) { $component_data = $this->get_component_data($component_id); $item_qty = $component_data['optional'] === 'yes' && $min_or_max === 'min' ? 0 : $component_data['quantity_' . $min_or_max]; if ($item_qty) { $composited_product = $this->get_composited_product($component_id, $product_id); $display_price += $item_qty * $composited_product->get_regular_price($min_or_max, true); } } $price = $display_price; } } else { $price = parent::get_regular_price(); if ($display) { $price = WC_CP_Core_Compatibility::is_wc_version_gte_2_4() ? parent::get_display_price($price) : WC_CP()->api->get_composited_product_price($this, $price); } } return $price; }
/** * Get min/max bundle regular price. * * @param string $min_or_max * @return double */ public function get_bundle_regular_price($min_or_max = 'min', $display = false) { if ($this->is_priced_per_product()) { if (!$this->is_synced()) { $this->sync_bundle(); } $price = $min_or_max === 'min' ? $this->min_bundle_regular_price : $this->max_bundle_regular_price; if ($price && $display) { $display_price = WC_PB_Helpers::get_product_display_price($this, $this->get_base_regular_price()); foreach ($this->bundled_items as $bundled_item) { $bundled_item_qty = $this->bundled_quantities_index[$min_or_max][$bundled_item->item_id]; if ($bundled_item_qty) { $display_price += $bundled_item_qty * $bundled_item->get_bundled_item_regular_price($min_or_max, true); } } $price = $display_price; } } else { $price = parent::get_regular_price(); if ($display) { $price = WC_PB_Core_Compatibility::is_wc_version_gte_2_4() ? parent::get_display_price($price) : WC_PB_Helpers::get_product_display_price($this, $price); } } return $price; }