Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Agregar tarifa para ciertos productos en el carrito de WooCommerce

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 ); }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Su código contiene algunos errores.

  • No es necesario usar WC()->cart , $cart se pasa a la función
  • $quantiy se sobrescribe en cada ciclo
  • Lo mismo para agregar la tarifa, esto se sobrescribe en cada bucle

Entonces 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 ); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!