Advertisement
palsushobhan

wcfm_shortcode_get_vendor_list_by_spmv_product_id

Apr 14th, 2025 (edited)
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. /* List of vendors selling a particular product (SPMV), sorted by product price ASC
  2.  *
  3.  * Shortcode: [wcfm_vendors_card_by_product id="123"]
  4.  *
  5.  * @param array $attr Shortcode attributes.
  6.  * @return string HTML output of vendor cards.
  7.  */
  8. add_shortcode('wcfm_vendors_card_by_product', function ($attr) {
  9.     global $wpdb, $WCFMmp;
  10.  
  11.     $defaults = array(
  12.         'id' => 0,
  13.     );
  14.     $atts = shortcode_atts($defaults, $attr);
  15.  
  16.     $product_id = absint(filter_input(INPUT_GET, 'id'));
  17.     if (!$product_id && $atts['id'] && absint($atts['id'])) {
  18.         $product_id = absint($atts['id']);
  19.     }
  20.     if (!$product_id) {
  21.         return '';
  22.     }
  23.  
  24.     $product = wc_get_product($product_id);
  25.     if (!$product) {
  26.         return '';
  27.     }
  28.  
  29.     $vendor_id_single = wcfm_get_vendor_id_by_post($product_id);
  30.     $vendor_list = [];
  31.     $is_multi_vendor_enabled = apply_filters('wcfm_is_allow_product_multivendor_title_edit_disable', true);
  32.  
  33.     if (!$is_multi_vendor_enabled) {
  34.         if(wcfm_is_vendor($vendor_id_single)) {
  35.             $vendor_list[] = $vendor_id_single;
  36.         }
  37.     } else {
  38.         $multi_parent_id = get_post_meta($product_id, '_is_multi_parent', true) ?: get_post_meta($product_id, '_has_multi_selling', true);
  39.  
  40.         if ($multi_parent_id) {
  41.             $sql = $wpdb->prepare(
  42.                 "SELECT GROUP_CONCAT(product_id) as products
  43.                 FROM `{$wpdb->prefix}wcfm_marketplace_product_multivendor`
  44.                 WHERE `parent_product_id` = %d",
  45.                 $multi_parent_id
  46.             );
  47.             $results = $wpdb->get_row($sql);
  48.             $product_list = isset($results->products) ? explode(',', $results->products) : array();
  49.  
  50.             if (!in_array($multi_parent_id, $product_list)) {
  51.                 array_unshift($product_list, $multi_parent_id);
  52.             }
  53.  
  54.             $product_ids = implode(',', $product_list);
  55.             $sql = "SELECT product_id, stock_status, stock_quantity
  56.                     FROM {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup
  57.                     WHERE product_id IN (%1s)
  58.                     ORDER BY wc_product_meta_lookup.min_price ASC";
  59.             $prepare = $wpdb->prepare($sql, $product_ids);
  60.             $product_metas = $wpdb->get_results($prepare);
  61.  
  62.             if (!empty($product_metas)) {
  63.                 foreach ($product_metas as $product_meta) {
  64.                     if ($product_meta->stock_status === 'outofstock') {
  65.                         continue;
  66.                     }
  67.                     $post_status = get_post_status($product_meta->product_id);
  68.                     if ($post_status !== 'publish') {
  69.                         continue;
  70.                     }
  71.                     $vendor_id = wcfm_get_vendor_id_by_post($product_meta->product_id);
  72.                     if (wcfm_is_vendor($vendor_id) && !in_array($vendor_id, $vendor_list)) {
  73.                         $vendor_list[] = $vendor_id;
  74.                     }
  75.                 }
  76.             }
  77.         } else {
  78.             if (wcfm_is_vendor($vendor_id_single) && !in_array($vendor_id_single, $vendor_list)) {
  79.                 $vendor_list[] = $vendor_id_single;
  80.             }
  81.         }
  82.     }
  83.  
  84.     if (empty($vendor_list)) {
  85.         return '';
  86.     }
  87.  
  88.     ob_start();
  89.     ?>
  90.     <div id="wcfmmp-stores-wrap" class="top-vendor-container">
  91.         <div class="wcfmmp-stores-content">
  92.             <ul class="top-vendor-list wcfmmp-store-wrap">
  93.                 <?php
  94.                 $args = array('per_row' => 3);
  95.                 foreach ($vendor_list as $vendor_id) {
  96.                     $args['store_id'] = $vendor_id;
  97.                     $WCFMmp->template->get_template('store-lists/wcfmmp-view-store-lists-card.php', $args);
  98.                 }
  99.                 ?>
  100.             </ul>
  101.         </div>
  102.     </div>
  103.     <?php
  104.     $list = ob_get_contents();
  105.     ob_end_clean();
  106.     return $list;
  107. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement