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

147
Vistas
Guarde el valor del campo personalizado del producto en el carrito y muéstrelo en el carrito y finalice la compra

He agregado algunas opciones personalizadas en la página de un solo producto de woocommerce usando el siguiente código en functions.php de mi tema:

 function options_on_single_product(){ ?> <input type="radio" name="option1" checked="checked" value="option1"> option 1 <br /> <input type="radio" name="option1" value="option2"> option 2 <?php } add_action("woocommerce_before_add_to_cart_button", "options_on_single_product");

Ahora quiero mostrar el valor de la opción seleccionada en la página del carrito. Por favor, ayúdame a hacer esto. Gracias

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

0

Aquí está el código completo para almacenar el campo personalizado del producto en el objeto del carrito y mostrarlo en las páginas del carrito y el pago:

 // Output the Custom field in Product pages add_action("woocommerce_before_add_to_cart_button", "options_on_single_product", 1); function options_on_single_product(){ ?> <label for="custom_field"> <input type="radio" name="custom_field" checked="checked" value="option1"> option 1 <br /> <input type="radio" name="custom_field" value="option2"> option 2 </label> <br /> <?php } // Stores the custom field value in Cart object add_filter( 'woocommerce_add_cart_item_data', 'save_custom_product_field_data', 10, 2 ); function save_custom_product_field_data( $cart_item_data, $product_id ) { if( isset( $_REQUEST['custom_field'] ) ) { $cart_item_data[ 'custom_field' ] = esc_attr($_REQUEST['custom_field']); // below statement make sure every add to cart action as unique line item $cart_item_data['unique_key'] = md5( microtime().rand() ); } return $cart_item_data; } // Outuput custom Item value in Cart and Checkout pages add_filter( 'woocommerce_get_item_data', 'output_custom_product_field_data', 10, 2 ); function output_custom_product_field_data( $cart_data, $cart_item ) { if( isset( $cart_item['custom_field'] ) ) { $cart_data[] = array( 'key' => __('Custom Item', 'woocommerce'), 'value' => $cart_item['custom_field'], 'display' => $cart_item['custom_field'], ); } return $cart_data; }

El código va en el archivo function.php de su tema secundario activo (o tema) o también en cualquier archivo de complemento.

Este código está probado y funciona.

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