Guest User

Untitled

a guest
Aug 31st, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1.  
  2. /**
  3. * @snippet Add new textarea to Product Category Pages - WooCommerce
  4. * @how-to Get CustomizeWoo.com FREE
  5. * @author Rodolfo Melogli
  6. * @compatible WooCommerce 5
  7. * @donate $9 https://businessbloomer.com/bloomer-armada/
  8. */
  9.  
  10. // ---------------
  11. // 1. Display field on "Add new product category" admin page
  12.  
  13. add_action( 'product_cat_add_form_fields', 'bbloomer_wp_editor_add', 10, 2 );
  14.  
  15. function bbloomer_wp_editor_add() {
  16. ?>
  17. <div class="form-field">
  18. <label for="seconddesc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label>
  19.  
  20. <?php
  21. $settings = array(
  22. 'textarea_name' => 'seconddesc',
  23. 'quicktags' => array( 'buttons' => 'em,strong,link' ),
  24. 'tinymce' => array(
  25. 'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
  26. 'theme_advanced_buttons2' => '',
  27. ),
  28. 'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
  29. );
  30.  
  31. wp_editor( '', 'seconddesc', $settings );
  32. ?>
  33.  
  34. <p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
  35. </div>
  36. <?php
  37. }
  38.  
  39. // ---------------
  40. // 2. Display field on "Edit product category" admin page
  41.  
  42. add_action( 'product_cat_edit_form_fields', 'bbloomer_wp_editor_edit', 10, 2 );
  43.  
  44. function bbloomer_wp_editor_edit( $term ) {
  45. $second_desc = htmlspecialchars_decode( get_term_meta( $term->term_id, 'seconddesc', true ) );
  46. ?>
  47. <tr class="form-field">
  48. <th scope="row" valign="top"><label for="second-desc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label></th>
  49. <td>
  50. <?php
  51.  
  52. $settings = array(
  53. 'textarea_name' => 'seconddesc',
  54. 'quicktags' => array( 'buttons' => 'em,strong,link' ),
  55. 'tinymce' => array(
  56. 'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
  57. 'theme_advanced_buttons2' => '',
  58. ),
  59. 'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
  60. );
  61.  
  62. wp_editor( $second_desc, 'seconddesc', $settings );
  63. ?>
  64.  
  65. <p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
  66. </td>
  67. </tr>
  68. <?php
  69. }
  70.  
  71. // ---------------
  72. // 3. Save field @ admin page
  73.  
  74. add_action( 'edit_term', 'bbloomer_save_wp_editor', 10, 3 );
  75. add_action( 'created_term', 'bbloomer_save_wp_editor', 10, 3 );
  76.  
  77. function bbloomer_save_wp_editor( $term_id, $tt_id = '', $taxonomy = '' ) {
  78. if ( isset( $_POST['seconddesc'] ) && 'product_cat' === $taxonomy ) {
  79. update_woocommerce_term_meta( $term_id, 'seconddesc', esc_attr( $_POST['seconddesc'] ) );
  80. }
  81. }
  82.  
  83. // ---------------
  84. // 4. Display field under products @ Product Category pages
  85.  
  86. add_action( 'woocommerce_after_shop_loop', 'bbloomer_display_wp_editor_content', 5 );
  87.  
  88. function bbloomer_display_wp_editor_content() {
  89. if ( is_product_taxonomy() ) {
  90. $term = get_queried_object();
  91. if ( $term && ! empty( get_term_meta( $term->term_id, 'seconddesc', true ) ) ) {
  92. echo '<p class="second-term-description">' . wc_format_content( htmlspecialchars_decode( get_term_meta( $term->term_id, 'seconddesc', true ) ) ) . '</p>';
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment