wordpress - 在 WooCommerce 中排除计算税的产品
问题描述
税收是小费,是客户选择的购物车总数的百分比,但它应该只计算一个类别(食品)的总数,而排除另一个(活动门票)。
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
function woo_add_cart_fee( $cart ){
if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
return;
}
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST;
}
if (isset($post_data['propina'])) {
global $woocommerce;
$porcentaje = $post_data['propina'] / 100;
$propina = ( $woocommerce->cart->cart_contents_total ) * $porcentaje;
WC()->cart->add_fee( 'Propina Sugerida:', $propina );
}
}
简而言之,($woocommerce->cart->cart_contents_total -活动门票总价)* 百分比
编辑:我想我找到了解决方案,但是当产品是变体时我遇到了问题
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
function woo_add_cart_fee( $cart ){
if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
return;
}
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST;
}
if (isset($post_data['propina'])) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$product_in_cart = false;
$ticketpamount = 0;
foreach($items as $item => $values) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// second level loop search, in case some items have several categories
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if ( $_categoryid === 122 ) {
//category is in cart!
$price = get_post_meta($values['product_id'] , '_price', true);
$ticketpamount += $price;
$product_in_cart = true;
}
}
}
$porcentaje = $post_data['propina'] / 100;
if ( $product_in_cart ) {
$propina = ( $woocommerce->cart->cart_contents_total - $ticketpamount ) * $porcentaje;
} else {
$propina = ( $woocommerce->cart->cart_contents_total ) * $porcentaje;
}
WC()->cart->add_fee( 'Propina Sugerida:', $propina );
}
}
解决方案
找到解决方案,添加变化并乘以数量
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
function woo_add_cart_fee( $cart ){
if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
return;
}
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST;
}
if (isset($post_data['propina'])) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$product_in_cart = false;
$ticketpamount = 0;
foreach($items as $item => $values) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// second level loop search, in case some items have several categories
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if ( $_categoryid === 122 ) {
//category is in cart!
//check if product is a variation and * quantity
if ( $item['variation_id'] ) {
$price = get_post_meta($values['variation_id'] , '_price', true) * $values['quantity'];
} else {
$price = get_post_meta($values['product_id'] , '_price', true);
}
$ticketpamount += $price;
$product_in_cart = true;
}
}
}
$porcentaje = $post_data['propina'] / 100;
if ( $product_in_cart ) {
$propina = ( $woocommerce->cart->cart_contents_total - $ticketpamount ) * $porcentaje;
} else {
$propina = ( $woocommerce->cart->cart_contents_total ) * $porcentaje;
}
WC()->cart->add_fee( 'Propina Sugerida:', $propina );
}
}
推荐阅读
- c# - 方法在上传到服务器端 asp.net 上的数据库时给出日期时间转换异常
- java - Intellij Idea CE 2018.2.1 的输出目录中不包含资源目录
- regex - lua中的正则表达式模式问题
- mysql - 用于选择多行作为这些行数据的数组的 MySQL 查询
- css - 为什么将鼠标悬停在错误的元素上会触发我的 CSS 动画?
- postgresql - 在 POSTGRESQL 中使用 JPA 获取 SELECT 查询中的唯一行
- javascript - 如何从 json 中的路由转换 res 而不是回调函数中的数组?
- excel - 有没有办法在离开程序的同时最小化/分离工作簿窗口?
- javascript - Antd 日期选择器掩码
- python - Atom(Mac)中的Python清除输出?