Support

Account

Home Forums Bug Reports Problem save values SUM – Advanced Custom Fields

Helping

Problem save values SUM – Advanced Custom Fields

  • I’m developing a plugin that sum numeric values of fields. It works. when saving the post loses all values. What am I doing wrong?
    acf-sum.php

    <?php
    
    /*
    Plugin Name: Advanced Custom Fields: Unique SUM
    Plugin URI: https://wordpress.org/plugins/acf-unique-id-field
    Description: Create a unique ID to easily identify repeater field's rows.
    Version: 0.0.1
    */
    
    // 1. set text domain
    // Reference: https://codex.wordpress.org/Function_Reference/load_plugin_textdomain
    load_plugin_textdomain( 'acf-sum', false, dirname( plugin_basename(__FILE__) ) . '/lang/' ); 
    
    // 2. Include field type for ACF5
    // $version = 5 and can be ignored until ACF6 exists
    function include_field_types_sum( $version ) {
        include_once('acf-sum-v5.php');
    }
    
    add_action('acf/include_field_types', 'include_field_types_sum');

    acf-sum-v5.php

    <?php
    class acf_field_sum extends acf_field {
    
        /*
        *  __construct
        *
        *  This function will setup the field type data
        *
        *  @type    function
        *  @date    5/03/2014
        *  @since   5.0.0
        *
        *  @param   n/a
        *  @return  n/a
        */
    
        function __construct() {
    
            /*
            *  name (string) Single word, no spaces. Underscores allowed
            */
    
            $this->name = 'sum_id';
    
            /*
            *  label (string) Multiple words, can include spaces, visible when selecting a field type
            */
    
            $this->label = __('SUM', 'acf-sum');
    
            /*
            *  category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME
            */
    
            $this->category = 'layout';
    
            /*
            *  l10n (array) Array of strings that are used in JavaScript. This allows JS strings to be translated in PHP and loaded via:
            *  var message = acf._e('unique_id', 'error');
            */
    
            $this->l10n = array(
            );
    
            // do not delete!
            parent::__construct();
    
        }
        /*
        *  render_field()
        *
        *  Create the HTML interface for your field
        *
        *  @param   $field (array) the $field being rendered
        *
        *  @type    action
        *  @since   3.6
        *  @date    23/01/13
        *
        *  @param   $field (array) the $field being edited
        *  @return  n/a
        */
        function render_field( $field ) {
            ?>
    
    <script type="text/javascript">
    let total = 0;
    document.querySelectorAll('input[type="number"]').forEach(el=>total+=+el.value);
    document.querySelector('#total').value = total;
    console.log(total);
    </script>
    
    <label for="total">Total: 
    <input id="total" type="text" disabled readonly name="<?php echo esc_attr($field['name']) ?>" value=''>
    </label>
            <?php
        }
    }
    // create field
    new acf_field_sum();
  • UPDATE: I managed to make it work but the value does not save in the database

    	function render_field( $field ) {
    		?>
    <label for="total">Total:</label>
    <input id="total" type="text" disabled readonly name="<?php echo esc_attr($field['name']) ?>" value='8'>
    
    <script type="text/javascript">
    let total = 0;
    document.querySelectorAll('input[type="number"]').forEach(el=>total+=+el.value);
    document.querySelector('#total').value = total;
    console.log(total);
    </script>
    		<?php
    	}
    }
    // create field
    new acf_field_sum();
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.