Advertisement
palsushobhan

wcfm product categories of store - shortcode

Mar 17th, 2025
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. add_shortcode('wcfm_store_categories', function($attr) {
  2.     global $wpdb, $post;
  3.        
  4.     $store_id = '';
  5.     if ( isset( $attr['id'] ) && !empty( $attr['id'] ) ) { $store_id = absint($attr['id']); }
  6.    
  7.     if ( !$store_id && wcfm_is_store_page() ) {
  8.         $wcfm_store_url = wcfm_get_option( 'wcfm_store_url', 'store' );
  9.         $store_name = apply_filters( 'wcfmmp_store_query_var', get_query_var( $wcfm_store_url ) );
  10.         $store_id  = 0;
  11.         if ( !empty( $store_name ) ) {
  12.             $store_user = get_user_by( 'slug', $store_name );
  13.         }
  14.         $store_id           = $store_user->ID;
  15.     }
  16.    
  17.     if( !$store_id && is_product() ) {
  18.         $store_id = $post->post_author;
  19.     }
  20.    
  21.     if( !$store_id && is_single() && $post && is_object( $post ) && wcfm_is_vendor( $post->post_author ) ) {
  22.         $store_id = $post->post_author;
  23.     }
  24.    
  25.     if( !$store_id ) return;
  26.    
  27.     $sql = $wpdb->prepare("SELECT `name` FROM {$wpdb->prefix}wcfm_store_categories AS cat
  28.        INNER JOIN {$wpdb->prefix}wcfm_store_categories_mapping AS map ON cat.ID = map.store_category_id
  29.        WHERE `is_active` = %d and `vendor_id` = %d", 1, $store_id);
  30.    
  31.     $store_categories = $wpdb->get_col($sql);
  32.     if( !empty( $store_categories ) ) {
  33.         return '<p class="store-phone"><i class="wcfmfa fa-layer-group" aria-hidden="true"></i> ' . implode(', ', $store_categories) . '</p>';
  34.     } else {
  35.         return '<p>No categories found for this store.</p>';
  36.     }
  37. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement