/* Plugin Name: Custom CRV-TAX Version: 1.2 Description: Add a fixed price CRV TAX to products in a specific category. Author: Carlos Escamilla */ add_action('woocommerce_cart_calculate_fees', 'add_crv_tax'); function add_crv_tax() { if (is_admin() && !defined('DOING_AJAX')) return; // Verificar si el usuario actual tiene los permisos necesarios if (!current_user_can('manage_woocommerce')) return; $category_id = 80; // Category ID $crv_tax = 0.10; // CRV TAX // Inicializar impuestos totales $total_tax = 0; // Obtener los productos en el carrito $cart_items = WC()->cart->get_cart(); // Iterar sobre los productos en el carrito foreach ($cart_items as $cart_item_key => $cart_item) { // Verificar si el producto pertenece a la categoría especificada if (has_term($category_id, 'product_cat', $cart_item['product_id'])) { // Obtener el número de unidades del producto $units = isset($cart_item['quantity']) ? $cart_item['quantity'] : 1; // Obtener el valor del atributo "Unidades" si está definido if (!empty($cart_item['data']->get_attributes())) { foreach ($cart_item['data']->get_attributes() as $attribute) { if ($attribute->get_name() === 'Units') { $units = (int)$attribute->get_options()[0]; break; } } } // Calcular el impuesto para este producto y agregarlo a los impuestos totales $total_tax += $units * $crv_tax * $cart_item['quantity']; } } // Agregar impuesto como una tarifa al total del carrito if ($total_tax > 0) { WC()->cart->add_fee('CRV TAX', $total_tax, false); } } ?>
No account yet?
Create an Account