• Resolved soulvt13

    (@soulvt13)


    So, I have created a table and able to display it without a problem
    I currently use this plugin as a simple stock inventory management with ProductId, ProductName, Quantity as an example.

    I created a page to display it using shortcode [cdbt-view] , no problem
    Then I create another page to update the data in case of adding stocks or selling something.. I found this can be done using [cdbt-edit] but we need to specify our final quantity after addition / subtraction by ourselves.

    I want to add a field in the update pop out for example if I need to add 5, then it will automatically add 5 to the quantity columns, is it viable using this plugin?

    https://wordpress.org/plugins/custom-database-tables/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author ka2

    (@ka2)

    Thank you for your inquiry.

    Yes, you can add the custom fields and customize the process at the time of updating data. I will introduce the sample code as an example below.

    Sample table:

    CREATE TABLE wp_products (
      ProductID bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Product ID',
      ProductName varchar(200) NOT NULL,
      Quantity int(11) unsigned NOT NULL DEFAULT '0',
      created datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Created Datetime',
      updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Updated Datetime',
      PRIMARY KEY (ProductID)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

    Sample Post:

    [cdbt-edit table="wp_products"]

    Filter hooks: (“functios.php” in theme)

    function add_custom_field_wp_products( $elements_options, $shortcode_name, $table ){
      if ( is_null( get_post() ) && 'cdbt-entry' === $shortcode_name && 'wp_products' === $table ) {
        $elements_options[] = [
          'elementName' => 'ChangeQuantity',
          'elementLabel' => 'Addition/Subtraction',
          'elementType' => 'number',
          'isRequired' => false,
          'defaultValue' => 0,
          'placeholder' => 'Enter changing quantity',
          'addClass' => '',
          'selectableList' => '',
          'horizontalList' => false,
          'elementSize' => 2,
          'helperText' => '',
          'elementExtras' => []
        ];
        $element_order = [0,1,3,2];
        array_multisort( $element_order, $elements_options );
      }
      return $elements_options;
    }
    add_filter( 'cdbt_shortcode_custom_forms', 'add_custom_field_wp_products', 10, 3 );
    
    function custom_update_wp_products( $data, $table_name, $data_field_format ){
      if ( ! is_admin() && 'wp_products' === $table_name && isset( $_POST['custom-database-tables']['ChangeQuantity'] ) ) {
        if ( intval( $_POST['custom-database-tables']['ChangeQuantity'] ) !== 0 ) {
          $data['Quantity'] += intval( $_POST['custom-database-tables']['ChangeQuantity'] );
        }
      }
      return $data;
    }
    add_filter( 'cdbt_before_update_data', 'custom_update_wp_products', 10, 3 );

    By the above code, you can have the additional field for addition or subtraction and custom update process. I have uploaded the demo page of this sample, please try to see.

    Please refer below full description for each filters (sorry in Japanese only).

    https://ka2.org/cdbt/v2/filter-reference/cdbt_shortcode_custom_forms/
    https://ka2.org/cdbt/v2/filter-reference/cdbt_before_update_data/

    Thank you,

    Thread Starter soulvt13

    (@soulvt13)

    Sorry if this sound silly, i should add the filter hooks in function.php right? Thanks

    I’m new to wordpress

    Plugin Author ka2

    (@ka2)

    Okay, I should be thanking you.

    Because I was recently also published the specifications of the filter hooks, it was a good experience to be able to introduce the mechanism of per that.

    Thread Starter soulvt13

    (@soulvt13)

    sorry to bother you..
    if we create table named stockmgmt, should we change the filter hooks to function add_custom_field_wpdt_stockmgmt …. and all wp_products to wpdt_stockmgmt or leave it be wp_products?

    and we should add above code in the functions.php from the theme right?

    Thread Starter soulvt13

    (@soulvt13)

    I have been able to add addition and subtraction, but when I clicked to update the data, the textbox didn’t automatically updated to the field. any solution??

    Plugin Author ka2

    (@ka2)

    Sorry for my late reply.

    I do not understand yet the automatic update mechanism that you want.
    Sorry to trouble you, please tell me the more detailed specifications.

    thank you,

    Thread Starter soulvt13

    (@soulvt13)

    I mean, for the cbdt edit, if we want to update the record, we ticked one record and clicked update button right? After we clicked update button, a pop out will appear with form of field we want to update. In the demo, after the pop out appear, the field will automatically entered with the details of the record,thus we only need to change the field we wanted to change. In my case, the field all left blank and needed to fill in one by one manually. For troubleshooting purposes, i have opened my website momentarily so you can see the problem. http://stock.andryanvt.xyz/index.php/update-data/

    And can the updated datetime be made automatically registered in the table without appearing in the pop out? ( making a better UI)
    Thankyou, sorry for troubling you

    Plugin Author ka2

    (@ka2)

    Thank you very much for your prompt reply.

    Your trouble is there does not set current values to the entry forms that pop out by shortcode “cdbt-edit”? My this recognition is wrong?
    If my above recognition was right, please try to confirm of whether occurred JavaScript errors on your page.

    Thank you for providing the URL. But, I could not see the page because I did not have authority, unfortunately.

    Thank you,

    Thread Starter soulvt13

    (@soulvt13)

    I’m really sorry for the inconvenience.
    I have lowered down the permission to view the data.

    Yeah that’s the problem I described.. My page simply consist of shortcodes from the plugins, sorry for troubling you again, where to check the javascript errors on my page?
    thank you

    Plugin Author ka2

    (@ka2)

    I’m very sorry, it was a bug in plugin.

    It was a problem that can not be successfully retrieved data when hide the primary key column (such as “ID”) at “cdbt-edit”.

    I will release a modified plugin in the next version. Please wait.

    Thank you for taking the time to this matter.

    Thread Starter soulvt13

    (@soulvt13)

    Thank you so much for your reply. Such an incredible support. Thumbs up! So for the mean time, if i didnt hide the id, it should be okay?

    Thread Starter soulvt13

    (@soulvt13)

    Hello another question
    This is my sql create table code

    CREATE TABLEwpdt_stockmgmt` (
    ProductID bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘Product ID’,
    ProductCode varchar(50) NOT NULL,
    ProductName varchar(200) NOT NULL,
    Quantity int(11) unsigned NOT NULL DEFAULT ‘0’,
    PRIMARY KEY (ProductID)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8`

    and this is what I added in the functions.php in theme

    function add_custom_field_wpdt_stockmgmt( $elements_options, $shortcode_name, $table ){
      if ( is_null( get_post() ) && 'cdbt-entry' === $shortcode_name && 'wpdt_stockmgmt' === $table ) {
        $elements_options[] = [
          'elementName' => 'ChangeQuantity',
          'elementLabel' => 'Addition/Subtraction',
          'elementType' => 'number',
          'isRequired' => false,
          'defaultValue' => 0,
          'placeholder' => 'Enter changing quantity',
          'addClass' => '',
          'selectableList' => '',
          'horizontalList' => false,
          'elementSize' => 2,
          'helperText' => '',
          'elementExtras' => []
        ];
        $element_order = [0,1,3,2];
        array_multisort( $element_order, $elements_options );
      }
      return $elements_options;
    }
    add_filter( 'cdbt_shortcode_custom_forms', 'add_custom_field_wpdt_stockmgmt', 10, 3);
    
    function custom_update_wpdt_stockmgmt( $data, $table_name, $data_field_format ){
      if ( ! is_admin() && 'wpdt_stockmgmt' === $table_name && isset( $_POST['custom-database-tables']['ChangeQuantity'] ) ) {
        if ( intval( $_POST['custom-database-tables']['ChangeQuantity'] ) !== 0 ) {
          $data['Quantity'] += intval( $_POST['custom-database-tables']['ChangeQuantity'] );
        }
      }
      return $data;
    }

    I can’t update my data using the Addition/Subtraction textbox, need to change manually the quantity. did I do something wrong? Please refers to my site again if you are not bothered. thank you

    Plugin Author ka2

    (@ka2)

    Your code has been leaked to add a function “custom_update_wpdt_stockmgmt” to the filter.
    Please add the following code to the end.

    add_filter( 'cdbt_before_update_data', 'custom_update_wpdt_stockmgmt', 10, 3 );

    Thank you,

    Thread Starter soulvt13

    (@soulvt13)

    Thank you so much.. Really grateful to meet you ! Thank you for the advice.

    Plugin Author ka2

    (@ka2)

    I closed this issue because that has resolved.

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Update Data Feature and Enquiry’ is closed to new replies.