Skip to content

Instantly share code, notes, and snippets.

@rwkyyy
Created March 29, 2022 14:05
Show Gist options
  • Save rwkyyy/5da7d549393463397c4a2074b816f3d2 to your computer and use it in GitHub Desktop.
Save rwkyyy/5da7d549393463397c4a2074b816f3d2 to your computer and use it in GitHub Desktop.
Enable Catalog Visibility in WooCommerce Product Editor (Gutenberg)
// fix catalog visibility
// original author + credits to https://dev.to/kalimahapps/enable-gutenberg-editor-in-woocommerce-466m
function enable_taxonomy_rest( $args ) {
$args['show_in_rest'] = true;
return $args;
}
add_filter( 'woocommerce_taxonomy_args_product_tag', 'enable_taxonomy_rest' );
function register_catalog_meta_boxes() {
global $current_screen;
// Make sure gutenberg is loaded before adding the metabox
if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
add_meta_box( 'catalog-visibility', __( 'Catalog visibility', 'textdomain' ), 'product_data_visibility', 'product', 'side' );
}
}
add_action( 'add_meta_boxes', 'register_catalog_meta_boxes' );
function product_data_visibility( $post ) {
$thepostid = $post->ID;
$product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product();
$current_visibility = $product_object->get_catalog_visibility();
$current_featured = wc_bool_to_string( $product_object->get_featured() );
$visibility_options = wc_get_product_visibility_options();
?>
<div class="misc-pub-section" id="catalog-visibility">
<?php esc_html_e( 'Catalog visibility:', 'woocommerce' ); ?>
<strong id="catalog-visibility-display">
<?php
echo isset( $visibility_options[ $current_visibility ] ) ? esc_html( $visibility_options[ $current_visibility ] ) : esc_html( $current_visibility );
if ( 'yes' === $current_featured ) {
echo ', ' . esc_html__( 'Featured', 'woocommerce' );
}
?>
</strong>
<a href="#catalog-visibility"
class="edit-catalog-visibility hide-if-no-js"><?php esc_html_e( 'Edit', 'woocommerce' ); ?></a>
<div id="catalog-visibility-select" class="hide-if-js">
<input type="hidden" name="current_visibility" id="current_visibility"
value="<?php echo esc_attr( $current_visibility ); ?>"/>
<input type="hidden" name="current_featured" id="current_featured"
value="<?php echo esc_attr( $current_featured ); ?>"/>
<?php
echo '<p>' . esc_html__( 'This setting determines which shop pages products will be listed on.', 'woocommerce' ) . '</p>';
foreach ( $visibility_options as $name => $label ) {
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 />';
}
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 />';
?>
<p>
<a href="#catalog-visibility"
class="save-post-visibility hide-if-no-js button"><?php esc_html_e( 'OK', 'woocommerce' ); ?></a>
<a href="#catalog-visibility"
class="cancel-post-visibility hide-if-no-js"><?php esc_html_e( 'Cancel', 'woocommerce' ); ?></a>
</p>
</div>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment