Tengo un script de trabajo que agrega una tarifa para ciertos productos en una matriz. Pero solo agrega la tarifa por el primer producto de la matriz.
He probado diferentes opciones con mis conocimientos pero no funciona. ¿Algún consejo sobre lo que estoy haciendo mal?
Este es el código:
/* Add fee to specific product*/ add_action('woocommerce_cart_calculate_fees', 'statie_geld'); function statie_geld() { if (is_admin() && !defined('DOING_AJAX')) {return;} foreach( WC()->cart->get_cart() as $item_keys => $item ) { $quantiy = $item['quantity']; //get quantity from cart if( in_array( $item['product_id'], statiegeld_ids() )) { WC()->cart->add_fee(__('Statiegeld Petfles 24'), 3.60 * $quantiy ); } } } function statiegeld_ids() { return array( 4535, 4537, 89694, 89706, 3223, 4742, 14846, 26972, 32925, 32927, 32929, 37475 ); }Su código contiene algunos errores.
WC()->cart , $cart se pasa a la función$quantiy se sobrescribe en cada cicloEntonces obtienes:
function action_woocommerce_cart_calculate_fees( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Initialize $quantity = 0; // Loop though each cart item foreach ( $cart->get_cart() as $cart_item ) { // Compare if ( in_array( $cart_item['product_id'], statiegeld_ids() ) ) { // Addition // Get product quantity in cart $quantity += $cart_item['quantity']; } } // Greater than if ( $quantity > 0 ) { // Add fee $cart->add_fee( __( 'Statiegeld Petfles 24', 'woocommerce' ), 3.60 * $quantity ); } } add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 ); // Specify the product IDs function statiegeld_ids() { return array( 4535, 4537, 89694, 89706, 3223, 4742, 14846, 26972, 32925, 32927, 32929, 37475 ); }