| |
| <?php
|
|
|
|
|
|
|
|
|
| function validar_produto_woocommerce_completo($post_id) {
|
|
|
| if (get_post_type($post_id) !== 'product') {
|
| return;
|
| }
|
|
|
|
|
| if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
|
| return;
|
| }
|
|
|
|
|
| if (!current_user_can('edit_post', $post_id)) {
|
| return;
|
| }
|
|
|
|
|
| $product = wc_get_product($post_id);
|
| if (!$product) {
|
| return;
|
| }
|
|
|
| $errors = [];
|
|
|
|
|
| $regular_price = $product->get_regular_price();
|
| if (empty($regular_price)) {
|
| $errors[] = 'preço';
|
| }
|
|
|
|
|
| $product_cats = get_the_terms($post_id, 'product_cat');
|
| if (empty($product_cats) || is_wp_error($product_cats)) {
|
| $errors[] = 'categoria';
|
| }
|
|
|
|
|
| $sku = $product->get_sku();
|
| if (empty($sku)) {
|
| $errors[] = 'referência (SKU)';
|
| }
|
|
|
|
|
| $weight = $product->get_weight();
|
| if (empty($weight)) {
|
| $errors[] = 'peso';
|
| }
|
|
|
|
|
| if (!empty($errors)) {
|
| $error_message = '<strong>Atenção:</strong> O produto <a href="' . get_edit_post_link($post_id) . '">' . get_the_title($post_id) . '</a> está incompleto. ';
|
| $error_message .= 'Falta preencher: ' . implode(', ', $errors) . '.';
|
|
|
| WC_Admin_Notices::add_custom_notice(
|
| 'produto_incompleto_' . $post_id,
|
| $error_message
|
| );
|
| } else {
|
|
|
| WC_Admin_Notices::remove_notice('produto_incompleto_' . $post_id);
|
| }
|
| }
|
| add_action('save_post', 'validar_produto_woocommerce_completo', 20);
|
|
|
|
|
|
|
|
|
| function validar_produto_completo_js() {
|
| global $post_type;
|
|
|
|
|
| if ($post_type !== 'product') {
|
| return;
|
| }
|
|
|
| ?>
|
| <script type="text/javascript">
|
| jQuery(document).ready(function($) {
|
|
|
| function verificarProdutoAntesDePublicar() {
|
| var erros = [];
|
|
|
|
|
| var regularPrice = $('#_regular_price').val();
|
| if (!regularPrice || regularPrice === '') {
|
| erros.push('preço');
|
| }
|
|
|
|
|
|
|
| var temCategoria = false;
|
| $('#product_catchecklist input[type="checkbox"]').each(function() {
|
| if ($(this).is(':checked')) {
|
| temCategoria = true;
|
| return false;
|
| }
|
| });
|
| if (!temCategoria) {
|
| erros.push('categoria');
|
| }
|
|
|
|
|
| var sku = $('#_sku').val();
|
| if (!sku || sku === '') {
|
| erros.push('referência (SKU)');
|
| }
|
|
|
|
|
| var weight = $('#_weight').val();
|
| if (!weight || weight === '') {
|
| erros.push('peso');
|
| }
|
|
|
|
|
| if (erros.length > 0) {
|
| alert('ATENÇÃO: O produto está incompleto. Faltam os seguintes campos obrigatórios: ' + erros.join(', ') + '.');
|
| return false;
|
| }
|
|
|
| return true;
|
| }
|
|
|
|
|
| $('#post').on('submit', function(e) {
|
|
|
| if ($('#publish').is(':focus') || $('#save-post').is(':focus')) {
|
| return verificarProdutoAntesDePublicar();
|
| }
|
| });
|
|
|
|
|
| $('#publish, #save-post').on('click', function(e) {
|
| return verificarProdutoAntesDePublicar();
|
| });
|
| });
|
| </script>
|
| <?php
|
| }
|
| add_action('admin_footer', 'validar_produto_completo_js');
|
|
|
|
|
|
|
|
|
| function destacar_categorias_pai_produto() {
|
| global $post_type;
|
|
|
|
|
| if ($post_type !== 'product') {
|
| return;
|
| }
|
|
|
| echo '<style>
|
| /* Aumentar o tamanho da caixa de categorias */
|
| .categorydiv div.tabs-panel,
|
| #taxonomy-product_cat .categorychecklist {
|
| max-height: 500px !important;
|
| height: auto !important;
|
| overflow: auto;
|
| }
|
|
|
| #product_catchecklist,
|
| #product_catchecklist-pop,
|
| #taxonomy-product_cat .categorychecklist {
|
| height: 500px !important;
|
| max-height: 500px !important;
|
| }
|
|
|
| .categorydiv .tabs-panel {
|
| min-height: 42px;
|
| height: 500px !important;
|
| overflow: auto;
|
| padding: 0 0.9em;
|
| }
|
|
|
| /* Deixar categorias pai em negrito */
|
| #product_catchecklist li.popular-category > label,
|
| #product_catchecklist > li > label,
|
| #product_cat-checklist li.popular-category > label,
|
| #product_cat-checklist > li > label {
|
| font-weight: bold !important;
|
| }
|
|
|
| /* Garantir que apenas categorias de primeiro nível (pai) fiquem em negrito */
|
| #product_catchecklist ul li > label,
|
| #product_cat-checklist ul li > label {
|
| font-weight: normal !important;
|
| }
|
|
|
| /* Estilo para destacar campos obrigatórios */
|
| ._regular_price_field label,
|
| ._sku_field label,
|
| ._weight_field label,
|
| #taxonomy-product_cat h2 {
|
| position: relative;
|
| }
|
|
|
| ._regular_price_field label:after,
|
| ._sku_field label:after,
|
| ._weight_field label:after,
|
| #taxonomy-product_cat h2:after {
|
| content: " *";
|
| color: red;
|
| font-weight: bold;
|
| }
|
| </style>';
|
|
|
|
|
| echo '<script type="text/javascript">
|
| jQuery(document).ready(function($) {
|
| // Aplicar negrito às categorias pai
|
| function boldParentCategories() {
|
| $("#product_catchecklist > li > label, #product_cat-checklist > li > label").css("font-weight", "bold");
|
| $("#product_catchecklist ul li > label, #product_cat-checklist ul li > label").css("font-weight", "normal");
|
| }
|
|
|
| // Executar na inicialização
|
| boldParentCategories();
|
|
|
| // Executar quando categorias forem atualizadas/carregadas
|
| $(document).ajaxComplete(function() {
|
| setTimeout(boldParentCategories, 200);
|
| });
|
| });
|
| </script>';
|
| }
|
| add_action('admin_head-post.php', 'destacar_categorias_pai_produto');
|
| add_action('admin_head-post-new.php', 'destacar_categorias_pai_produto');
|
| |
| |
Comments