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

142
Views
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

about 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

about 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!