Advertisement
palsushobhan

rental-15.0.1-inventory-compatibility-update

Oct 31st, 2023 (edited)
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. add_filter('wcfm_product_fields_stock', function($stock_fields, $product_id, $product_type) {
  2.     global $WCFMu;
  3.     if (apply_filters('wcfm_is_allow_rental', true) && WCFMu_Dependencies::wcfm_wc_rental_pro_active_check()) {
  4.         remove_filter('wcfm_product_fields_stock', [ $WCFMu->wcfmu_integrations, 'wcfm_wcrental_product_inventory_manage' ], 80);
  5.        
  6.         $args = array(
  7.             'post_type'      => 'Inventory',
  8.             'posts_per_page' => -1,
  9.             'orderby'        => 'date',
  10.             'order'          => 'ASC',
  11.             'post_status'    => 'publish'
  12.         );
  13.    
  14.         $inventories = get_posts($args);
  15.  
  16.         $formatted_inventories = [];
  17.         if(!empty($inventories)) {
  18.             foreach ($inventories as $key => $inventory) {
  19.                 $formatted_inventories[$inventory->ID] = $inventory->post_title;
  20.             }
  21.             wp_reset_postdata();
  22.         }
  23.  
  24.         $selected_inventories = rnb_get_product_inventory_id($product_id);
  25.        
  26.         $inventory_elements = apply_filters(
  27.             'wcfm_redq_rental_fields_inventory',
  28.             [
  29.                 '_redq_product_inventory' => [
  30.                     'label'       => __('Choose Inventories', 'redq-rental'),
  31.                     'type'        => 'select',
  32.                     'attributes'  => array( 'multiple' => 'multiple', 'style' => 'width: 60%;' ),
  33.                     'class'       => 'wcfm-select wcfm_ele redq_rental',
  34.                     'label_class' => 'wcfm_title wcfm_ele redq_rental',
  35.                     'value'       => !empty($selected_inventories) && is_array($selected_inventories) ? $selected_inventories : [],
  36.                     'options'     => $formatted_inventories,
  37.                 ],
  38.             ]
  39.         );
  40.  
  41.         $stock_fields = array_merge($stock_fields, $inventory_elements);
  42.  
  43.         return $stock_fields;
  44.     }
  45. }, 79, 3);
  46. add_action('wcfm_products_manage_inventory_end', function() {
  47. ?>
  48. <script>
  49.     jQuery(function($) {
  50.         $("#_redq_product_inventory").select2();
  51.     });
  52. </script>
  53. <?php
  54. });
  55.  
  56. add_action('after_wcfm_products_manage_meta_save', function($new_product_id, $wcfm_products_manage_form_data) {
  57.     if ($wcfm_products_manage_form_data['product_type'] == 'redq_rental' && isset($wcfm_products_manage_form_data['_redq_product_inventory'])) {
  58.         global $wpdb;
  59.         $pivot_table = $wpdb->prefix . 'rnb_inventory_product';
  60.  
  61.         $wpdb->delete($pivot_table, array('product' => $new_product_id), array('%d'));
  62.  
  63.         $values = array();
  64.         $fields = array();
  65.  
  66.         foreach ($wcfm_products_manage_form_data['_redq_product_inventory'] as $pvi) {
  67.             $values[] = "(%d, %d)";
  68.             $fields[] = $pvi;
  69.             $fields[] = $new_product_id;
  70.         }
  71.  
  72.         $values = implode(",", $values);
  73.         $wpdb->query($wpdb->prepare(
  74.             "INSERT INTO $pivot_table ( inventory, product ) VALUES $values",
  75.             $fields
  76.         ));
  77.  
  78.         update_post_meta($new_product_id, '_redq_product_inventory', $wcfm_products_manage_form_data['_redq_product_inventory']); //Required for demo importing
  79.     }
  80. }, 79, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement