按照对上一个问题的Exclude some products from calculated additional fee in WooCommerce回答,我对前面代码中的数组进行了一些更改.
然后,我在产品编辑页面中添加了一个复选框:如果它处于活动状态(选中),则不会为该产品计税.如果该选项处于非活动状态(默认情况下未选中),则将计算该税.
1. Modify the Function to Include an Excluded Array:个
// +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 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);
}
OR this: (combined version! (maybe better all in one))个
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的核心不兼容?或者它将不会在新的和future 的版本中创建?它不会损坏数据库吗?有没有可能用更干净、更少的代码,以更好、更安全、更容易的方式完成这项工作?