This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Permite mostrar contenido personalizado en la parte superior de la ficha de producto | |
| if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ){ | |
| // Muestra el contenido si lo hay | |
| add_action( 'woocommerce_before_single_product_summary', 'add_custom_content_before_main_content_of_product', 5 ); | |
| function add_custom_content_before_main_content_of_product(){ | |
| global $post; | |
| $top_content = get_post_meta( $post->ID, 'top_content', true ); | |
| if ( !empty( $top_content ) ) { | |
| ?> | |
| <div style="margin-bottom: 50px;"><?php echo $top_content; ?></div> | |
| <?php | |
| } | |
| } | |
| // Crea un nuevo metabox en la página de edición del producto para editar el contenido a mostrar | |
| add_action( 'add_meta_boxes', 'woo_update_notification_metabox' ); | |
| function woo_update_notification_metabox(){ | |
| add_meta_box( 'woo-top-content-box', 'Información adicional destacada antes del producto', 'render_woo_top_content_metabox', 'product', 'normal', 'default' ); | |
| } | |
| // Carga el metabox | |
| function render_woo_top_content_metabox( $post ){ | |
| $settings = array( | |
| 'textarea_name' => 'top_content', | |
| 'textarea_rows' => 10, | |
| 'quicktags' => array( 'buttons' => 'em,strong,link' ), | |
| 'tinymce' => array( | |
| 'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator', | |
| 'theme_advanced_buttons2' => '', | |
| ), | |
| 'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>' | |
| ); | |
| $top_content = get_post_meta( $post->ID, 'top_content', true ); | |
| wp_editor( htmlspecialchars_decode( $top_content ), 'top_content', $settings ); | |
| wp_nonce_field( 'woo-top-content-box-nonce', 'top_content_nonce' ); | |
| } | |
| // Guarda el nuevo contenido en la ficha de producto | |
| add_action( 'save_post', 'woo_top_content_metabox_save' ); | |
| function woo_top_content_metabox_save( $post_id ){ | |
| // Bail if we're doing an auto save | |
| if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; | |
| // if our nonce isn't there, or we can't verify it, bail | |
| if( !isset( $_POST[ 'top_content_nonce' ] ) || !wp_verify_nonce( $_POST[ 'top_content_nonce' ], 'woo-top-content-box-nonce' ) ) return; | |
| // if our current user can't edit this post, bail | |
| if( 'product' == $_POST['post_type'] ) { | |
| if( !current_user_can('edit_page', $post_id) ) { | |
| return $post_id; | |
| } else { | |
| $top_content = isset( $_POST[ 'top_content' ] )? $_POST[ 'top_content' ] : ''; | |
| update_post_meta( $post_id, 'top_content', $top_content ); | |
| } | |
| } | |
| } | |
| // Mueve la etiqueta de oferta dentro del wrapper de la imagen de producto | |
| remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 ); | |
| add_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_sale_flash' ); | |
| } |