在 从 WooCommerce 中计算的额外费用中排除某些产品回答我之前的问题之后,我对之前的代码中的数组进行了一些更改。
然后,我在产品编辑页面添加了一个复选框:如果它处于活动状态(选中),则不会计算该产品的税费。如果它处于非活动状态(默认情况下未选中),则会计算税费。
1。修改函数以包含排除的数组:
// +9% tax add-fee on paypal
add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway', 10, 1 );
function add_checkout_fee_for_gateway( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Retrieve excluded product IDs from post meta
$excluded_product_ids = get_option('excluded_product_ids', array());
// Only on checkout page and for specific payment method ID
if ( is_checkout() && ! is_wc_endpoint_url()
&& WC()->session->get('chosen_payment_method') === 'paypal' ) {
$percentage_rate = 0.09; // Defined percentage rate
$custom_subtotal = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $item ) {
// Calculate items subtotal from non excluded products
if( ! in_array($item['product_id'], $excluded_product_ids) ) {
$custom_subtotal += (float) $item['line_subtotal'];
}
}
if ( $custom_subtotal > 0 ) {
$cart->add_fee( __('9% value added tax'), ($custom_subtotal * $percentage_rate), true, '' );
}
}
}
2。添加复选框到产品编辑页面:
// Add checkbox to product edit page
add_action('woocommerce_product_options_general_product_data', 'add_custom_product_field');
function add_custom_product_field() {
global $post;
// Checkbox field
woocommerce_wp_checkbox(
array(
'id' => 'exclude_from_tax',
'wrapper_class' => 'show_if_simple',
'label' => __('Exclude from Tax Calculation'),
'description' => __('Check this box to exclude this product from tax calculation.'),
'value' => get_post_meta($post->ID, 'exclude_from_tax', true) ? 'yes' : 'no',
)
);
}
// Save checkbox value
add_action('woocommerce_process_product_meta', 'save_custom_product_field');
function save_custom_product_field($post_id) {
// Checkbox field
$checkbox = isset($_POST['exclude_from_tax']) ? 'yes' : 'no';
update_post_meta($post_id, 'exclude_from_tax', $checkbox);
}
或者这个:(组合版本!(也许更好))
add_action('woocommerce_cart_calculate_fees', 'add_checkout_fee_and_product_field', 10, 1);
function add_checkout_fee_and_product_field($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
// Retrieve excluded product IDs from post meta
$excluded_product_ids = get_option('excluded_product_ids', array());
// Add checkbox to product edit page
add_action('woocommerce_product_options_general_product_data', 'add_custom_product_field');
function add_custom_product_field() {
global $post;
// Checkbox field
woocommerce_wp_checkbox(
array(
'id' => 'exclude_from_tax',
'wrapper_class' => 'show_if_simple',
'label' => __('Exclude from Tax Calculation'),
'description' => __('Check this box to exclude this product from tax calculation.'),
'value' => get_post_meta($post->ID, 'exclude_from_tax', true) ? 'yes' : 'no',
)
);
}
// Save checkbox value
add_action('woocommerce_process_product_meta', 'save_custom_product_field');
function save_custom_product_field($post_id) {
// Checkbox field
$checkbox = isset($_POST['exclude_from_tax']) ? 'yes' : 'no';
update_post_meta($post_id, 'exclude_from_tax', $checkbox);
}
// Only on checkout page and for specific payment method ID
if (is_checkout() && !is_wc_endpoint_url() && WC()->session->get('chosen_payment_method') === 'paypal') {
$percentage_rate = 0.09; // Defined percentage rate
$custom_subtotal = 0; // Initializing
// Loop through cart items
foreach ($cart->get_cart() as $item) {
// Calculate items subtotal from non excluded products
if (!in_array($item['product_id'], $excluded_product_ids)) {
$custom_subtotal += (float)$item['line_subtotal'];
}
}
if ($custom_subtotal > 0) {
$cart->add_fee(__('9% value added tax'), ($custom_subtotal * $percentage_rate), true, '');
}
}
}
现在不知道我的方法和代码是否正确?实际上是否与 WordPress 和 WooCommerce 的插件、主题或核心不兼容?或者它不会在新的和未来的版本中创建?不会损坏数据库吗?是否可以用更干净、更少的代码以更好、更安全、更简单的方式做到这一点?
您的代码中存在一些错误...请尝试以下修改后的代码版本:
// Percentage tax fee for paypal payment
add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway', 10, 1 );
function add_checkout_fee_for_gateway( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only on checkout page and for specific payment method ID
if ( is_checkout() && ! is_wc_endpoint_url()
&& WC()->session->get('chosen_payment_method') === 'paypal' ) {
// Retrieve excluded product IDs from options
$percentage_rate = 0.09; // Defined percentage rate
$custom_subtotal = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $item ) {
$product = wc_get_product( $item['product_id'] );
// Calculate items subtotal from non excluded products
if( $product->get_meta('_tax_fee_excluded') !== 'yes' ) {
$custom_subtotal += (float) $item['line_subtotal'];
}
}
if ( $custom_subtotal > 0 ) {
$cart->add_fee( __('9% value added tax'), ($custom_subtotal * $percentage_rate), true, '' );
}
}
}
// Add a checkbox to product edit page
add_action('woocommerce_product_options_general_product_data', 'add_admin_product_custom_field');
function add_admin_product_custom_field() {
// Checkbox field
woocommerce_wp_checkbox( array(
'id' => '_tax_fee_excluded',
'wrapper_class' => 'show_if_simple',
'label' => __('Exclude from Tax Calculation'),
'description' => __('Check this box to exclude this product from tax calculation.')
) );
}
// Save checkbox value from product edit page
add_action('woocommerce_admin_process_product_object', 'save_admin_product_custom_field_value');
function save_admin_product_custom_field_value( $product ) {
$product->update_meta_data('_tax_fee_excluded', isset($_POST['_tax_fee_excluded']) ? 'yes' : 'no');
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。