Advertisement
longnguyenwp

Field with loop - pstidsen

Oct 7th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. add_filter( 'rwmb_meta_boxes', 'add_image_boxes' );
  2. function add_image_boxes($meta_boxes){
  3.     $post_id = false;
  4.     if (isset($_GET['post'])){
  5.         $post_id = intval($_GET['post']);
  6.     } else{
  7.         // return; ------- here
  8.     }
  9.    
  10.     if ($post_id){
  11.         $product = wc_get_product($post_id);
  12.     }
  13.    
  14.     // if (is_a($product, "WC_Product")){ ------- here
  15.         $args = array(
  16.             'taxonomy'      => 'pa_color',
  17.             'object_ids'    => $post_id,
  18.         );
  19.         $colors = get_terms($args);
  20.        
  21.         $fields = array();
  22.         foreach($colors as $color){
  23.             $fields[] = [
  24.                 'type'       => 'image_advanced',
  25.                 'max_status' => false,
  26.                 'name'       => "Add images of " . $color->name,
  27.                 'id'         => "method1" . $color->slug,
  28.             ];
  29.         }
  30.        
  31.         $meta_boxes[] = [
  32.                 'title'      => 'Add images of the different colors',
  33.                 'id'         => 'add_images_of_the_different_colors',
  34.                 'post_types' => ['product'],
  35.                 'context'    => 'normal',
  36.                 'priority'   => 'high',
  37.                 'fields'     => $fields,
  38.             ];
  39.     // } ------- here
  40.  
  41.     foreach($colors as $color){
  42.         $meta_boxes[] = [
  43.             'title'      => "Add images of " . $color->name,
  44.             'id'         => 'method2' . $color->slug,
  45.             'post_types' => ['product'],
  46.             'context'    => 'normal',
  47.             'priority'   => 'high',
  48.             'fields'     => [
  49.                 [
  50.                     'type'       => 'image_advanced',
  51.                     'id'         => 'method2' . $color->slug,
  52.                     'name'       => 'Add images',
  53.                     'max_status' => false,
  54.                 ],
  55.             ],
  56.         ];
  57.     }
  58.    
  59.     return $meta_boxes;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement