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

115
Views
PHP modifica la variable obtenida y vuelve a la interfaz para JS

Estoy obteniendo atributos de productos de Woocommerce y repitiéndolos en una etiqueta de secuencia de comandos como variable para usar con javascript en la interfaz. Esto podría ser una mala práctica, siéntete libre de iluminarme.

Ejemplo:

Atributos del producto:

Altura total: 43m

Longitud total: 55m

PHP consulta "Altura total" como nombre de atributo y "43m" como valor de atributo. PHP reemplaza el espacio vacío con "-". No puedo definir una var de javascript con "-" en el nombre de la var. Ejemplo: var Total-height = "43m";

¿Cómo podría solucionar este problema? Aquí está mi código. Gracias por adelantado.

 function product_attribute_dimensions(){ global $product; foreach ($product->get_attributes() as $taxonomy => $attribute_obj ) { // Get the attribute label $attribute_label_name = wc_attribute_label($taxonomy); $value = $product->get_attribute($taxonomy); if ($value) { $label = get_taxonomy($taxonomy)->labels->singular_name; $profile_one = $value; echo '<script>' . $attribute_label_name . ' = "' . $value . '"; </script>'; } }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Según tengo entendido, ¿la cadena generada en la variable "$attribute_label_name" es el problema? Eche un vistazo a https://www.php.net/manual/de/function.str-replace.php

Con esta función nativa de PHP, puede buscar un carácter (p. ej., "-") y reemplazarlo con otra cosa (p. ej., "_") echo '<script>' . str_replace("-", "_", $attribute_label_name) . ' = "' . $value . '";

Pero como dijiste, este podría no ser el mejor enfoque. Personalmente, agregaría este tipo de información en un atributo HTML "data-X" en algún elemento HTML y lo extraería en mi JS. Similar a ésto:

 <div id="some_element" class="could be hidden" data-total-height="<?= $value ?>"></div>

Podría consultar algo como esto con jQuery $("#some_element").attr("data-total-height")

about 4 years ago · Juan Pablo Isaza Report

0

intenta usar window["variable_name"] haz esto:

 echo '<script>window["' . $attrname . '"]=' . $attrval

luego en tu js:

 let this_var = window[attrname]

Parece la forma más clara y corta de hacer esto.

about 4 years ago · Juan Pablo Isaza Report

0

function product_attribute_dimensions() { global $product; $product_atrributes = array(); foreach ($product->get_attributes() as $taxonomy => $attribute_obj) { // Get the attribute label $attribute_label_name = wc_attribute_label($taxonomy); $attribute_label_name = str_replace(' ', '_', $attribute_label_name); $value = $product->get_attribute($taxonomy); if ($value) { $product_atrributes[$attribute_label_name] = $value; } } echo '<script type="text/javascript">var product_atrributes = ' . json_encode($product_atrributes) . ';</script>'; }

Ahora puede usar en JS como product_atrributes.Total_height.value De esta manera, también se puede evitar la etiqueta de secuencia de comandos redundante.

about 4 years ago · Juan Pablo Isaza 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!