Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

364
Vistas
Aumente los precios de los artículos del carrito según el método de pago en WooCommerce

Quiero agregar un valor porcentual a los precios de los artículos del carrito en función de la pasarela de pago seleccionada.

El problema al que me enfrento es que el precio del producto de variación no se actualiza para el precio del producto. El precio seleccionado inicialmente se muestra todo el tiempo.

¿Cómo puedo obtener el precio modificado en consecuencia?

Mi código hasta ahora:

 // Set custom cart item price function add_custom_price( $cart ) { // This is necessary for WC 3.0+ if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Avoiding hook repetition (when using price calculations for example | optional) if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; // Loop through cart items foreach ( $cart->get_cart() as $cart_item ) { $chosen_payment_method = WC()->session->get('chosen_payment_method'); if($chosen_payment_method == 'cod') { $increaseby = 3; } elseif($chosen_payment_method == 'paypal') { $increaseby = 8; } else { $increaseby = 10; } $price = get_post_meta($cart_item['product_id'] , '_price', true); $price = $price + (($price * $increaseby)/100); $cart_item['data']->set_price( $price ); } } add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);

Cualquier ayuda muy apreciada.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Hay algunos errores en tu código.

  • Use WC()->session->get( 'chosen_payment_method' ); fuera del bucle foreach
  • get_post_meta() no es necesario para obtener el precio, puede usar get_price()
  • También necesitará jQuery que se activa al cambiar el método de pago.

Entonces obtienes:

 function action_woocommerce_before_calculate_totals( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; // Get payment method $chosen_payment_method = WC()->session->get( 'chosen_payment_method' ); // Compare if ( $chosen_payment_method == 'cod' ) { $increaseby = 3; } elseif ( $chosen_payment_method == 'paypal' ) { $increaseby = 8; } else { $increaseby = 10; } // Loop through cart items foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) { // Get price $price = $cart_item['data']->get_price(); // Set price $cart_item['data']->set_price( $price + ( $price * $increaseby ) / 100 ); } } add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 ); function action_wp_footer() { if ( is_checkout() && ! is_wc_endpoint_url() ) : ?> <script type="text/javascript"> jQuery(function($){ $( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', function() { $(document.body).trigger( 'update_checkout' ); }); }); </script> <?php endif; } add_action( 'wp_footer', 'action_wp_footer' );
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda