// Añade esto al final del archivo, DENTRO del bloque if (defined('WP_CLI') && WP_CLI) WP_CLI::add_command('pedraja marcas', function($args, $assoc_args) { $brands = get_terms(array( 'taxonomy' => 'product_brand', 'hide_empty' => false, 'orderby' => 'name' )); if (empty($brands) || is_wp_error($brands)) { WP_CLI::log("No hay marcas."); return; } WP_CLI::log("Marcas disponibles:"); WP_CLI::log(""); foreach ($brands as $brand) { WP_CLI::log(" - {$brand->name} ({$brand->count} productos)"); } }); WP_CLI::add_command('pedraja borrar-marca', function($args, $assoc_args) { if (empty($args[0])) { WP_CLI::error('Uso: wp pedraja borrar-marca "NOMBRE_MARCA" [--delete-cats]'); } $brand_name = $args[0]; $delete_cats = isset($assoc_args['delete-cats']); // Buscar la marca $brand = get_term_by('name', $brand_name, 'product_brand'); if (!$brand) { $brand = get_term_by('slug', sanitize_title($brand_name), 'product_brand'); } if (!$brand || is_wp_error($brand)) { WP_CLI::error("Marca '{$brand_name}' no encontrada. Use: wp pedraja marcas"); } // Obtener productos $products = get_posts(array( 'post_type' => 'product', 'numberposts' => -1, 'tax_query' => array( array( 'taxonomy' => 'product_brand', 'field' => 'term_id', 'terms' => $brand->term_id ) ), 'fields' => 'ids' )); $total = count($products); if ($total === 0) { WP_CLI::log("No hay productos de la marca '{$brand->name}'."); return; } WP_CLI::log("Marca: {$brand->name}"); WP_CLI::log("Productos a borrar: $total"); if ($delete_cats) { WP_CLI::log("Se borraran tambien las categorias vacias."); } WP_CLI::log(""); WP_CLI::confirm("Estas SEGURO? Esta accion NO se puede deshacer.", $assoc_args); // Recopilar categorías si se van a borrar $categories_to_check = array(); if ($delete_cats) { foreach ($products as $product_id) { $cats = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids')); if (!is_wp_error($cats)) { $categories_to_check = array_merge($categories_to_check, $cats); } } $categories_to_check = array_unique($categories_to_check); } // Borrar productos $progress = \WP_CLI\Utils\make_progress_bar('Borrando productos', $total); $deleted = 0; foreach ($products as $product_id) { if (wp_delete_post($product_id, true)) { $deleted++; } $progress->tick(); if ($deleted % 50 === 0) { wp_cache_flush(); } } $progress->finish(); // Borrar categorías vacías $cats_deleted = 0; if ($delete_cats && !empty($categories_to_check)) { WP_CLI::log("Borrando categorias vacias..."); foreach ($categories_to_check as $cat_id) { $term = get_term($cat_id, 'product_cat'); if (!is_wp_error($term) && $term->count === 0) { wp_delete_term($cat_id, 'product_cat'); $cats_deleted++; } } } wp_cache_flush(); WP_CLI::success("Borrados: $deleted productos de '{$brand->name}'"); if ($cats_deleted > 0) { WP_CLI::success("Borradas: $cats_deleted categorias vacias"); } }); Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/importador-productos-pedraja/importador-productos-pedraja.php:1) in /var/www/html/wp-includes/pluggable.php on line 1531 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/importador-productos-pedraja/importador-productos-pedraja.php:1) in /var/www/html/wp-includes/pluggable.php on line 1534