Advertisement
michaellevelup

Metabox for Catalog visibility in Product editor

Jul 7th, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | None | 0 0
  1. /* Activate products visibility in Gutenberg */
  2. function king_register_catalog_meta_boxes() {
  3. global $current_screen;
  4. if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
  5. add_meta_box( 'catalog-visibility', __( 'Catalog visibility', 'textdomain' ), 'product_data_visibility', 'product', 'side' );
  6. }
  7. }
  8. add_action( 'add_meta_boxes', 'king_register_catalog_meta_boxes' );
  9. function product_data_visibility( $post ) {
  10. $thepostid = $post->ID;
  11. $product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product();
  12. $current_visibility = $product_object->get_catalog_visibility();
  13. $current_featured = wc_bool_to_string( $product_object->get_featured() );
  14. $visibility_options = wc_get_product_visibility_options();
  15. ?>
  16. <div class="misc-pub-section" id="catalog-visibility">
  17. <?php esc_html_e( 'Catalog visibility:', 'woocommerce' ); ?>
  18. <strong id="catalog-visibility-display">
  19. <?php
  20. echo isset( $visibility_options[ $current_visibility ] ) ? esc_html( $visibility_options[ $current_visibility ] ) : esc_html( $current_visibility );
  21. if ( 'yes' === $current_featured ) {
  22. echo ', ' . esc_html__( 'Featured', 'woocommerce' );
  23. }
  24. ?>
  25. </strong>
  26. <a href="#catalog-visibility"
  27. class="edit-catalog-visibility hide-if-no-js"><?php esc_html_e( 'Edit', 'woocommerce' ); ?></a>
  28. <div id="catalog-visibility-select" class="hide-if-js">
  29. <input type="hidden" name="current_visibility" id="current_visibility"
  30. value="<?php echo esc_attr( $current_visibility ); ?>" />
  31. <input type="hidden" name="current_featured" id="current_featured"
  32. value="<?php echo esc_attr( $current_featured ); ?>" />
  33. <?php
  34. echo '<p>' . esc_html__( 'This determines in which pages of the online store the products will be seen.', 'woocommerce' ) . '</p>';
  35. foreach ( $visibility_options as $name => $label ) {
  36. echo '<input type="radio" name="_visibility" id="_visibility_' . esc_attr( $name ) . '" value="' . esc_attr( $name ) . '" ' . checked( $current_visibility, $name, false ) . ' data-label="' . esc_attr( $label ) . '" /> <label for="_visibility_' . esc_attr( $name ) . '" class="selectit">' . esc_html( $label ) . '</label><br />';
  37. }
  38. echo '<br /><input type="checkbox" name="_featured" id="_featured" ' . checked( $current_featured, 'yes', false ) . ' /> <label for="_featured">' . esc_html__( 'This is a featured product', 'woocommerce' ) . '</label><br />';
  39. ?>
  40. <p>
  41. <a href="#catalog-visibility"
  42. class="save-post-visibility hide-if-no-js button"><?php esc_html_e( 'Accept', 'woocommerce' ); ?></a>
  43. <a href="#catalog-visibility"
  44. class="cancel-post-visibility hide-if-no-js"><?php esc_html_e( 'Cancel', 'woocommerce' ); ?></a>
  45. </p>
  46. </div>
  47. </div>
  48. <?php
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement