本文整理汇总了PHP中WC_Tax::get_shop_base_rate方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Tax::get_shop_base_rate方法的具体用法?PHP WC_Tax::get_shop_base_rate怎么用?PHP WC_Tax::get_shop_base_rate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Tax
的用法示例。
在下文中一共展示了WC_Tax::get_shop_base_rate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_product_subtotal
/**
* Get the product row subtotal.
*
* Gets the tax etc to avoid rounding issues.
*
* When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate
*
* @params object product
* @params int quantity
* @return string formatted price
*/
public function get_product_subtotal($_product, $quantity)
{
global $woocommerce;
$price = $_product->get_price();
$taxable = $_product->is_taxable();
$base_tax_rates = $this->tax->get_shop_base_rate($_product->tax_class);
$tax_rates = $this->tax->get_rates($_product->get_tax_class());
// This will get the base rate unless we're on the checkout page
// Taxable
if ($taxable) {
if ($this->tax_display_cart == 'excl') {
$row_price = $_product->get_price_excluding_tax($quantity);
$product_subtotal = woocommerce_price($row_price);
if ($this->prices_include_tax && $this->tax_total > 0) {
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->ex_tax_or_vat() . '</small>';
}
} else {
$row_price = $_product->get_price_including_tax($quantity);
$product_subtotal = woocommerce_price($row_price);
if (!$this->prices_include_tax && $this->tax_total > 0) {
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->inc_tax_or_vat() . '</small>';
}
}
// Non-taxable
} else {
$row_price = $price * $quantity;
$product_subtotal = woocommerce_price($row_price);
}
return apply_filters('woocommerce_cart_product_subtotal', $product_subtotal, $_product, $quantity, $this);
}
示例2: elseif
/**
* Get the product row subtotal.
*
* Gets the tax etc to avoid rounding issues.
*
* When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate
*
* @params object product
* @params int quantity
* @return string formatted price
*/
function get_product_subtotal($_product, $quantity)
{
global $woocommerce;
$price = $_product->get_price();
$taxable = $_product->is_taxable();
$base_tax_rates = $this->tax->get_shop_base_rate($_product->tax_class);
$tax_rates = $this->tax->get_rates($_product->get_tax_class());
// This will get the base rate unless we're on the checkout page
// Taxable
if ($taxable) {
if (($this->display_cart_ex_tax || $woocommerce->customer->is_vat_exempt()) && $this->prices_include_tax) {
$base_taxes = $this->tax->calc_tax($price * $quantity, $base_tax_rates, true);
$base_tax_amount = array_sum($base_taxes);
$row_price = $price * $quantity - $base_tax_amount;
$product_subtotal = woocommerce_price($row_price);
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->ex_tax_or_vat() . '</small>';
} elseif (!$this->display_cart_ex_tax && $tax_rates !== $base_tax_rates && $this->prices_include_tax) {
$base_taxes = $this->tax->calc_tax($price * $quantity, $base_tax_rates, true, true);
$modded_taxes = $this->tax->calc_tax($price * $quantity - array_sum($base_taxes), $tax_rates, false);
$row_price = $price * $quantity - array_sum($base_taxes) + array_sum($modded_taxes);
$product_subtotal = woocommerce_price($row_price);
if (!$this->prices_include_tax) {
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->inc_tax_or_vat() . '</small>';
}
} else {
$row_price = $price * $quantity;
$product_subtotal = woocommerce_price($row_price);
}
// Non taxable
} else {
$row_price = $price * $quantity;
$product_subtotal = woocommerce_price($row_price);
}
return apply_filters('woocommerce_cart_product_subtotal', $product_subtotal, $_product, $quantity, $this);
}
示例3: woocommerce_get_price
public static function woocommerce_get_price($price, $product)
{
global $post, $woocommerce;
$baseprice = $price;
$result = $baseprice;
if ($post == null || !is_admin()) {
if ($product->is_type('variation')) {
$commission = WRP_Variations_Admin::get_commission($product, $product->variation_id);
} else {
$commission = self::get_commission($product);
}
if ($commission) {
$baseprice = $product->get_regular_price();
if ($product->get_sale_price() != $product->get_regular_price() && $product->get_sale_price() == $product->price) {
if (get_option("wrp-baseprice", "regular") == "sale") {
$baseprice = $product->get_sale_price();
}
}
$product_price = $baseprice;
$type = get_option("wrp-method", "rate");
$result = 0;
if ($type == "rate") {
// if rate and price includes taxes
if ($product->is_taxable() && get_option('woocommerce_prices_include_tax') == 'yes') {
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate($product->tax_class);
$taxes = $_tax->calc_tax($baseprice, $tax_rates, true);
$product_price = $_tax->round($baseprice - array_sum($taxes));
}
$result = self::bcmul($product_price, $commission, WOO_ROLE_PRICING_DECIMALS);
if ($product->is_taxable() && get_option('woocommerce_prices_include_tax') == 'yes') {
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate($product->tax_class);
$taxes = $_tax->calc_tax($result, $tax_rates, false);
// important false
$result = $_tax->round($result + array_sum($taxes));
}
} else {
$result = self::bcsub($product_price, $commission, WOO_ROLE_PRICING_DECIMALS);
}
}
}
return $result;
}
示例4: woocommerce_get_price
public static function woocommerce_get_price($price, $product)
{
global $post, $woocommerce;
$baseprice = $price;
$result = $baseprice;
if ($post == null || !is_admin()) {
$roleprice = self::get_role_price($product);
if (empty($roleprice)) {
$regularprice = $product->get_regular_price();
if (!empty($regularprice)) {
$baseprice = $regularprice;
}
if ($product->get_sale_price() != $product->get_regular_price() && $product->get_sale_price() == $product->price) {
if (get_option("wrp-baseprice", "regular") == "sale") {
$baseprice = $product->get_sale_price();
}
}
$product_price = $baseprice;
} else {
$baseprice = $roleprice;
$product_price = $roleprice;
}
$result = 0;
if ($product->is_taxable() && get_option('woocommerce_prices_include_tax') == 'yes') {
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate($product->tax_class);
$taxes = $_tax->calc_tax($baseprice, $tax_rates, true);
$product_price = $_tax->round($baseprice - array_sum($taxes));
}
$result = $product_price;
if ($product->is_taxable() && get_option('woocommerce_prices_include_tax') == 'yes') {
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate($product->tax_class);
$taxes = $_tax->calc_tax($result, $tax_rates, false);
// important false
$result = $_tax->round($result + array_sum($taxes));
}
}
return $result;
}
示例5: __construct
public function __construct()
{
$this->order = new WC_Order();
$this->type = 'preview';
$this->number = get_option('wc_gzdp_invoice_simple') + 1;
$this->recipient = array('firstname' => _x('Max', 'invoices-preview', 'woocommerce-germanized-pro'), 'lastname' => _x('Mustermann', 'invoices-preview', 'woocommerce-germanized-pro'));
$this->address = implode('<br/>', array(_x('Max Mustermann', 'invoices-preview', 'woocommerce-germanized-pro'), _x('Musterstraße 35', 'invoices-preview', 'woocommerce-germanized-pro'), _x('12209 Musterstadt', 'invoices-preview', 'woocommerce-germanized-pro')));
$base_rate = WC_Tax::get_shop_base_rate();
$base_rate_id = key($base_rate);
$calc_tax = true;
if (get_option('woocommerce_gzd_small_enterprise') == 'yes') {
$calc_tax = false;
}
$this->items = array();
$total = 0;
$total_tax = 0;
$shipping = 0;
for ($i = 0; $i < 30; $i++) {
$qty = 1;
$subtotal_gross = 35.95;
$subtotal_tax = $calc_tax ? array_sum(WC_Tax::calc_tax($subtotal_gross, $base_rate, true)) : 0;
$subtotal_net = $subtotal_gross - $subtotal_tax;
$subtotal = $subtotal_net;
$item_total = $qty * $subtotal;
$item_tax = $qty * $subtotal_tax;
$item = array('name' => _x('Testprodukt', 'invoices-preview', 'woocommerce-germanized-pro'), 'item_meta' => array(), 'type' => 'line_item', 'qty' => $qty, 'line_total' => $item_total, 'line_subtotal' => $subtotal, 'line_tax' => $item_tax, 'line_subtotal_tax' => $subtotal_tax);
$total += $subtotal_gross;
$total_tax += $item_tax;
array_push($this->items, $item);
}
$this->totals = array('subtotal' => $total - $shipping - $total_tax, 'total' => $total, 'shipping' => $shipping, 'tax' => $total_tax, 'discount' => 0);
if ($calc_tax) {
$tax_totals = array('amount' => $total_tax, 'rate_id' => $base_rate_id, 'formatted_amount' => wc_price($total_tax));
$this->tax_totals = array();
$this->tax_totals['DE'] = (object) $tax_totals;
}
}
示例6: round
/**
* Returns the price (excluding tax) - ignores tax_class filters since the price may *include* tax and thus needs subtracting.
*
* @access public
* @return string
*/
function get_price_excluding_tax()
{
$price = $this->get_price();
if ($this->is_taxable() && get_option('woocommerce_prices_include_tax') == 'yes') {
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate($this->tax_class);
$taxes = $_tax->calc_tax($price, $tax_rates, true);
$tax_amount = $_tax->get_tax_total($taxes);
$price = round($price - $tax_amount, 2);
}
return apply_filters('woocommerce_get_price_excluding_tax', $price, $this);
}
示例7: vtprd_get_price_excluding_tax_forced
function vtprd_get_price_excluding_tax_forced($product_id, $price, $product)
{
global $post, $wpdb, $woocommerce, $vtprd_cart, $vtprd_cart_item, $vtprd_setup_options, $vtprd_info;
//changed $this-> to $product->
//use $discount_price as basis
$qty = 1;
if ($product->is_taxable()) {
//if ( $product->is_taxable() && get_option('woocommerce_prices_include_tax') === 'yes' ) {
$_tax = new WC_Tax();
$tax_rates = $_tax->get_shop_base_rate($product->tax_class);
$taxes = $_tax->calc_tax($price * $qty, $tax_rates, true);
$price = $_tax->round($price * $qty - array_sum($taxes));
}
return $price;
}
示例8: output
/**
* Output the form
*/
public function output()
{
$this->errors = array();
$step = 1;
try {
if (!empty($_POST) && !check_admin_referer('create_booking_notification')) {
throw new Exception(__('Error - please try again', 'woocommerce-bookings'));
}
if (!empty($_POST['create_booking'])) {
$customer_id = absint($_POST['customer_id']);
$bookable_product_id = absint($_POST['bookable_product_id']);
$booking_order = wc_clean($_POST['booking_order']);
if (!$bookable_product_id) {
throw new Exception(__('Please choose a bookable product', 'woocommerce-bookings'));
}
if ($booking_order === 'existing') {
$order_id = absint($_POST['booking_order_id']);
$booking_order = $order_id;
if (!$booking_order || get_post_type($booking_order) !== 'shop_order') {
throw new Exception(__('Invalid order ID provided', 'woocommerce-bookings'));
}
}
$step++;
$product = get_product($bookable_product_id);
$booking_form = new WC_Booking_Form($product);
} elseif (!empty($_POST['create_booking_2'])) {
$customer_id = absint($_POST['customer_id']);
$bookable_product_id = absint($_POST['bookable_product_id']);
$booking_order = wc_clean($_POST['booking_order']);
$product = get_product($bookable_product_id);
$booking_form = new WC_Booking_Form($product);
$booking_data = $booking_form->get_posted_data($_POST);
$booking_cost = ($cost = $booking_form->calculate_booking_cost($_POST)) && !is_wp_error($cost) ? number_format($cost, 2, '.', '') : 0;
$create_order = false;
if ('yes' === get_option('woocommerce_prices_include_tax')) {
if (version_compare(WOOCOMMERCE_VERSION, '2.3', '<')) {
$base_tax_rates = WC_Tax::get_shop_base_rate($product->tax_class);
} else {
$base_tax_rates = WC_Tax::get_base_tax_rates($product->tax_class);
}
$base_taxes = WC_Tax::calc_tax($booking_cost, $base_tax_rates, true);
$booking_cost = round($booking_cost - array_sum($base_taxes), absint(get_option('woocommerce_price_num_decimals')));
}
// Data to go into the booking
$new_booking_data = array('user_id' => $customer_id, 'product_id' => $product->id, 'resource_id' => isset($booking_data['_resource_id']) ? $booking_data['_resource_id'] : '', 'persons' => $booking_data['_persons'], 'cost' => $booking_cost, 'start_date' => $booking_data['_start_date'], 'end_date' => $booking_data['_end_date'], 'all_day' => $booking_data['_all_day'] ? 1 : 0);
// Create order
if ($booking_order === 'new') {
$create_order = true;
$order_id = $this->create_order($booking_cost, $customer_id);
if (!$order_id) {
throw new Exception(__('Error: Could not create order', 'woocommerce-bookings'));
}
} elseif ($booking_order > 0) {
$order_id = absint($booking_order);
if (!$order_id || get_post_type($order_id) !== 'shop_order') {
throw new Exception(__('Invalid order ID provided', 'woocommerce-bookings'));
}
$order = new WC_Order($order_id);
update_post_meta($order_id, '_order_total', $order->get_total() + $booking_cost);
update_post_meta($order_id, '_booking_order', '1');
} else {
$order_id = 0;
}
if ($order_id) {
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $product->get_title(), 'order_item_type' => 'line_item'));
if (!$item_id) {
throw new Exception(__('Error: Could not create item', 'woocommerce-bookings'));
}
// Add line item meta
woocommerce_add_order_item_meta($item_id, '_qty', 1);
woocommerce_add_order_item_meta($item_id, '_tax_class', $product->get_tax_class());
woocommerce_add_order_item_meta($item_id, '_product_id', $product->id);
woocommerce_add_order_item_meta($item_id, '_variation_id', '');
woocommerce_add_order_item_meta($item_id, '_line_subtotal', $booking_cost);
woocommerce_add_order_item_meta($item_id, '_line_total', $booking_cost);
woocommerce_add_order_item_meta($item_id, '_line_tax', 0);
woocommerce_add_order_item_meta($item_id, '_line_subtotal_tax', 0);
// We have an item id
$new_booking_data['order_item_id'] = $item_id;
// Add line item data
foreach ($booking_data as $key => $value) {
if (strpos($key, '_') !== 0) {
woocommerce_add_order_item_meta($item_id, get_wc_booking_data_label($key, $product), $value);
}
}
}
// Create the booking itself
$new_booking = get_wc_booking($new_booking_data);
$new_booking->create($create_order ? 'unpaid' : 'pending-confirmation');
wp_safe_redirect(admin_url('post.php?post=' . ($create_order ? $order_id : $new_booking->id) . '&action=edit'));
exit;
}
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
}
switch ($step) {
case 1:
//.........这里部分代码省略.........
示例9: sync
//.........这里部分代码省略.........
$product->tax_class = $wc_product->get_tax_class();
$product->stock_control = $wc_product->managing_stock();
$product->stock_level = $wc_product->get_stock_quantity();
if (method_exists($wc_product, 'get_type')) {
$product->type = $wc_product->get_type();
} else {
$product->type = $wc_product->product_type;
}
$product->description = apply_filters('the_content', $wc_product->post->post_content);
$product->short_description = apply_filters('the_content', $wc_product->post->post_excerpt);
if (method_exists($wc_product, 'get_width')) {
$product->width = $wc_product->get_width();
if (!is_numeric($product->width)) {
unset($product->width);
}
$product->height = $wc_product->get_height();
if (!is_numeric($product->height)) {
unset($product->height);
}
$product->length = $wc_product->get_length();
if (!is_numeric($product->length)) {
unset($product->length);
}
} else {
$product->length = $wc_product->length;
$product->width = $wc_product->width;
$product->height = $wc_product->height;
}
$product->weight = $wc_product->get_weight();
if (!is_numeric($product->weight)) {
unset($product->weight);
}
if ($product->is_taxable && 'yes' === get_option('woocommerce_prices_include_tax')) {
$tax_rates = WC_Tax::get_shop_base_rate($product->tax_class);
$taxes = WC_Tax::calc_tax($product->listprice, $tax_rates, true);
$product->listprice = $product->listprice - array_sum($taxes);
}
if ($product->type == 'variable') {
$product->skus = array();
foreach ($wc_product->get_children() as $child_id) {
$child_product = $wc_product->get_child($child_id);
$img = wp_get_attachment_image_src($child_product->get_image_id(), 'full');
$img = $img[0];
$child_product_data = array('id' => $child_id, 'sku' => $child_product->get_sku(), 'enabled' => $wc_product->is_purchasable() && ($wc_product->managing_stock() || $wc_product->is_in_stock()), 'price' => $child_product->get_price_excluding_tax(), 'listprice' => $child_product->get_regular_price(), 'is_taxable' => $child_product->is_taxable(), 'tax_class' => $child_product->get_tax_class(), 'stock_control' => $child_product->managing_stock(), 'stock_level' => $child_product->get_stock_quantity(), 'images' => array(array('source' => $img, 'sequence' => 0)));
$attributes = array();
$termsmap = array();
$names = array();
foreach ($child_product->get_variation_attributes() as $name => $value) {
$name = preg_replace('/(pa_)?attribute_/', '', $name);
if (!isset($names[$name])) {
$names[$name] = true;
$terms = get_terms(array('taxonomy' => $name));
if ($terms) {
foreach ($terms as $term) {
$termsmap[$term->slug] = $term->name;
}
}
}
if ($value && (gettype($value) == 'string' || gettype($value) == 'integer')) {
if (array_key_exists($value, $termsmap)) {
$newvalue = $termsmap[$value];
} else {
$newvalue = $value;
}
} else {
$newvalue = '';
示例10: build_transaction_lines
/**
* Build the transaction lines
*
* @param array $items
* @param String $country
*
* @return array
*/
private function build_transaction_lines($items, $country)
{
// Transaction lines
$transaction_lines = array();
// Lo0p
if (count($items) > 0) {
// Country manager
$country_manager = new WC_TA_Country_Manager();
foreach ($items as $item_key => $item) {
if (isset($item['data'])) {
// The transaction line
$transaction_line = array();
$product = $item['data'];
// Set the product type
$type = 'default';
// Check if this is a virtual product
if ($product->is_virtual()) {
$type = 'e-service';
} else {
$transaction_line['informative'] = true;
}
// Check if this is an e-book
$is_ebook = get_post_meta($item['id'], '_ebook', true);
if ('yes' === $is_ebook) {
$type = 'e-book';
}
// Check if Taxamo supports taxes for this country
if (false === $country_manager->is_tax_supported_for_country($country)) {
// Taxes aren't supported, set the line to informative
$transaction_line['informative'] = true;
}
// Set the product type
$transaction_line['product_type'] = $type;
// Custom ID
$transaction_line['custom_id'] = "" . $item_key;
// Quantity
$transaction_line['quantity'] = $item['quantity'];
// Price
if ($product->is_taxable()) {
// Always without tax
$transaction_line['amount'] = $item['line_total'];
// Get the base tax rates
if (method_exists('WC_Tax', 'get_base_tax_rates')) {
$base_tax_rates = WC_Tax::get_base_tax_rates($product->get_tax_class());
} else {
$base_tax_rates = WC_Tax::get_shop_base_rate($product->get_tax_class());
}
$base_tax_rate = array_shift($base_tax_rates);
// Set the tax rate
$transaction_line['tax_rate'] = $base_tax_rate['rate'];
} else {
// Set Price
$transaction_line['amount'] = $product->get_price();
// Set 0 tax rate
$transaction_line['tax_rate'] = 0;
// Force non taxable
$transaction_line['informative'] = true;
}
// What do we want? Floats! When do we want them? Now!
// Check if amount is set
if (isset($transaction_line['amount'])) {
$transaction_line['amount'] = floatval($transaction_line['amount']);
}
// Check if total_amount is set
if (isset($transaction_line['total_amount'])) {
$transaction_line['total_amount'] = floatval($transaction_line['total_amount']);
}
// Float the tax rate
$transaction_line['tax_rate'] = floatval($transaction_line['tax_rate']);
// Add transaction line to transaction lines
$transaction_lines[] = $transaction_line;
}
}
}
return $transaction_lines;
}
示例11: wc_gzd_cart_totals_order_total_tax_html
/**
* Get order total tax html.
*
* @return void
*/
function wc_gzd_cart_totals_order_total_tax_html()
{
$_tax = new WC_Tax();
// If prices are tax inclusive, show taxes here
if (get_option('woocommerce_calc_taxes') == 'yes' && WC()->cart->tax_display_cart == 'incl') {
$tax_array = array();
if (get_option('woocommerce_tax_total_display') == 'itemized') {
foreach (WC()->cart->get_tax_totals() as $code => $tax) {
$rate = wc_gzd_get_tax_rate($tax->tax_rate_id);
if (!$rate) {
continue;
}
if (!empty($rate) && isset($rate->tax_rate)) {
$tax->rate = $rate->tax_rate;
}
if (!isset($tax_array[$tax->rate])) {
$tax_array[$tax->rate] = array('tax' => $tax, 'amount' => $tax->amount, 'contains' => array($tax));
} else {
array_push($tax_array[$tax->rate]['contains'], $tax);
$tax_array[$tax->rate]['amount'] += $tax->amount;
}
}
} else {
$base_rate = array_values(WC_Tax::get_shop_base_rate());
$base_rate = (object) $base_rate[0];
$base_rate->rate = $base_rate->rate;
$tax_array[] = array('tax' => $base_rate, 'contains' => array($base_rate), 'amount' => WC()->cart->get_taxes_total(true, true));
}
if (!empty($tax_array)) {
foreach ($tax_array as $tax) {
echo '
<tr class="order-tax">
<th>' . (get_option('woocommerce_tax_total_display') == 'itemized' ? sprintf(__('incl. %s%% VAT', 'woocommerce-germanized'), wc_gzd_format_tax_rate_percentage($tax['tax']->rate)) : __('incl. VAT', 'woocommerce-germanized')) . '</th>
<td>' . wc_price($tax['amount']) . '</td>
</tr>';
}
}
}
}
示例12: create_tax_rates
public static function create_tax_rates()
{
global $wpdb;
// Delete digital rates
$wpdb->delete($wpdb->prefix . 'woocommerce_tax_rates', array('tax_rate_class' => 'virtual-rate'), array('%s'));
$rates = array('BE' => 21, 'BG' => 20, 'CZ' => 21, 'DK' => 25, 'DE' => 19, 'EE' => 20, 'EL' => 23, 'ES' => 21, 'FR' => 20, 'HR' => 25, 'IE' => 23, 'IT' => 22, 'CY' => 19, 'LV' => 21, 'LT' => 21, 'LU' => 15, 'HU' => 27, 'MT' => 18, 'NL' => 21, 'AT' => 20, 'PL' => 23, 'PT' => 23, 'RO' => 24, 'SI' => 22, 'SK' => 20, 'FI' => 24, 'SE' => 25, 'UK' => 20);
if (!empty($rates)) {
$count = 0;
foreach ($rates as $iso => $rate) {
$_tax_rate = array('tax_rate_country' => $iso, 'tax_rate_state' => '', 'tax_rate' => (string) number_format((double) wc_clean($rate), 4, '.', ''), 'tax_rate_name' => 'MwSt. ' . $iso . ' virtual', 'tax_rate_priority' => 1, 'tax_rate_compound' => 0, 'tax_rate_shipping' => 0, 'tax_rate_order' => $count++, 'tax_rate_class' => 'virtual-rate');
// Check if standard rate exists
if (WC()->countries->get_base_country() == $iso) {
$base_rate = WC_Tax::get_shop_base_rate();
$base_rate = reset($base_rate);
if (!empty($base_rate)) {
$_tax_rate['tax_rate_name'] = $base_rate['label'];
}
}
$wpdb->insert($wpdb->prefix . 'woocommerce_tax_rates', $_tax_rate);
$tax_rate_id = $wpdb->insert_id;
do_action('woocommerce_tax_rate_added', $tax_rate_id, $_tax_rate);
}
}
// Clear tax transients
$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE %s;", '_transient_wc_tax_rates%'));
$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE %s;", '_transient_timeout_wc_tax_rates%'));
}
示例13: order_item_totals
/**
* Improve tax display within order totals
*
* @param array $order_totals
* @param object $order
* @return array
*/
public function order_item_totals($order_totals, $order)
{
// Set to formatted total without displaying tax info behind the price
$order_totals['order_total']['value'] = $order->get_formatted_order_total();
// Tax for inclusive prices
if ('yes' == get_option('woocommerce_calc_taxes') && 'incl' == $order->tax_display_cart) {
$tax_array = array();
if ('itemized' == get_option('woocommerce_tax_total_display')) {
foreach ($order->get_tax_totals() as $code => $tax) {
$tax->rate = WC_Tax::get_rate_percent($tax->rate_id);
if (!isset($tax_array[$tax->rate])) {
$tax_array[$tax->rate] = array('tax' => $tax, 'amount' => $tax->amount, 'contains' => array($tax));
} else {
array_push($tax_array[$tax->rate]['contains'], $tax);
$tax_array[$tax->rate]['amount'] += $tax->amount;
}
}
} else {
$base_rate = array_values(WC_Tax::get_shop_base_rate());
$base_rate = (object) $base_rate[0];
$base_rate->rate = $base_rate->rate;
$tax_array[] = array('tax' => $base_rate, 'contains' => array($base_rate), 'amount' => $order->get_total_tax());
}
if (!empty($tax_array)) {
foreach ($tax_array as $tax) {
$order_totals['tax_' . $tax['tax']->label] = array('label' => '<span class="tax small tax-label">' . (get_option('woocommerce_tax_total_display') == 'itemized' ? sprintf(__('incl. %s%% VAT', 'woocommerce-germanized'), wc_gzd_format_tax_rate_percentage($tax['tax']->rate)) : __('incl. VAT', 'woocommerce-germanized')) . '</span>', 'value' => '<span class="tax small tax-value">' . wc_price($tax['amount']) . '</span>');
}
}
}
return $order_totals;
}
示例14: calculate_tax_for_subscription
/**
* Calculates a price (could be per period price or sign-up fee) for a subscription less tax
* if the subscription is taxable and the prices in the store include tax.
*
* Based on the WC_Product::get_price_excluding_tax() function.
*
* @param float $price The price to adjust based on taxes
* @param WC_Product $product The product the price belongs too (needed to determine tax class)
* @since 1.0
*/
public static function calculate_tax_for_subscription($price, $product, $deduct_base_taxes = false)
{
_deprecated_function(__METHOD__, '1.5.8', 'WC_Product::get_price_including_tax()');
if ($product->is_taxable()) {
$tax = new WC_Tax();
$base_tax_rates = $tax->get_shop_base_rate($product->tax_class);
$tax_rates = $tax->get_rates($product->get_tax_class());
// This will get the base rate unless we're on the checkout page
if ($deduct_base_taxes && get_option('woocommerce_prices_include_tax') == 'yes') {
$base_taxes = $tax->calc_tax($price, $base_tax_rates, true);
$taxes = $tax->calc_tax($price - array_sum($base_taxes), $tax_rates, false);
} elseif (get_option('woocommerce_prices_include_tax') == 'yes') {
$taxes = $tax->calc_tax($price, $base_tax_rates, true);
} else {
$taxes = $tax->calc_tax($price, $base_tax_rates, false);
}
$tax_amount = $tax->get_tax_total($taxes);
} else {
$tax_amount = 0;
}
return $tax_amount;
}
示例15: calculate_totals
/**
* Calculate totals for the items in the cart.
*
* @access public
*/
public function calculate_totals()
{
$this->reset();
do_action('woocommerce_before_calculate_totals', $this);
if (sizeof($this->get_cart()) == 0) {
$this->set_session();
return;
}
$tax_rates = array();
$shop_tax_rates = array();
/**
* Calculate subtotals for items. This is done first so that discount logic can use the values.
*/
foreach ($this->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
// Count items + weight
$this->cart_contents_weight += $_product->get_weight() * $values['quantity'];
$this->cart_contents_count += $values['quantity'];
// Prices
$base_price = $_product->get_price();
$line_price = $_product->get_price() * $values['quantity'];
$line_subtotal = 0;
$line_subtotal_tax = 0;
/**
* No tax to calculate
*/
if (!$_product->is_taxable()) {
// Subtotal is the undiscounted price
$this->subtotal += $line_price;
$this->subtotal_ex_tax += $line_price;
/**
* Prices include tax
*
* To prevent rounding issues we need to work with the inclusive price where possible
* otherwise we'll see errors such as when working with a 9.99 inc price, 20% VAT which would
* be 8.325 leading to totals being 1p off
*
* Pre tax coupons come off the price the customer thinks they are paying - tax is calculated
* afterwards.
*
* e.g. $100 bike with $10 coupon = customer pays $90 and tax worked backwards from that
*/
} elseif ($this->prices_include_tax) {
// Get base tax rates
if (empty($shop_tax_rates[$_product->tax_class])) {
$shop_tax_rates[$_product->tax_class] = $this->tax->get_shop_base_rate($_product->tax_class);
}
// Get item tax rates
if (empty($tax_rates[$_product->get_tax_class()])) {
$tax_rates[$_product->get_tax_class()] = $this->tax->get_rates($_product->get_tax_class());
}
$base_tax_rates = $shop_tax_rates[$_product->tax_class];
$item_tax_rates = $tax_rates[$_product->get_tax_class()];
/**
* ADJUST TAX - Calculations when base tax is not equal to the item tax
*/
if ($item_tax_rates !== $base_tax_rates) {
// Work out a new base price without the shop's base tax
$taxes = $this->tax->calc_tax($line_price, $base_tax_rates, true, true);
// Now we have a new item price (excluding TAX)
$line_subtotal = $line_price - array_sum($taxes);
// Now add modifed taxes
$tax_result = $this->tax->calc_tax($line_subtotal, $item_tax_rates);
$line_subtotal_tax = array_sum($tax_result);
/**
* Regular tax calculation (customer inside base and the tax class is unmodified
*/
} else {
// Calc tax normally
$taxes = $this->tax->calc_tax($line_price, $item_tax_rates, true);
$line_subtotal_tax = array_sum($taxes);
$line_subtotal = $line_price - array_sum($taxes);
}
/**
* Prices exclude tax
*
* This calculation is simpler - work with the base, untaxed price.
*/
} else {
// Get item tax rates
if (empty($tax_rates[$_product->get_tax_class()])) {
$tax_rates[$_product->get_tax_class()] = $this->tax->get_rates($_product->get_tax_class());
}
$item_tax_rates = $tax_rates[$_product->get_tax_class()];
// Base tax for line before discount - we will store this in the order data
$taxes = $this->tax->calc_tax($line_price, $item_tax_rates);
$line_subtotal_tax = array_sum($taxes);
$line_subtotal = $line_price;
}
// Add to main subtotal
$this->subtotal += $line_subtotal + $line_subtotal_tax;
$this->subtotal_ex_tax += $line_subtotal;
}
/**
* Calculate totals for items
//.........这里部分代码省略.........