0

I have a custom meta_key called 'sales' this holds information of all sales a sales person has carried out along with a flag to say if it has been authorised, awaiting authorisation or if it has been rejected.

Wordpress is saving each sale as a separate row in the usermeta table, which I don't have an issue with what I am stuck on is updating a specific sales status; i.e changing it from pending (2), to either rejected (0) or approved (1).

I have tried a few ways to do this following advice on other forums etc but it seems like I may be storing the meta data incorrectly in the first place as all the examples are amusing that there is only on row per custom meta_key per user; so the posts are suggesting to update the value, delete all data associated with the key then to upload the modified array.

I currently have the following code but it isn't working as I'd imagined as it is saving the data into another array so becoming further nested.

if(isset($_POST['approved'])) {

    $saleStatus = 2;

    switch ($_POST['approved']) {
        case 'approve':
            $saleStatus = 1;
            break;

        case 'reject':
            $saleStatus = 0;
            break;
    }

    $sales = get_user_meta($_POST['repID'], 'sales');

    foreach ($sales as $key => $sale) {

        if($sale['reg'] === $_POST['carReg']) {

            $sales[$key]['approved'] = $saleStatus;

        }

    }

    update_user_meta( $_POST['repID'], 'sales', $sales);
}

UPDATED

$sales = get_user_meta($_POST['repID'], 'sales', true);

foreach ($sales as $key => $value) {
  if($value === $_POST['carReg']) {
    $sales[0]['approved'] = $saleStatus; //This is overwriting all meta_values making them duplicates
 }
}

update_user_meta( $_POST['repID'], 'sales', $sales);

ADDING SALE

if(isset($_POST['customer-name']) && isset($_POST['make']) && isset($_POST['model']) && isset($_POST['reg']) && isset($_POST['condition'])) {

    $branch         = sanitize_text_field($GLOBALS['user_rep']['repBranch']);
    $customer_name  = sanitize_text_field($_POST['customer-name']);
    $make           = sanitize_text_field($_POST['make']);
    $model          = sanitize_text_field($_POST['model']);
    $reg            = sanitize_text_field($_POST['reg']);
    $condition      = sanitize_text_field($_POST['condition']);

    $newSales = array(
            'branch' => $branch,
            'customer_name' => $customer_name,
            'make' => $make,
            'model' => $model,
            'reg' => $reg,
            'condition' => $condition,
            'time' => time(),
            'approved' => 2
        );

    update_user_meta(wp_get_current_user()->ID, 'sales', $newSales);    
}

1 Answer 1

0

The problem is this line

$sales = get_user_meta($_POST['repID'], 'sales');

change it to

$sales = get_user_meta($_POST['repID'], 'sales', true);

The last parameter says, that you are getting a single value - an array. Otherwise you get an array inside another array.

https://developer.wordpress.org/reference/functions/get_post_meta/

EDIT: After reading the comments, as you have the meta in multiple rows, you should do update_post_meta in the foreach loop:

$sales = get_user_meta($_POST['repID'], 'sales');

foreach ($sales as $key => $sale) {
    $old_sale_data = $sale;
    if($sale['reg'] === $_POST['carReg']) {
        $sale['approved'] = $saleStatus;
         update_user_meta( $_POST['repID'], 'sales', $sale, $old_sale_data);
    }
}

https://codex.wordpress.org/Function_Reference/update_post_meta

Btw., saving the sales as user meta in array isn't a great idea, unless everything you're going to do with the data is displaying it. Doing queries on the data, searching, reporting, all that will be almost impossible with data saved this way. You'd be better creating a custom post type sales ad saving the meta for each of the sale, or even better creating a separate db table for sales...

7
  • I changed it as per your suggestion but now I cannot seem to target the value I want to change; for example if there are 3 sales and you approve one of them then this overwrites the others with the same info - See update
    – Pierre
    Commented Jul 3, 2016 at 22:29
  • I think that since each sale is stored in a separate row that 'true' isn't working as suggested due to it only returning the field value of the first row it comes across
    – Pierre
    Commented Jul 3, 2016 at 22:34
  • I'm trying to modify how a sale is added to the meta_data so that the suggested solution will work but currently the code above to do so isn't working it's just overwriting the existing data; when I try to merge or push the new sale with the current ones I get nested and duplicate arrays
    – Pierre
    Commented Jul 3, 2016 at 23:14
  • See edits, if that doesn't work, give us the output of var_dump(get_user_meta($_POST['repID'], 'sales')); Commented Jul 4, 2016 at 7:02
  • I'll give it a go; funny enough I was speaking to a friend and he suggested the same thing saying it'd be easier to just have them as a custom post type
    – Pierre
    Commented Jul 4, 2016 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.