'Woocommerce how to write or increment to existing rows in database
I have the following array
[0] => Array (
[product_id] => 11
[product_variation] => 0
[product_count] => 1
)
[1] => Array (
[product_id] => 14
[product_variation] => 18
[product_count] => 3
)
[2] => Array (
[product_id] => 14
[product_variation] => 18
[product_count] => 1
)
Now I want to write this to my database and create clean rows of data. If the product_variation is '0' that means the product is not a variable product. I want to write that to a database row that matches the product_id and increase the product_count in the database accordingly. If the product_variation is not '0' it increase the count in a row that matches the product_variation.
So the end result would contain two rows in my database:
product_id || product_variation || product_count
11 0 1
14 18 4
So far I got:
<?php
global $wpdb;
$database_orders = $wpdb->get_results( "SELECT * FROM wp_custom_orders");
$products = array();
//some code to fill $products
foreach ( $products as $product ){
if ($product['product_variation'] == '0' ) {
//write to product row where variation is the same
} else {
//write to product row where id is the same
}
}
?>
Now I know about the simple insert/update functions already available and queries using sql but I am not sure how to verify if something already exists in the database and adjust a number properly. Any help would be greatly appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
