Script Php: differenze tra le versioni
Da Webmobili Wiki.
| (3 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 26: | Riga 26: | ||
$fornitori = wp_get_post_terms($product->get_id(), 'pa_fornitore'); | $fornitori = wp_get_post_terms($product->get_id(), 'pa_fornitore'); | ||
$codici_fornitore = wp_get_post_terms($product->get_id(), 'pa_codice_fornitore'); | $codici_fornitore = wp_get_post_terms($product->get_id(), 'pa_codice_fornitore'); | ||
</syntaxhighlight> | |||
'''Recuperare tutti i termini (categorie + tag + attributi globali)''' | |||
<syntaxhighlight lang="php"> | |||
$taxonomies = get_object_taxonomies('product'); | |||
foreach ($taxonomies as $taxonomy) { | |||
$terms = wp_get_post_terms($product->get_id(), $taxonomy); | |||
if (!empty($terms)) { | |||
echo "<strong>{$taxonomy}:</strong> "; | |||
echo implode(', ', wp_list_pluck($terms, 'name')); | |||
echo "<br>"; | |||
} | |||
} | |||
</syntaxhighlight> | |||
=== Attributi personalizzati (NON globali) === | |||
Gli attributi personalizzati (non globali) che inserisci a mano nella scheda prodotto non sono tassonomie: sono salvati come metadati in _product_attributes. | |||
Quindi per quelli bisogna usare: | |||
<syntaxhighlight lang="php"> | |||
$attributes = $product->get_attributes(); | |||
</syntaxhighlight> | |||
=== Codice per recuperare sia attributi globali che custom === | |||
<syntaxhighlight lang="php"> | |||
$product_id = 123; // ID del prodotto da controllare | |||
$product = wc_get_product($product_id); | |||
// 1. Recupera categorie e tag | |||
$categories = wp_get_post_terms($product->get_id(), 'product_cat'); | |||
$tags = wp_get_post_terms($product->get_id(), 'product_tag'); | |||
echo "<h3>Categorie</h3>"; | |||
echo implode(', ', wp_list_pluck($categories, 'name')) . "<br>"; | |||
echo "<h3>Tag</h3>"; | |||
echo implode(', ', wp_list_pluck($tags, 'name')) . "<br>"; | |||
// 2. Recupera attributi globali (tassonomie con prefisso pa_) | |||
$taxonomies = get_object_taxonomies('product'); | |||
echo "<h3>Attributi Globali</h3>"; | |||
foreach ($taxonomies as $taxonomy) { | |||
if (strpos($taxonomy, 'pa_') === 0) { // solo gli attributi | |||
$terms = wp_get_post_terms($product->get_id(), $taxonomy); | |||
if (!empty($terms)) { | |||
echo ucfirst(str_replace('pa_', '', $taxonomy)) . ": "; | |||
echo implode(', ', wp_list_pluck($terms, 'name')); | |||
echo "<br>"; | |||
} | |||
} | |||
} | |||
// 3. Recupera attributi personalizzati (non globali) | |||
echo "<h3>Attributi Personalizzati</h3>"; | |||
$attributes = $product->get_attributes(); | |||
foreach ($attributes as $attribute) { | |||
if ($attribute->is_taxonomy()) { | |||
// già gestito sopra (pa_*), lo salto | |||
continue; | |||
} | |||
$name = $attribute->get_name(); | |||
$options = $attribute->get_options(); | |||
echo ucfirst($name) . ": " . implode(', ', $options) . "<br>"; | |||
} | |||
</syntaxhighlight> | |||
=== Errori === | |||
Mostrare gli errori solo in pagine specifiche. | |||
<syntaxhighlight lang="php"> | |||
add_action('admin_init', function() { | |||
if (isset($_GET['page']) && $_GET['page'] === 'designbest-esw/config') { | |||
ini_set('display_errors', 1); | |||
ini_set('display_startup_errors', 1); | |||
error_reporting(E_ALL); | |||
} | |||
}); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Versione attuale delle 14:54, 5 dic 2025
Woocommerce
[modifica]Qualsiasi tassonomia associata ai prodotti in WooCommerce.
Tassonomie standard WooCommerce
[modifica]- Categorie prodotti
$categories = wp_get_post_terms($product->get_id(), 'product_cat');
- Tagprodotti
$tags = wp_get_post_terms($product->get_id(), 'product_tag');
Attributi globali WooCommerce
[modifica]Gli attributi globali vengono registrati come tassonomie con prefisso pa_. Ad esempio:
- Attributo brand → tassonomia pa_brand
- Attributo fornitore → tassonomia pa_fornitore
- Attributo codice_fornitore → tassonomia pa_codice_fornitore
Quindi puoi recuperarli così:
$brands = wp_get_post_terms($product->get_id(), 'pa_brand');
$fornitori = wp_get_post_terms($product->get_id(), 'pa_fornitore');
$codici_fornitore = wp_get_post_terms($product->get_id(), 'pa_codice_fornitore');
Recuperare tutti i termini (categorie + tag + attributi globali)
$taxonomies = get_object_taxonomies('product');
foreach ($taxonomies as $taxonomy) {
$terms = wp_get_post_terms($product->get_id(), $taxonomy);
if (!empty($terms)) {
echo "<strong>{$taxonomy}:</strong> ";
echo implode(', ', wp_list_pluck($terms, 'name'));
echo "<br>";
}
}
Attributi personalizzati (NON globali)
[modifica]Gli attributi personalizzati (non globali) che inserisci a mano nella scheda prodotto non sono tassonomie: sono salvati come metadati in _product_attributes. Quindi per quelli bisogna usare:
$attributes = $product->get_attributes();
Codice per recuperare sia attributi globali che custom
[modifica]$product_id = 123; // ID del prodotto da controllare
$product = wc_get_product($product_id);
// 1. Recupera categorie e tag
$categories = wp_get_post_terms($product->get_id(), 'product_cat');
$tags = wp_get_post_terms($product->get_id(), 'product_tag');
echo "<h3>Categorie</h3>";
echo implode(', ', wp_list_pluck($categories, 'name')) . "<br>";
echo "<h3>Tag</h3>";
echo implode(', ', wp_list_pluck($tags, 'name')) . "<br>";
// 2. Recupera attributi globali (tassonomie con prefisso pa_)
$taxonomies = get_object_taxonomies('product');
echo "<h3>Attributi Globali</h3>";
foreach ($taxonomies as $taxonomy) {
if (strpos($taxonomy, 'pa_') === 0) { // solo gli attributi
$terms = wp_get_post_terms($product->get_id(), $taxonomy);
if (!empty($terms)) {
echo ucfirst(str_replace('pa_', '', $taxonomy)) . ": ";
echo implode(', ', wp_list_pluck($terms, 'name'));
echo "<br>";
}
}
}
// 3. Recupera attributi personalizzati (non globali)
echo "<h3>Attributi Personalizzati</h3>";
$attributes = $product->get_attributes();
foreach ($attributes as $attribute) {
if ($attribute->is_taxonomy()) {
// già gestito sopra (pa_*), lo salto
continue;
}
$name = $attribute->get_name();
$options = $attribute->get_options();
echo ucfirst($name) . ": " . implode(', ', $options) . "<br>";
}
Errori
[modifica]Mostrare gli errori solo in pagine specifiche.
add_action('admin_init', function() {
if (isset($_GET['page']) && $_GET['page'] === 'designbest-esw/config') {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
});