0

I'm cycling through an associative array and adding each child array as a row in a table.

The last column in that table is a button in which I want to use to remove that array from the parent array in the database.

Here's my markup:

<?php
$response_cost = get_post_meta( 379, '_wc_booking_pricing', true );
?>

<div id="response-cost" class="hidden"><?php echo json_encode( $response_cost); ?></div>

<?php
$i = 0;
foreach ($response_cost as $response) { ?> 
    <tr id="array-<?php echo $i; ?>">
        <!-- table construction -->
        <td><button class="remove-array" data-id="<?php echo $i; ?>"><i class="fa fa-times"></i></button></td>
    </tr>

    <?php
    $i++; 
};
?>

Here's my jQuery that takes care of removing the child array:

(function($) {
    $(document).ready (function () {
        $(function(){
            var arrString= $('#response-cost').text();
            const arr = JSON.parse(arrString);

            $('.remove-array').click(function(){
                var val = $(this).attr("data-id");
                arr.splice(val, 1);
                var arr_stringify = JSON.stringify(arr);

                $.ajax({
                    url: ajax_object.ajaxurl,
                    type: 'POST',
                    data:{
                        action: 'update_cost_rule_table_action',
                        stringified_arr: arr_stringify,
                    },
                    success: function( data ){
                        console.log( data );
                    }
                });
            });
        });
    });
}) (jQuery);

Here's the function in my function.php file:

add_action( 'wp_ajax_update_cost_rule_table_action', 'update_cost_rule_table' );
add_action( 'wp_ajax_nopriv_update_cost_rule_table_action', 'update_cost_rule_table' );
function update_cost_rule_table(){
    $response_cost = json_decode($_POST['stringified_arr']);
    update_field('_wc_booking_pricing', $response_cost, 379);
    wp_die();
}

And here's how I'm enqueuing the script:

function gt21_scripts() {
    wp_register_script( 'cost-rule', get_template_directory_uri() . '/js/cost-rule.js', array( 'jquery' ) );
    wp_enqueue_script( 'cost-rule' );
    wp_localize_script( 'cost-rule', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
};

add_action( 'wp_enqueue_scripts', 'gt21_scripts' );

When I click the button on the front end to remove the corresponding child array, It's currently emptying the whole cell in the dB and therefore removing the row altogether so my table returns empty.

Eric Brockman
  • 824
  • 2
  • 10
  • 37
  • Are you using ACF or no? `update_field` - also your jQuery has some redundancies. What kind of value are you expecting to set for the `_wc_booking_pricing` is it supposed to be an array or a single value? – Howard E Oct 22 '21 at 21:13
  • Not using ACF, but `update_field()` seems to be working fine. If I remove the `json_decode()` line it is updating. The problem is that it's updating the with the stringified JSON instead of an array, which is what I'm expecting. – Eric Brockman Oct 22 '21 at 21:28
  • Basically I can get the jQuery to work and return stringified JSON like so: `[{"type":"custom","cost":"3","modifier":"","base_cost":"2","base_modifier":"","from":"2021-10-23","to":"2021-10-31"}]`, but I need to convert it to an array like this before updating the field: `a:1:{i:0;a:7:{s:4:"type";s:6:"custom";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:10:"2021-10-23";s:2:"to";s:10:"2021-10-31";}` – Eric Brockman Oct 22 '21 at 21:31
  • Why are you trying to write a serialized array? Wordpress wants to serialize arrays when you store as post_meta. Also - `update_field` is an ACF function, so unless `_wc_booking_pricing` is an ACF field, you want to use `update_post_meta` – Howard E Oct 22 '21 at 21:57
  • I have ACF installed for other custom meta fields, so maybe that's why it's working? the `_wc_booking_pricing` is a field created by a WooCommerce booking add on. That's how they store the data when you update using the dashboard. I'm trying to mimic that functionality on the front end. So if I can get that field populated with the serialized array, it will update the product from the front end. – Eric Brockman Oct 22 '21 at 22:09

0 Answers0