Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('wcfm_product_fields_stock', function($stock_fields, $product_id, $product_type) {
- global $WCFMu;
- if (apply_filters('wcfm_is_allow_rental', true) && WCFMu_Dependencies::wcfm_wc_rental_pro_active_check()) {
- remove_filter('wcfm_product_fields_stock', [ $WCFMu->wcfmu_integrations, 'wcfm_wcrental_product_inventory_manage' ], 80);
- $args = array(
- 'post_type' => 'Inventory',
- 'posts_per_page' => -1,
- 'orderby' => 'date',
- 'order' => 'ASC',
- 'post_status' => 'publish'
- );
- $inventories = get_posts($args);
- $formatted_inventories = [];
- if(!empty($inventories)) {
- foreach ($inventories as $key => $inventory) {
- $formatted_inventories[$inventory->ID] = $inventory->post_title;
- }
- wp_reset_postdata();
- }
- $selected_inventories = rnb_get_product_inventory_id($product_id);
- $inventory_elements = apply_filters(
- 'wcfm_redq_rental_fields_inventory',
- [
- '_redq_product_inventory' => [
- 'label' => __('Choose Inventories', 'redq-rental'),
- 'type' => 'select',
- 'attributes' => array( 'multiple' => 'multiple', 'style' => 'width: 60%;' ),
- 'class' => 'wcfm-select wcfm_ele redq_rental',
- 'label_class' => 'wcfm_title wcfm_ele redq_rental',
- 'value' => !empty($selected_inventories) && is_array($selected_inventories) ? $selected_inventories : [],
- 'options' => $formatted_inventories,
- ],
- ]
- );
- $stock_fields = array_merge($stock_fields, $inventory_elements);
- return $stock_fields;
- }
- }, 79, 3);
- add_action('wcfm_products_manage_inventory_end', function() {
- ?>
- <script>
- jQuery(function($) {
- $("#_redq_product_inventory").select2();
- });
- </script>
- <?php
- });
- add_action('after_wcfm_products_manage_meta_save', function($new_product_id, $wcfm_products_manage_form_data) {
- if ($wcfm_products_manage_form_data['product_type'] == 'redq_rental' && isset($wcfm_products_manage_form_data['_redq_product_inventory'])) {
- global $wpdb;
- $pivot_table = $wpdb->prefix . 'rnb_inventory_product';
- $wpdb->delete($pivot_table, array('product' => $new_product_id), array('%d'));
- $values = array();
- $fields = array();
- foreach ($wcfm_products_manage_form_data['_redq_product_inventory'] as $pvi) {
- $values[] = "(%d, %d)";
- $fields[] = $pvi;
- $fields[] = $new_product_id;
- }
- $values = implode(",", $values);
- $wpdb->query($wpdb->prepare(
- "INSERT INTO $pivot_table ( inventory, product ) VALUES $values",
- $fields
- ));
- update_post_meta($new_product_id, '_redq_product_inventory', $wcfm_products_manage_form_data['_redq_product_inventory']); //Required for demo importing
- }
- }, 79, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement