Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.66 KB | None | 0 0
  1. <?php
  2.  
  3. require_once dirname( __FILE__ ) . '/quote_print.php';
  4.  
  5. function p52_quote_post_type() {
  6.     register_post_type( 'p52-quote',
  7.         array(
  8.             'labels' => array(
  9.                 'name' => _x('Quote', 'post type general name'),
  10.                 'singular_name' => _x('Quote', 'post type singular name'),
  11.                 'add_new' => _x('Add New', 'quote'),
  12.                 'add_new_item' => __('Add New Quote'),
  13.                 'edit_item' => __('Edit Quote'),
  14.                 'new_item' => __('New Quote'),
  15.                 'all_items' => __('All Quotes'),
  16.                 'view_item' => __('View Quote'),
  17.                 'search_items' => __('Search Quotes'),
  18.                 'not_found' =>  __('Quote wasn\'t found'),
  19.                 'not_found_in_trash' => __('Quote wasn\'t found in Trash'),
  20.                 'parent_item_colon' => '',
  21.                 'menu_name' => 'P52 Quotes'            
  22.             ),
  23.             'public' => true,
  24.             'publicly_queryable' => true,
  25.             'show_in_menu' => true,
  26.             'query_var' => true,
  27.             'supports' => array( 'title' ),
  28.             'capability_type' => 'post',
  29.             'rewrite' => true,
  30.             'menu_position' => 5,
  31.             'hierarchical' => false,
  32.             'menu_icon' => 'dashicons-admin-links',
  33.         )
  34.     );
  35. }
  36. add_action( 'init', 'p52_quote_post_type' );
  37.  
  38. /***************************
  39. * create contact metaboxes
  40. ***************************/
  41. function p52_register_quote_contact_meta_boxes( $meta_boxes ) {
  42.     $prefix = 'p52_quote_';
  43.    
  44.     // Get all customer custom post type posts
  45.     $customers = get_posts( array(
  46.         'posts_per_page' => -1,
  47.         'post_type' => 'p52-contact'
  48.     ) );
  49.    
  50.     // start customer
  51.     $meta_boxes[] = array(
  52.         'id'       => $prefix . 'customer',
  53.         'title'    => 'Customer Info',
  54.         'pages'    => 'p52-quote',
  55.         'context'  => 'normal',
  56.         'priority' => 'low',
  57.        
  58.  
  59.         'fields' => array(            
  60.        
  61.             array(
  62.                 'name'  => 'Name',
  63.                 'id'    => $prefix . 'name',
  64.                 'type'  => 'post',
  65.                 'post_type' => 'p52-contact',
  66.                 'placeholder' => 'Select a Customer',
  67.                 'columns' => 6
  68.             ),
  69.        
  70.            
  71.             array(
  72.                 'name'  => 'Company',
  73.                 'id'    => $prefix . 'company',
  74.                 'type'  => 'text',
  75.                 'columns' => 6
  76.             ),
  77.            
  78.             array(
  79.                 'name'  => 'Email',
  80.                 'id'    => $prefix . 'email',
  81.                 'type'  => 'text',
  82.                 'columns' => 6
  83.             ),
  84.            
  85.             array(
  86.                 'name'  => 'Phone',
  87.                 'id'    => $prefix . 'phone',
  88.                 'type'  => 'hidden',
  89.                 'columns' => 6
  90.             ),
  91.            
  92.             array(
  93.                 'name'  => 'Address',
  94.                 'id'    => $prefix . 'address',
  95.                 'type'  => 'textarea',
  96.                 'columns' => 6
  97.             ),
  98.            
  99.             array(
  100.                 'name'  => 'Delivery Address',
  101.                 'id'    => $prefix . 'delivery_address',
  102.                 'type'  => 'textarea',
  103.                 'columns' => 6
  104.             ),
  105.            
  106.                        
  107.         ), // end customer fields
  108.     ); // end customer
  109.    
  110.     // Append phone or another field of customer to meta box above.
  111.     foreach ( $customers as $customer )
  112.     {
  113.         // Show this phone field if match name.
  114.         $meta_box['fields'][] = array(
  115.             'name'      => 'Phone',
  116.             'id'        => $prefix . 'phone_' . $customer->ID,
  117.             'type'      => 'text',
  118.             'std'       => get_post_meta( $customer->ID, 'p52_quote_phone', true ),
  119.             'visible'   => array( $prefix . 'name', $customer->ID )
  120.         );
  121.     }
  122.     $meta_boxes[] = $meta_box;
  123.  
  124.     return $meta_boxes;
  125. }
  126.  
  127. add_filter( 'rwmb_meta_boxes', 'p52_register_quote_contact_meta_boxes' );
  128.  
  129. // Save data to phone field
  130. add_action('rwmb_before_save_post', function($post_id)
  131. {
  132.     // Get person ID to save from "Select a Customer" field
  133.     $person_to_save = intval( $_POST['p52_quote_name'] );
  134.     // Save related field to phone field
  135.     $_POST['p52_quote_phone'] = $_POST['p52_quote_phone_' . $person_to_save];
  136.     // Unset all hidden fields
  137.     foreach ( $_POST as $key => $value )
  138.     {
  139.         if ( strpos( $key, 'p52_quote_phone_' ) )
  140.             unset( $_POST[$key] );
  141.     }
  142. } );
  143.  
  144. /***************************
  145. * create product metaboxes
  146. ***************************/
  147. function p52_register_quote_product_meta_boxes( $meta_boxes ) {
  148.     $prefix = 'p52_quote_';
  149.  
  150.     // start products
  151.     $meta_boxes[] = array(
  152.         'id'       => $prefix . 'products',
  153.         'title'    => 'Product Info',
  154.         'pages'    => 'p52-quote',
  155.         'context'  => 'normal',
  156.         'priority' => 'low',
  157.        
  158.  
  159.         'fields' => array(
  160.            
  161.             array(
  162.                 'id' => 'quote-products',
  163.                 //'name' => 'Products',
  164.                 'type' => 'Group',
  165.                 'clone' => true,
  166.                 'columns' => 12,
  167.        
  168.         'fields' => array(
  169.                        
  170.             array(
  171.                 'name'  => 'Product',
  172.                 'id'    => $prefix . 'product',
  173.                 'type'  => 'post',
  174.                 'post_type' => 'p52-product',
  175.                 'placeholder' => 'Select a Product',
  176.                 'columns' => 4
  177.             ),
  178.            
  179.             array(
  180.                 'name'  => 'Description',
  181.                 'id'    => $prefix . 'description',
  182.                 'type'  => 'textarea',
  183.                 'columns' => 4
  184.             ),
  185.            
  186.              array(
  187.                 'name'  => 'Qty',
  188.                 'id'    => $prefix . 'qty',
  189.                 'type'  => 'number',
  190.                 'step' => .01,
  191.                 'columns' => 2
  192.             ),
  193.            
  194.             array(
  195.                 'name'  => 'Price',
  196.                 'id'    => $prefix . 'price',
  197.                 'type'  => 'number',
  198.                 'step' => .01,
  199.                 'columns' => 2
  200.             ),
  201.                        
  202.             ), // end product fields
  203.                
  204.         ), // end group fieds
  205.            
  206.     ), // end all fields
  207.  
  208.  ); // end products
  209.  
  210.     return $meta_boxes;
  211. }
  212.  
  213. add_filter( 'rwmb_meta_boxes', 'p52_register_quote_product_meta_boxes' );
  214.  
  215. /***************************
  216. * create contact metaboxes
  217. ***************************/
  218. function p52_register_quote_notes_meta_boxes( $meta_boxes ) {
  219.     $prefix = 'p52_quote_';
  220.        
  221.     // start notes
  222.     $meta_boxes[] = array(
  223.         'id'       => $prefix . 'quote_notes',
  224.         'title'    => 'Notes',
  225.         'pages'    => 'p52-quote',
  226.         'context'  => 'normal',
  227.         'priority' => 'low',
  228.        
  229.  
  230.         'fields' => array(            
  231.        
  232.              array(
  233.                 'name'  => 'Notes',
  234.                 'id'    => $prefix . 'notes',
  235.                  'std' => 'Quote valid for 30 days from date of issue',
  236.                 'type'  => 'textarea',
  237.                 'columns' => 12,
  238.             ),
  239.            
  240.                        
  241.         ), // end your fields
  242.     ); // end your info
  243.  
  244.     return $meta_boxes;
  245. }
  246.  
  247. add_filter( 'rwmb_meta_boxes', 'p52_register_quote_notes_meta_boxes' );
  248.  
  249. function build_table($products){
  250. $yourcurrency = quote_currency_info();
  251.     // data rows
  252.     foreach( $products as $key=>$value){
  253.  
  254.         $html .= '<tr class="item-row">';
  255.  
  256.             $html .= '<td>' . get_the_title($value[p52_quote_product]) . '</td>';
  257.             $html .= '<td>' . $value[p52_quote_description] . '</td>';
  258.             $html .= '<td>' . $value[p52_quote_qty] . '</td>';
  259.             $html .= '<td>' . $yourcurrency . '&nbsp;' . $value[p52_quote_price] . '</td>';
  260.             $html .= '<td>' . $yourcurrency . '&nbsp;' . $value[p52_quote_qty] * $value[p52_quote_price] . '</td>';
  261.  
  262.         $html .= '</tr>';
  263.        
  264.  
  265.     }
  266.  
  267.     return $html;
  268. }
  269.  
  270. function get_price_total($products){
  271.    
  272.     $total=0;
  273.     foreach( $products as $key=>$value){
  274.         $total+=$value[p52_quote_qty] * $value[p52_quote_price];
  275.     }
  276.     return $total;
  277.  
  278. }`
  279.  
  280.  
  281. And this is the full code for my contact
  282.  
  283. `<?php
  284.  
  285. /***************************
  286. * create contact cpt
  287. ***************************/
  288. function p52_contact_post_type() {
  289.     register_post_type( 'p52-contact',
  290.         array(
  291.             'labels' => array(
  292.                 'name' => _x('Contact', 'post type general name'),
  293.                 'singular_name' => _x('Contact', 'post type singular name'),
  294.                 'add_new' => _x('Add New', 'Contact'),
  295.                 'add_new_item' => __('Add New Contact'),
  296.                 'edit_item' => __('Edit Contact'),
  297.                 'new_item' => __('New Contact'),
  298.                 'all_items' => __('All Contacts'),
  299.                 'view_item' => __('View Contacts'),
  300.                 'search_items' => __('Search Contacts'),
  301.                 'not_found' =>  __('Contact wasn\'t found'),
  302.                 'not_found_in_trash' => __('Contact wasn\'t found in Trash'),
  303.                 'parent_item_colon' => '',
  304.                 'menu_name' => 'P52 Contacts'              
  305.             ),
  306.             'public' => true,
  307.             'publicly_queryable' => true,
  308.             'show_in_menu' => true,
  309.             'query_var' => true,
  310.             'supports' => array( 'title' ),
  311.             'capability_type' => 'post',
  312.             'rewrite' => true,
  313.             'menu_position' => 5,
  314.             'hierarchical' => false,
  315.             'menu_icon' => 'dashicons-admin-users',
  316.         )
  317.     );
  318. }
  319. add_action( 'init', 'p52_contact_post_type' );
  320.  
  321. /***************************
  322. * create contact metaboxes
  323. ***************************/
  324. function p52_contact_before_meta_boxes( $meta_boxes ) {
  325.     $prefix = 'p52_contact_';
  326.  
  327.    
  328.     // contact info meta box
  329.     $meta_boxes[] = array(
  330.         'id'       => $prefix . 'contact',
  331.         'title'    => 'Contact Info',
  332.         'pages'    => array('p52-contact'),
  333.         'context'  => 'normal',
  334.         'priority' => 'high',
  335.  
  336.         'fields' => array(            
  337.        
  338.             array(
  339.                 'name'  => 'Company',
  340.                 'id'    => $prefix . 'company',
  341.                 'type'  => 'text',
  342.                 'columns' => 6
  343.             ),
  344.            
  345.             array(
  346.                 'name'  => 'Relationship',
  347.                 'id'    => $prefix . 'relationship',
  348.                 'type'  => 'select',
  349.                 'options' => array(
  350.                     'prospect' => 'Prospect',
  351.                     'lead' => 'Lead',
  352.                     'client' => 'Client',
  353.                     'model' => 'Model',
  354.                     'assitant' => 'Assistant',
  355.                     'supplier' => 'Supplier'
  356.                     ),
  357.                 'placeholder' => 'Choose your relationship',
  358.                 'columns' => 6
  359.             ),
  360.            
  361.             array(
  362.                 'name'  => 'Phone',
  363.                 'id'    => $prefix . 'phone',
  364.                 'type'  => 'text',
  365.                 'columns' => 6
  366.             ),
  367.            
  368.             array(
  369.                 'name'  => 'Email',
  370.                 'id'    => $prefix . 'email',
  371.                 'type'  => 'text',
  372.                 'columns' => 6
  373.             ),
  374.            
  375.             array(
  376.                 'name'  => 'Address',
  377.                 'id'    => $prefix . 'address',
  378.                 'type'  => 'textarea',
  379.                 'columns' => 6
  380.             ),
  381.            
  382.             array(
  383.                 'name'  => 'Postal Address',
  384.                 'id'    => $prefix . 'postal',
  385.                 'type'  => 'textarea',
  386.                 'columns' => 6
  387.             ),
  388.            
  389.                        
  390.         )
  391.     );
  392.    
  393.    
  394.                     // contact tasks meta box
  395.                     $meta_boxes[] = array(
  396.                         'id'       => $prefix . 'tasks',
  397.                         'title'    => 'Tasks',
  398.                         'pages'    => array('p52-contact'),
  399.                         'context'  => 'normal',
  400.                         'priority' => 'high',
  401.  
  402.                         'fields' => array(
  403.                            
  404.                                 array(
  405.                                     'id' => $prefix .  'contact_tasks',                                    
  406.                                     'type'  => 'group',
  407.                                     'clone' => true,
  408.                                    
  409.                                 'fields' => array(
  410.                                    
  411.                                     array(
  412.                                         'name'  => 'Task Name',
  413.                                         'id'    => $prefix . 'task_name',
  414.                                         'type'  => 'text',
  415.                                         'columns' => 6
  416.                                     ),
  417.  
  418.                                     array(
  419.                                         'name'  => 'Task Style',
  420.                                         'id'    => $prefix . 'task_style',
  421.                                         'type'  => 'select',
  422.                                         'options' => array(
  423.                                             'task_email' => 'Email',
  424.                                             'task_phone' => 'Phone',
  425.                                             'task_meeting' => 'Meeting',
  426.                                             'task_skype' => 'Skype',
  427.                                             'task_mockup' => 'Mockup',
  428.                                             'task_wireframe' => 'Wireframe',
  429.                                             'task_design' => 'Design',
  430.                                             'task_photography' => 'Photography',
  431.                                             'task_production' => 'Production',
  432.                                         ),
  433.                                         'placeholder' => 'Select a style',
  434.                                         'columns' => 6
  435.                                     ),
  436.  
  437.                                     array(
  438.                                         'name'  => 'Due Date',
  439.                                         'id'    => $prefix . 'task_due',
  440.                                         'type'  => 'datetime',
  441.                                         'columns' => 4
  442.                                     ),
  443.  
  444.                                     array(
  445.                                         'name'  => 'Status',
  446.                                         'id'    => $prefix . 'task_status',
  447.                                         'type'  => 'select',
  448.                                         'options' => array(
  449.                                             'task_not-started' => 'Not Started',
  450.                                             'task_in_progress' => 'In Progess',
  451.                                             'task_waiting_customer' => 'Waiting Customer',
  452.                                             'task_completed' => 'Completed',
  453.                                         ),
  454.                                         'placeholder' => 'Select a status',
  455.                                         'columns' => 4
  456.                                     ),
  457.  
  458.                                     array(
  459.                                         'name'  => 'Date Complete',
  460.                                         'id'    => $prefix . 'task_date_completed',
  461.                                         'type'  => 'datetime',
  462.                                         'columns' => 4
  463.                                     ),
  464.                            
  465.                                     array(
  466.                                         'name'  => 'Task Description',
  467.                                         'id'    => $prefix . 'task_description',
  468.                                         'type'  => 'wysiwyg',
  469.                                     ),
  470.                             ),// end task fields
  471.                         ),
  472.                     ),
  473.                 ); // end tasks
  474.    
  475.    
  476.     // contact social meta box
  477.                     $meta_boxes[] = array(
  478.                         'id'       => $prefix . 'social',
  479.                         'title'    => 'Social Links',
  480.                         'pages'    => array('p52-contact'),
  481.                         'context'  => 'side',
  482.  
  483.                         'fields' => array(
  484.                            
  485.                                 array(
  486.                                     'id' => $prefix .  'contact_social',                                    
  487.                                     'type'  => 'group',
  488.                                     'clone' => true,
  489.                                    
  490.                                 'fields' => array(
  491.                                    
  492.                                     array(
  493.                                         'name'  => 'Social Media',
  494.                                         'id'    => $prefix . 'social_type',
  495.                                         'type'  => 'select',
  496.                                         'options' => array(
  497.                                             'facebook' => 'Facebook',
  498.                                             'twitter' => 'Twitter',
  499.                                             'google' => 'Google+',
  500.                                             'pinterest' => 'Pinterest',
  501.                                             'instagram' => 'Instagram',
  502.                                             'linkedin' => 'LinkedIn',
  503.                                             'website' => 'Website',
  504.                                             '500px' => '500px',
  505.                                             'behance' => 'Behance',
  506.                                         ),
  507.                                         'placeholder' => 'Select the Social Media type',
  508.                                     ),
  509.  
  510.                                     array(
  511.                                         'name'  => 'Link',
  512.                                         'id'    => $prefix . 'social_link',
  513.                                         'type'  => 'text',
  514.                                         'placeholder' => 'Enter the URL',
  515.                                     ),
  516.                                    
  517.                             ),// end task fields
  518.                         ),
  519.                     ),
  520.                 ); // end tasks
  521.  
  522.     return $meta_boxes;
  523. }
  524.  
  525. add_filter( 'rwmb_meta_boxes', 'p52_contact_before_meta_boxes' );`
  526.  
  527.  
  528. And then finally my products
  529.  
  530. `<?php
  531.  
  532. /***************************
  533. * create product cpt
  534. ***************************/
  535. function p52_product_post_type() {
  536.     register_post_type( 'p52-product',
  537.         array(
  538.             'labels' => array(
  539.                 'name' => _x('Product', 'post type general name'),
  540.                 'singular_name' => _x('Product', 'post type singular name'),
  541.                 'add_new' => _x('Add New', 'Product'),
  542.                 'add_new_item' => __('Add New Product'),
  543.                 'edit_item' => __('Edit Product'),
  544.                 'new_item' => __('New Product'),
  545.                 'all_items' => __('All Products'),
  546.                 'view_item' => __('View Products'),
  547.                 'search_items' => __('Search Products'),
  548.                 'not_found' =>  __('Product wasn\'t found'),
  549.                 'not_found_in_trash' => __('Product wasn\'t found in Trash'),
  550.                 'parent_item_colon' => '',
  551.                 'menu_name' => 'P52 Products'              
  552.             ),
  553.             'public' => true,
  554.             'publicly_queryable' => true,
  555.             'show_in_menu' => true,
  556.             'query_var' => true,
  557.             'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
  558.             'capability_type' => 'post',
  559.             'rewrite' => true,
  560.             'menu_position' => 5,
  561.             'hierarchical' => false,
  562.             'menu_icon' => 'dashicons-cart',
  563.         )
  564.     );
  565. }
  566. add_action( 'init', 'p52_product_post_type' );
  567.  
  568. /***************************
  569. * create product metaboxes
  570. ***************************/
  571. function p52_register_product_meta_boxes( $meta_boxes ) {
  572.     $prefix = 'p52_product_';
  573.  
  574.    
  575.     // b&a meta box
  576.     $meta_boxes[] = array(
  577.         'id'       => 'product_info',
  578.         'title'    => 'Product Info',
  579.         'pages'    => array('p52-product'),
  580.         'context'  => 'side',
  581.  
  582.         'fields' => array(            
  583.            
  584.              array(
  585.                 'name'  => 'Cost',
  586.                 'id'    => $prefix . 'cost',
  587.                 'type'  => 'number',
  588.             ),
  589.            
  590.             array(
  591.                 'name'  => 'RRP',
  592.                 'id'    => $prefix . 'rrp',
  593.                 'type'  => 'number',
  594.             ),
  595.            
  596.             array(
  597.                 'name'  => 'Unit',
  598.                 'id'    => $prefix . 'unit',
  599.                 'type'  => 'select',
  600.                 'options' => array(
  601.                     'ea' => 'Each',
  602.                     'hr' => 'Hourly',
  603.                     'day' => 'Daily',
  604.                     'wk' => 'Weekly',
  605.                     'mth' => 'Month',
  606.                     'yr' => 'Yearly'
  607.                 ),
  608.                 'placeholder' => 'Select your unit of sale',
  609.             ),
  610.            
  611.             array(
  612.                 'name'  => 'Type',
  613.                 'id'    => $prefix . 'type',
  614.                 'type'  => 'select',
  615.                 'options' => array(
  616.                     'product' => 'Product',
  617.                     'service' => 'Service',
  618.                     'subscription' => 'Subscription',
  619.                     'support' => 'Support',
  620.                     'license' => 'License',
  621.                 ),  
  622.                 'placeholder' => 'Select your product type',
  623.             ),
  624.         ),
  625.     );
  626.  
  627.     return $meta_boxes;
  628. }
  629.  
  630. add_filter( 'rwmb_meta_boxes', 'p52_register_product_meta_boxes' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement