Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.78 KB | None | 0 0
  1. /***************************
  2. * create contact metaboxes
  3. ***************************/
  4. function p52_register_quote_contact_meta_boxes( $meta_boxes ) {
  5.     $prefix = 'p52_quote_';
  6.    
  7.     // Get all customer custom post type posts
  8.     $customers = get_posts( array(
  9.         'posts_per_page' => -1,
  10.         'post_type' => 'p52-contact'
  11.     ) );
  12.    
  13.     // start customer
  14.     $meta_box = array(
  15.         'id'       => $prefix . 'customer',
  16.         'title'    => 'Customer Info',
  17.         'pages'    => 'p52-quote',
  18.         'context'  => 'normal',
  19.         'priority' => 'low',
  20.        
  21.  
  22.         'fields' => array(            
  23.        
  24.             array(
  25.                 'name'  => 'Name',
  26.                 'id'    => $prefix . 'name',
  27.                 'type'  => 'post',
  28.                 'post_type' => 'p52-contact',
  29.                 'placeholder' => 'Select a Customer',
  30.                 'columns' => 6
  31.             ),
  32.        
  33.            
  34.             array(
  35.                 'name'  => 'Company',
  36.                 'id'    => $prefix . 'company',
  37.                 'type'  => 'hidden',
  38.                 'columns' => 6
  39.             ),
  40.            
  41.             array(
  42.                 'name'  => 'Email',
  43.                 'id'    => $prefix . 'email',
  44.                 'type'  => 'hidden',
  45.                 'columns' => 6
  46.             ),
  47.            
  48.             array(
  49.                 'name'  => 'Phone',
  50.                 'id'    => $prefix . 'phone',
  51.                 'type'  => 'hidden',
  52.                 'columns' => 6
  53.             ),
  54.            
  55.             array(
  56.                 'name'  => 'Address',
  57.                 'id'    => $prefix . 'address',
  58.                 'type'  => 'hidden',
  59.                 'columns' => 6
  60.             ),
  61.            
  62.                        
  63.         ), // end customer fields
  64.     ); // end customer
  65.    
  66.     // Append phone or another field of customer to meta box above.
  67.         foreach ( $customers as $customer )
  68.         {
  69.             // Show this email field if match name.
  70.             $meta_box['fields'][] = array(
  71.                 'name'      => 'Email',
  72.                 'id'        => $prefix . 'email_' . $customer->ID,
  73.                 'type'      => 'text',
  74.                 'std'       => get_post_meta( $customer->ID, 'p52_contact_email', true ),
  75.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  76.                 'columns'   => 6
  77.             );
  78.            
  79.             // Show this phone field if match name.
  80.             $meta_box['fields'][] = array(
  81.                 'name'      => 'Phone',
  82.                 'id'        => $prefix . 'phone_' . $customer->ID,
  83.                 'type'      => 'text',
  84.                 'std'       => get_post_meta( $customer->ID, 'p52_contact_phone', true ),
  85.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  86.                 'columns'   => 6
  87.             );
  88.            
  89.             // Show this company field if match name.
  90.             $meta_box['fields'][] = array(
  91.                 'name'      => 'Company',
  92.                 'id'        => $prefix . 'company_' . $customer->ID,
  93.                 'type'      => 'text',
  94.                 'std'       => get_post_meta( $customer->ID, 'p52_contact_company', true ),
  95.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  96.                 'columns'   => 6
  97.             );
  98.            
  99.             // Show this address field if match name.
  100.             $meta_box['fields'][] = array(
  101.                 'name'      => 'Address',
  102.                 'id'        => $prefix . 'address_' . $customer->ID,
  103.                 'type'      => 'textarea',
  104.                 'std'       => get_post_meta( $customer->ID, 'p52_contact_address', true ),
  105.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  106.                 'columns'   => 6
  107.             );
  108.            
  109.         }
  110.    
  111.    
  112.         $meta_boxes[] = $meta_box;
  113.    
  114.     // Get all product custom post type posts
  115.     $products = get_posts( array(
  116.         'posts_per_page' => -1,
  117.         'post_type' => 'p52-product'
  118.     ) );
  119.    
  120.     // start products
  121.     $meta_box = array(
  122.         'id'       => $prefix . 'products',
  123.         'title'    => 'Product Info',
  124.         'pages'    => 'p52-quote',
  125.         'context'  => 'normal',
  126.         'priority' => 'low',
  127.        
  128.  
  129.         'fields' => array(
  130.            
  131.             array(
  132.                 'id' => 'quote-products',
  133.                 //'name' => 'Products',
  134.                 'type' => 'Group',
  135.                 'clone' => true,
  136.                 'columns' => 12,
  137.        
  138.         'fields' => array(
  139.                        
  140.             array(
  141.                 'name'  => 'Product',
  142.                 'id'    => $prefix . 'product',
  143.                 'type'  => 'post',
  144.                 'post_type' => 'p52-product',
  145.                 'placeholder' => 'Select a Product',
  146.                 'columns' => 4
  147.             ),
  148.            
  149.             array(
  150.                 'name'  => 'Description',
  151.                 'id'    => $prefix . 'description',
  152.                 'type'  => 'hidden',
  153.                 'columns' => 4
  154.             ),
  155.            
  156.              array(
  157.                 'name'  => 'Qty',
  158.                 'id'    => $prefix . 'qty',
  159.                 'type'  => 'number',
  160.                 'step' => .01,
  161.                 'columns' => 2,
  162.             ),
  163.            
  164.             array(
  165.                 'name'  => 'Price',
  166.                 'id'    => $prefix . 'price',
  167.                 'type'  => 'hidden',
  168.                 'step' => 0.01,
  169.                 'columns' => 2
  170.             ),
  171.                        
  172.             ), // end product fields
  173.                
  174.         ), // end group fieds
  175.            
  176.     ), // end all fields
  177.  
  178.  ); // end products
  179.    
  180.     // Append phone or another field of product to meta box above.
  181.         foreach ( $products as $product )
  182.         {
  183.             // Show this description field if match product.
  184.             $meta_box['fields'][] = array(
  185.                 'name'      => 'New Description',
  186.                 'id'        => $prefix . 'description_' . $product->ID,
  187.                 'type'      => 'textarea',
  188.                 'std'       => get_post_meta( $product->ID, 'p52_product_description', true ),
  189.                 'visible'   => array( $prefix . 'product', $product->ID ),
  190.                 'columns'   => 4
  191.             );
  192.            
  193.             // Show this price field if match product.
  194.             $meta_box['fields'][] = array(
  195.                 'name'      => 'New Price',
  196.                 'id'        => $prefix . 'price_' . $product->ID,
  197.                 'type'      => 'number',
  198.                 'std'       => get_post_meta( $product->ID, 'p52_product_price', true ),
  199.                 'visible'   => array( $prefix . 'product', $product->ID ),
  200.                 'step'      => 0.01,
  201.                 'columns'   => 2
  202.             );
  203.            
  204.         }
  205.    
  206.         $meta_boxes[] = $meta_box;
  207.    
  208.     // start notes
  209.     $meta_boxes[] = array(
  210.         'id'       => $prefix . 'quote_notes',
  211.         'title'    => 'Notes',
  212.         'pages'    => 'p52-quote',
  213.         'context'  => 'side',
  214.         'priority' => 'low',
  215.        
  216.  
  217.         'fields' => array(            
  218.        
  219.              array(
  220.                 'name'  => 'Notes',
  221.                 'id'    => $prefix . 'notes',
  222.                  'std' => 'Quote valid for 30 days from date of issue',
  223.                 'type'  => 'textarea',
  224.                 'columns' => 12,
  225.             ),
  226.            
  227.                        
  228.         ), // end note fields
  229.     ); // end notes
  230.  
  231.     return $meta_boxes;
  232. }
  233.  
  234. add_filter( 'rwmb_meta_boxes', 'p52_register_quote_contact_meta_boxes' );
  235.  
  236. // Save data to phone field
  237. add_action('rwmb_before_save_post', function($post_id)
  238. {
  239.     // Get person ID to save from "Select a Customer" field
  240.     $person_to_save = intval( $_POST['p52_quote_name'] );
  241.     // Save related field to phone field
  242.     $_POST['p52_quote_phone'] = $_POST['p52_quote_phone_' . $person_to_save];
  243.     // Unset all hidden fields
  244.     foreach ( $_POST as $key => $value )
  245.     {
  246.         if ( strpos( $key, 'p52_quote_phone_' ) )
  247.             unset( $_POST[$key] );
  248.     }
  249.    
  250.     // Save related field to company field
  251.     $_POST['p52_quote_company'] = $_POST['p52_quote_company_' . $person_to_save];
  252.     // Unset all hidden fields
  253.     foreach ( $_POST as $key => $value )
  254.     {
  255.         if ( strpos( $key, 'p52_quote_company_' ) )
  256.             unset( $_POST[$key] );
  257.     }
  258.    
  259.     // Save related field to email field
  260.     $_POST['p52_quote_email'] = $_POST['p52_quote_email_' . $person_to_save];
  261.     // Unset all hidden fields
  262.     foreach ( $_POST as $key => $value )
  263.     {
  264.         if ( strpos( $key, 'p52_quote_email_' ) )
  265.             unset( $_POST[$key] );
  266.     }
  267.    
  268.     // Save related field to address field
  269.     $_POST['p52_quote_address'] = $_POST['p52_quote_address_' . $person_to_save];
  270.     // Unset all hidden fields
  271.     foreach ( $_POST as $key => $value )
  272.     {
  273.         if ( strpos( $key, 'p52_quote_address_' ) )
  274.             unset( $_POST[$key] );
  275.     }
  276.    
  277.     // Save related field to delivery address field
  278.     $_POST['p52_quote_delivery_address'] = $_POST['p52_quote_delivery_address_' . $person_to_save];
  279.     // Unset all hidden fields
  280.     foreach ( $_POST as $key => $value )
  281.     {
  282.         if ( strpos( $key, 'p52_quote_delivery_address_' ) )
  283.             unset( $_POST[$key] );
  284.     }
  285.    
  286.     // Get product ID to save from "Select a Product" field
  287.     $product_to_save = intval( $_POST['p52_quote_products'] );
  288.     // Save related field to phone field
  289.     $_POST['p52_quote_description'] = $_POST['p52_quote_description_' . $product_to_save];
  290.     // Unset all hidden fields
  291.     foreach ( $_POST as $key => $value )
  292.     {
  293.         if ( strpos( $key, 'p52_quote_description_' ) )
  294.             unset( $_POST[$key] );
  295.     }
  296.    
  297.     // Save related field to price field
  298.     $_POST['p52_quote_price'] = $_POST['p52_price_' . $product_to_save];
  299.     // Unset all hidden fields
  300.     foreach ( $_POST as $key => $value )
  301.     {
  302.         if ( strpos( $key, 'p52_quote_price_' ) )
  303.             unset( $_POST[$key] );
  304.     }
  305.    
  306. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement