我正在WooCommerce网站上工作.当我在结帐页面上应用优惠券时,它也会自动应用于税收.
我只希望优惠券代码适用于购物车总数.我通过谷歌搜索了一个相关的钩子或插件,但找不到有效的解决方案.
这是我当前的代码钩子functions.php,但它没有按预期工作.
add_action('woocommerce_product_tax_class', 'set_tax_class');
function set_tax_class () {
if (!empty($woocommerce->cart->applied_coupons)){
$tax_class = 'gratuty';
}
return $tax_class;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试了woocommerce_cart_calculate_fees钩子,但它没有用.
在应用优惠券时,我应该使用哪个挂钩更新购物车,而不更改税金?
我在WooCommerce中创建了一个插件,并且在向CART/CHECKOUT页面添加自定义折扣时遇到了一个小问题.
如何在不创建优惠券的情况下将自定义折扣应用于购物车?假设我想在购物车页面上给予5美元的折扣.我怎样才能做到这一点?
以下是我使用优惠券应用折扣的插件文件中的代码,但我想在不使用优惠券的情况下添加另一个自定义折扣.
插件文件中的Action Hook:
add_action('woocommerce_calculate_totals',array(&$this,'cart_order_total_action'));
Run Code Online (Sandbox Code Playgroud)
它在插件文件中的功能是:
public function cart_order_total_action(){
if ( is_user_logged_in() ){
global $woocommerce;
global $current_user;
global $wpdb;
$u_id = $current_user->ID;
$table_name = $wpdb->prefix."woocommerce_customer_reward_ms";
$thetable2 = $wpdb->prefix . "woocommerce_customer_reward_cart_ms";
$table_name3 = $wpdb->prefix."woocommerce_customer_reward_points_log_ms";
$data = $wpdb->get_row("SELECT * from $table_name where id=$u_id");
$data2 = $wpdb->get_row("SELECT * from $thetable2");
/* Order Id goes here */
$orders=array();//order ids
$args = array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => 'shop_order',
'post_status' => 'publish',
'tax_query'=>array(
array(
'taxonomy' =>'shop_order_status',
'field' => …Run Code Online (Sandbox Code Playgroud) 我需要手动设置折扣而不使用优惠券。
我已经检查了 Github 中的 Woocommerce 源,我看到它使用了“set_discount_total”函数,但它不起作用。
我试过了WC()->cart->set_discount_total(15),$order->set_discount_total(15);但没有。
这是我的代码:
<?php
function letsgo_order_total($order_id, $posted_data, $order) {
$order = wc_get_order( $order_id );
$order->set_discount_total(50);
}
add_action('woocommerce_checkout_order_processed','letsgo_order_total',10,3);
?>
Run Code Online (Sandbox Code Playgroud)
实际上我发现了一种方法,它可以工作,但我不喜欢代码:
<?php
$cart_discount = 50;
$discount = (double)get_post_meta($order_id,'_cart_discount',true);
update_post_meta($order_id,'_cart_discount',$cart_discount + $discount);
?>
Run Code Online (Sandbox Code Playgroud)