if( 151 == $cf7->id ){
$cookieid = $posted_data['cookieid'];
$cookie_status = get_post_meta ( $posted_data['cookieid'], 'cookie_status', true );
$wpdb->query('set global transaction_isolation="serializable"');
$wpdb->query('START TRANSACTION');
$stock = get_quantity($cookieid);
if( (int) $stock['quantity'] <= 0){
$errMsg = 'Sold out! Sorry, those went fast. Better luck next time.';
$abort = true;
$submission->set_status( 'validation_failed' );
$submission->set_response( $cf7->message( 'validation_error' ) ); //msg from admin settings;
$submission->set_response( $cf7->filter_message($errMsg) ); //custom msg;
}
elseif( $stock['quantity'] > 0 && $cookie_status=='live' ){
$cookie_quantty_tbl = $wpdb->prefix . "cookie";
$cookie_address_tbl = $wpdb->prefix . "cookie_address";
$new_stock_qty = (int) $stock['quantity'] - 1 ;
$stock_id = (int) $stock['id'] ;
set_quantity($new_stock_qty, $stock_id);
$wpdb->insert(
$cookie_address_tbl,
array(
'first_name'=> trim($posted_data['First_Name']),
'phone_no' => trim($posted_data['Phone_Number']),
'email' => trim($posted_data['Email']),
'cookieid' => trim($posted_data['cookieid']),
'address1' => trim($posted_data['Address_Line_1']),
'address2' => trim($posted_data['Address_Line_2']),
'city' => trim($posted_data['City']),
'state' => trim($posted_data['State'][0]),
'zipcode' => trim($posted_data['Zip_Code'])
),
array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s'
)
);
$wpdb->query('COMMIT');
}else{
}
}
function get_quantity($cookieid){
global $wpdb;
$cookietable = $wpdb->prefix . "cookie";
$cokiesql = "SELECT * FROM `ahs_cookie` WHERE cookieid = $cookieid FOR UPDATE" ;
$cokiercd = $wpdb->get_row($cokiesql) ;
return array(
'id' => $cokiercd->id,
'quantity' => $cokiercd->quantity
);
}
function set_quantity($stock_qty,$stock_id){
global $wpdb;
$cookietable = $wpdb->prefix . "cookie";
$wpdb->update(
$cookietable,
array(
'quantity' => $stock_qty
),
array( 'id' => $stock_id ),
array( '%d' ),
array( '%d' )
);
}
Related