本文整理汇总了PHP中WC_Tax类的典型用法代码示例。如果您正苦于以下问题:PHP WC_Tax类的具体用法?PHP WC_Tax怎么用?PHP WC_Tax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WC_Tax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_order
/**
* Create a order.
*
* @since 2.4
*
* @return WC_Order Order object.
*/
public static function create_order()
{
// Create product
$product = WC_Helper_Product::create_simple_product();
WC_Helper_Shipping::create_simple_flat_rate();
$order_data = array('status' => 'pending', 'customer_id' => 1, 'customer_note' => '', 'total' => '');
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Required, else wc_create_order throws an exception
$order = wc_create_order($order_data);
// Add order products
$item_id = $order->add_product($product, 4);
// Set billing address
$billing_address = array('country' => 'US', 'first_name' => 'Jeroen', 'last_name' => 'Sormani', 'company' => 'WooCompany', 'address_1' => 'WooAddress', 'address_2' => '', 'postcode' => '123456', 'city' => 'WooCity', 'state' => 'NY', 'email' => 'admin@example.org', 'phone' => '555-32123');
$order->set_address($billing_address, 'billing');
// Add shipping costs
$shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates());
$order->add_shipping(new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate'));
// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method($payment_gateways['bacs']);
// Set totals
$order->set_total(10, 'shipping');
$order->set_total(0, 'cart_discount');
$order->set_total(0, 'cart_discount_tax');
$order->set_total(0, 'tax');
$order->set_total(0, 'shipping_tax');
$order->set_total(40, 'total');
// 4 x $10 simple helper product
return wc_get_order($order->id);
}
示例2: create_order
/**
* Create a order.
*
* @since 2.4
*
* @return WC_Order Order object.
*/
public static function create_order($customer_id = 1)
{
// Create product
$product = WC_Helper_Product::create_simple_product();
WC_Helper_Shipping::create_simple_flat_rate();
$order_data = array('status' => 'pending', 'customer_id' => $customer_id, 'customer_note' => '', 'total' => '');
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Required, else wc_create_order throws an exception
$order = wc_create_order($order_data);
// Add order products
$order->add_product($product, 4);
// Set billing address
$order->set_billing_first_name('Jeroen');
$order->set_billing_last_name('Sormani');
$order->set_billing_company('WooCompany');
$order->set_billing_address_1('WooAddress');
$order->set_billing_address_2('');
$order->set_billing_city('WooCity');
$order->set_billing_state('NY');
$order->set_billing_postcode('123456');
$order->set_billing_country('US');
$order->set_billing_email('admin@example.org');
$order->set_billing_phone('555-32123');
// Add shipping costs
$shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates());
$rate = new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate');
$item = new WC_Order_Item_Shipping();
$item->set_props(array('method_title' => $rate->label, 'method_id' => $rate->id, 'total' => wc_format_decimal($rate->cost), 'taxes' => $rate->taxes, 'meta_data' => $rate->get_meta_data()));
$order->add_item($item);
// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method($payment_gateways['bacs']);
// Set totals
$order->set_shipping_total(10);
$order->set_discount_total(0);
$order->set_discount_tax(0);
$order->set_cart_tax(0);
$order->set_shipping_tax(0);
$order->set_total(40);
// 4 x $10 simple helper product
$order->save();
return $order;
}
示例3: calculate_shipping
/**
* Calculate shipping when this method is used standalone.
*/
public function calculate_shipping($package)
{
$_tax = new WC_Tax();
$taxes = array();
$shipping_cost = 0;
// This shipping method loops through products, adding up the cost
if (sizeof($package['contents']) > 0) {
foreach ($package['contents'] as $item_id => $values) {
if ($values['quantity'] > 0) {
if ($values['data']->needs_shipping()) {
$rule = false;
$item_shipping_cost = 0;
if ($values['variation_id']) {
$rule = woocommerce_per_product_shipping_get_matching_rule($values['variation_id'], $package);
}
if ($rule === false) {
$rule = woocommerce_per_product_shipping_get_matching_rule($values['product_id'], $package);
}
if ($rule) {
$item_shipping_cost += $rule->rule_item_cost * $values['quantity'];
$item_shipping_cost += $rule->rule_cost;
} elseif ($this->cost === '0' || $this->cost > 0) {
// Use default
$item_shipping_cost += $this->cost * $values['quantity'];
} else {
// NO default and nothing found - abort
return;
}
// Fee
$item_shipping_cost += $this->get_fee($this->fee, $item_shipping_cost) * $values['quantity'];
$shipping_cost += $item_shipping_cost;
if (get_option('woocommerce_calc_taxes') == 'yes' && $this->tax_status == 'taxable') {
$rates = $_tax->get_shipping_tax_rates($values['data']->get_tax_class());
$item_taxes = $_tax->calc_shipping_tax($item_shipping_cost, $rates);
// Sum the item taxes
foreach (array_keys($taxes + $item_taxes) as $key) {
$taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0);
}
}
}
}
}
}
// Add order shipping cost + tax
if ($this->order_fee) {
$order_fee = $this->get_fee($this->order_fee, $shipping_cost);
$shipping_cost += $order_fee;
if (get_option('woocommerce_calc_taxes') == 'yes' && $this->tax_status == 'taxable') {
$rates = $_tax->get_shipping_tax_rates();
$item_taxes = $_tax->calc_shipping_tax($order_fee, $rates);
// Sum the item taxes
foreach (array_keys($taxes + $item_taxes) as $key) {
$taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0);
}
}
}
// Add rate
$this->add_rate(array('id' => $this->id, 'label' => $this->title, 'cost' => $shipping_cost, 'taxes' => $taxes));
}
示例4: add_rate
/**
* Add a rate
*
* Add a shipping rate. If taxes are not set they will be calculated based on cost.
*
* @access public
* @param array $args (default: array())
* @return void
*/
function add_rate($args = array())
{
global $woocommerce;
$defaults = array('id' => '', 'label' => '', 'cost' => '0', 'taxes' => '', 'calc_tax' => 'per_order');
$args = wp_parse_args($args, $defaults);
extract($args);
// Id and label are required
if (!$id || !$label) {
return;
}
// Handle cost
$total_cost = is_array($cost) ? array_sum($cost) : $cost;
// Taxes - if not an array and not set to false, calc tax based on cost and passed calc_tax variable
// This saves shipping methods having to do complex tax calculations
if (!is_array($taxes) && $taxes !== false && $total_cost > 0 && get_option('woocommerce_calc_taxes') == 'yes' && $this->tax_status == 'taxable') {
$_tax = new WC_Tax();
$taxes = array();
switch ($calc_tax) {
case "per_item":
// If we have an array of costs we can look up each items tax class and add tax accordingly
if (is_array($cost)) {
$cart = $woocommerce->cart->get_cart();
foreach ($cost as $cost_key => $amount) {
if (!isset($cart[$cost_key])) {
continue;
}
$_product = $cart[$cost_key]['data'];
$rates = $_tax->get_shipping_tax_rates($_product->get_tax_class());
$item_taxes = $_tax->calc_shipping_tax($amount, $rates);
// Sum the item taxes
foreach (array_keys($taxes + $item_taxes) as $key) {
$taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0);
}
}
// Add any cost for the order - order costs are in the key 'order'
if (isset($cost['order'])) {
$rates = $_tax->get_shipping_tax_rates();
$item_taxes = $_tax->calc_shipping_tax($cost['order'], $rates);
// Sum the item taxes
foreach (array_keys($taxes + $item_taxes) as $key) {
$taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0);
}
}
}
break;
default:
$rates = $_tax->get_shipping_tax_rates();
$taxes = $_tax->calc_shipping_tax($total_cost, $rates);
break;
}
}
$this->rates[] = new WC_Shipping_Rate($id, $label, $total_cost, $taxes, $this->id);
}
示例5: get_formatted_order_total
/**
* Gets order total - formatted for display.
*
* @return string
*/
public function get_formatted_order_total($tax_display = '', $display_refunded = true)
{
$formatted_total = wc_price($this->get_total(), array('currency' => $this->get_order_currency()));
$order_total = $this->get_total();
$total_refunded = $this->get_total_refunded();
$tax_string = '';
// Tax for inclusive prices
if (wc_tax_enabled() && 'incl' == $tax_display) {
$tax_string_array = array();
if ('itemized' == get_option('woocommerce_tax_total_display')) {
foreach ($this->get_tax_totals() as $code => $tax) {
$tax_amount = $total_refunded && $display_refunded ? wc_price(WC_Tax::round($tax->amount - $this->get_total_tax_refunded_by_rate_id($tax->rate_id)), array('currency' => $this->get_order_currency())) : $tax->formatted_amount;
$tax_string_array[] = sprintf('%s %s', $tax_amount, $tax->label);
}
} else {
$tax_amount = $total_refunded && $display_refunded ? $this->get_total_tax() - $this->get_total_tax_refunded() : $this->get_total_tax();
$tax_string_array[] = sprintf('%s %s', wc_price($tax_amount, array('currency' => $this->get_order_currency())), WC()->countries->tax_or_vat());
}
if (!empty($tax_string_array)) {
$tax_string = ' ' . sprintf(__('(Includes %s)', 'woocommerce'), implode(', ', $tax_string_array));
}
}
if ($total_refunded && $display_refunded) {
$formatted_total = '<del>' . strip_tags($formatted_total) . '</del> <ins>' . wc_price($order_total - $total_refunded, array('currency' => $this->get_order_currency())) . $tax_string . '</ins>';
} else {
$formatted_total .= $tax_string;
}
return apply_filters('woocommerce_get_formatted_order_total', $formatted_total, $this);
}
示例6: set_tax_class
/**
* Set tax class.
*
* @param string $value
* @throws WC_Data_Exception
*/
public function set_tax_class($value)
{
if ($value && !in_array($value, WC_Tax::get_tax_classes())) {
$this->error('order_item_fee_invalid_tax_class', __('Invalid tax class', 'woocommerce'));
}
$this->set_prop('tax_class', $value);
}
示例7: save_tax_rate_hidden_status
/**
* Whenever a WooCommerce tax rate is saved, also save the tax rate's hidden status.
*
* Executed whenever a WooCommerce tax rate is added, updated or deleted.
*
* @param int $tax_rate_id The WooCommerce Tax Rate ID.
*/
public function save_tax_rate_hidden_status()
{
// nonce and cap checks are copied from WC_AJAX::tax_rates_save_changes()
// Use return instead of exit so that WooCommerce core will handle the AJAX error messages
if (!isset($_POST['wc_tax_nonce'], $_POST['changes'])) {
return;
}
$current_class = !empty($_POST['current_class']) ? $_POST['current_class'] : '';
// This is sanitized seven lines later.
if (!wp_verify_nonce($_POST['wc_tax_nonce'], 'wc_tax_nonce-class:' . $current_class)) {
return;
}
// Check User Caps
if (!current_user_can('manage_woocommerce')) {
return;
}
$hidden_rates = array();
$current_class = WC_Tax::format_tax_rate_class($current_class);
$rates = WC_Tax::get_rates_for_tax_class($current_class);
foreach ($rates as $tax_rate_id => $rate) {
if (isset($_POST['tax_rate_hidden'][$tax_rate_id])) {
// @codingStandardsIgnoreEnd
$hidden_rates[$tax_rate_id] = true;
} else {
if (isset($hidden_rates[$tax_rate_id])) {
unset($hidden_rates[$tax_rate_id]);
}
}
}
wc_hidden_taxes()->set_option('hidden_rates', $hidden_rates);
}
示例8: test_cart_get_discounted_price
/**
* Test some discount logic which has caused issues in the past.
* Tickets:
* https://github.com/woothemes/woocommerce/issues/10573
* https://github.com/woothemes/woocommerce/issues/10963
*
* Due to discounts being split amongst products in cart.
*/
public function test_cart_get_discounted_price()
{
global $wpdb;
// We need this to have the calculate_totals() method calculate totals
if (!defined('WOOCOMMERCE_CHECKOUT')) {
define('WOOCOMMERCE_CHECKOUT', true);
}
# Test case 1 #10963
// Create dummy coupon - fixed cart, 1 value
$coupon = WC_Helper_Coupon::create_coupon();
// Add coupon
WC()->cart->add_discount($coupon->code);
// Create dummy product - price will be 10
$product = WC_Helper_Product::create_simple_product();
// Add product to cart x1, calc and test
WC()->cart->add_to_cart($product->id, 1);
WC()->cart->calculate_totals();
$this->assertEquals('9.00', number_format(WC()->cart->total, 2, '.', ''));
$this->assertEquals('1.00', number_format(WC()->cart->discount_cart, 2, '.', ''));
// Add product to cart x2, calc and test
WC()->cart->add_to_cart($product->id, 1);
WC()->cart->calculate_totals();
$this->assertEquals('19.00', number_format(WC()->cart->total, 2, '.', ''));
$this->assertEquals('1.00', number_format(WC()->cart->discount_cart, 2, '.', ''));
// Add product to cart x3, calc and test
WC()->cart->add_to_cart($product->id, 1);
WC()->cart->calculate_totals();
$this->assertEquals('29.00', number_format(WC()->cart->total, 2, '.', ''));
$this->assertEquals('1.00', number_format(WC()->cart->discount_cart, 2, '.', ''));
// Clean up the cart
WC()->cart->empty_cart();
WC()->cart->remove_coupons();
# Test case 2 #10573
update_post_meta($product->id, '_regular_price', '29.95');
update_post_meta($product->id, '_price', '29.95');
update_post_meta($coupon->id, 'discount_type', 'percent');
update_post_meta($coupon->id, 'coupon_amount', '10');
update_option('woocommerce_prices_include_tax', 'yes');
update_option('woocommerce_calc_taxes', 'yes');
$tax_rate = array('tax_rate_country' => '', 'tax_rate_state' => '', 'tax_rate' => '10.0000', 'tax_rate_name' => 'TAX', 'tax_rate_priority' => '1', 'tax_rate_compound' => '0', 'tax_rate_shipping' => '1', 'tax_rate_order' => '1', 'tax_rate_class' => '');
WC_Tax::_insert_tax_rate($tax_rate);
$product = wc_get_product($product->id);
WC()->cart->add_to_cart($product->id, 1);
WC()->cart->add_discount($coupon->code);
WC()->cart->calculate_totals();
$cart_item = current(WC()->cart->get_cart());
$this->assertEquals('24.51', number_format($cart_item['line_total'], 2, '.', ''));
// Cleanup
$wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_tax_rates");
$wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_tax_rate_locations");
WC()->cart->empty_cart();
WC()->cart->remove_coupons();
update_option('woocommerce_prices_include_tax', 'no');
update_option('woocommerce_calc_taxes', 'no');
// Delete coupon
WC_Helper_Coupon::delete_coupon($coupon->id);
// Clean up product
WC_Helper_Product::delete_product($product->id);
}
示例9: ywev_get_tax_classes
function ywev_get_tax_classes()
{
if (version_compare(WOOCOMMERCE_VERSION, '2.3', '<')) {
return array_filter(array_map('trim', explode("\n", get_option('woocommerce_tax_classes'))));
} else {
return WC_Tax::get_tax_classes();
}
}
示例10: set_base_tax_rates
public function set_base_tax_rates($rates, $tax_class)
{
$location = WC_Tax::get_tax_location($tax_class);
if (in_array($tax_class, array('virtual-rate', 'virtual-reduced-rate')) && isset($location[0]) && sizeof($location) === 4 && $location[0] !== WC()->countries->get_base_country()) {
list($country, $state, $postcode, $city) = $location;
$rates = WC_Tax::find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class));
}
return $rates;
}
示例11: widget
/**
* Output widget.
*
* @see WP_Widget
*
* @param array $args
* @param array $instance
*/
public function widget($args, $instance)
{
global $wp, $wp_the_query;
if (!is_post_type_archive('product') && !is_tax(get_object_taxonomies('product'))) {
return;
}
if (!$wp_the_query->post_count) {
return;
}
$min_price = isset($_GET['min_price']) ? esc_attr($_GET['min_price']) : '';
$max_price = isset($_GET['max_price']) ? esc_attr($_GET['max_price']) : '';
wp_enqueue_script('wc-price-slider');
// Find min and max price in current result set
$prices = $this->get_filtered_price();
$min = floor($prices->min_price);
$max = ceil($prices->max_price);
if ($min === $max) {
return;
}
$this->widget_start($args, $instance);
if ('' === get_option('permalink_structure')) {
$form_action = remove_query_arg(array('page', 'paged'), add_query_arg($wp->query_string, '', home_url($wp->request)));
} else {
$form_action = preg_replace('%\\/page/[0-9]+%', '', home_url(trailingslashit($wp->request)));
}
/**
* Adjust max if the store taxes are not displayed how they are stored.
* Min is left alone because the product may not be taxable.
* Kicks in when prices excluding tax are displayed including tax.
*/
if (wc_tax_enabled() && 'incl' === get_option('woocommerce_tax_display_shop') && !wc_prices_include_tax()) {
$tax_classes = array_merge(array(''), WC_Tax::get_tax_classes());
$class_max = $max;
foreach ($tax_classes as $tax_class) {
if ($tax_rates = WC_Tax::get_rates($tax_class)) {
$class_max = $max + WC_Tax::get_tax_total(WC_Tax::calc_exclusive_tax($max, $tax_rates));
}
}
$max = $class_max;
}
echo '<form method="get" action="' . esc_url($form_action) . '">
<div class="price_slider_wrapper">
<div class="price_slider" style="display:none;"></div>
<div class="price_slider_amount">
<input type="text" id="min_price" name="min_price" value="' . esc_attr($min_price) . '" data-min="' . esc_attr(apply_filters('woocommerce_price_filter_widget_min_amount', $min)) . '" placeholder="' . esc_attr__('Min price', 'woocommerce') . '" />
<input type="text" id="max_price" name="max_price" value="' . esc_attr($max_price) . '" data-max="' . esc_attr(apply_filters('woocommerce_price_filter_widget_max_amount', $max)) . '" placeholder="' . esc_attr__('Max price', 'woocommerce') . '" />
<button type="submit" class="button">' . __('Filter', 'woocommerce') . '</button>
<div class="price_label" style="display:none;">
' . __('Price:', 'woocommerce') . ' <span class="from"></span> — <span class="to"></span>
</div>
' . wc_query_string_form_fields(null, array('min_price', 'max_price'), '', true) . '
<div class="clear"></div>
</div>
</div>
</form>';
$this->widget_end($args);
}
示例12: 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;
}
示例13: tax_classes
public function tax_classes()
{
$tax_classes = WC_Tax::get_tax_classes();
$classes_options = array();
if (!empty($tax_classes)) {
foreach ($tax_classes as $class) {
$classes_options[sanitize_title($class)] = esc_html($class);
}
}
return $classes_options;
}
示例14: tax_rates
/**
* Returns base tax rates for all tax classes
* @return array
*/
public static function tax_rates()
{
$rates = array();
foreach (self::tax_classes() as $class => $label) {
if ($rate = WC_Tax::get_base_tax_rates($class)) {
// WC_Tax returns a assoc array with int as keys = world of pain in js
// possibly change $key to $rate['id']
$rates[$class] = $rate;
}
}
return $rates;
}
示例15: 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;
}