I am trying to update woocommerce sessions but the update query which I am using is not working. When I tried to run the query in phpmyadmin the query is running successfully. But, when I am trying to run an update query in code form the database is not updated while on performing var_dump I am getting int(1). But, when I checked the database it is not updated.Can somebody please help me figuring out what mistake I am commiting?Here's is the code:
function st_update_cart_item_data(){
global $woocommerce;
$cart_item_key = array_key_first($woocommerce->cart->get_cart());
$session_data = $woocommerce->session->get_session_data();
$session_cart_data = unserialize($session_data['cart']);
$session_cart_data = array_values($session_cart_data);
$session_cart_data = $session_cart_data[array_key_first($session_cart_data)];
$woocommerce_gift_form_data = $session_cart_data['gift_form_data'];
$woocommerce_gift_recipient_data = $session_cart_data['gift_form_data']['gift_recipient_data'];
if($woocommerce_gift_form_data['bulk-gifting'])
{
if (isset($_POST['update_button']))
{
$gift_form_data = array();
$gift_form_data = filter_input_array(INPUT_POST);
$gift_form_data = $gift_form_data['gift_recipient_data'];
$woocommerce_gift_recipient_data = $gift_form_data;
$session_cart_data['gift_form_data']['gift_recipient_data'] = $woocommerce_gift_recipient_data;
$woocommerce_cart_item = array(
$cart_item_key => $session_cart_data
);
$woocommerce_cart_item['coloredcow'] = 'tehri';
$woocommerce_cart_item = serialize($woocommerce_cart_item);
$session_cart_data = $woocommerce_cart_item;
global $current_user;
if ( is_user_logged_in() ) {
global $wpdb;
$get_session_id = "SELECT {$wpdb->prefix}woocommerce_sessions.session_id FROM {$wpdb->prefix}woocommerce_sessions ORDER BY {$wpdb->prefix}woocommerce_sessions.session_id DESC LIMIT 1";
$session_id = $wpdb->get_results($get_session_id);
$session_id = json_decode(json_encode($session_id), true);
$session_id =(int) $session_id[0]['session_id'];
$update_query = "UPDATE {$wpdb->prefix}woocommerce_sessions SET {$wpdb->prefix}woocommerce_sessions.session_value = '$session_cart_data' WHERE {$wpdb->prefix}woocommerce_sessions.session_id = $session_id" ;
$result = $wpdb->query($update_query);
}
}
}
}
Having a try with Ninja Forms, I’m actually able to get value from a field ID using $form_data array variable.
function my_ninja_function( $form_data ) {
$my_field_id = 1;
$my_value_from_field_id = $form_data['fields'][$my_field_id]['value'];
echo $my_value_from_field_id;
// output value is possible
}
And now trying to get value from a field key, without success...
$my_field_key = 'my_key';
$my_value_from_field_key = $form_data['fields'][$my_field_key]['value'];
echo $my_value_from_field_key;
// output value is not possible
with a little more effort...
$form_fields = $form_data['fields'];
foreach( $form_fields as $field ){
$field_value = $field['value'];
$field_key = $field['key'];
$data[$field_key] = $field_value;
};
$my_value_from_key = $data['my_key'];
echo $my_value_from_key;
// output is possible
It works!
By value I'm assuming you mean the field's label. You can get a field's label from the field's settings like this:
$form_id = 1;
$form_fields = Ninja_Forms()->form($form_id)->get_fields();
foreach( $form_fields as $field ) {
$model = $field->get_settings();
$label = $model['label'];
}
if you really do mean value, then perhaps you are referring to a form submission's field value. You can get those like this:
$sub_id = 1; // Need to know the submission's ID
$sub = Ninja_Forms()->form()->sub($sub_id)->get();
$form_id = 1;
$form_fields = Ninja_Forms()->form($form_id)->get_fields();
foreach( $form_fields as $field ) {
$model = $field->get_settings();
$value = $sub->get_field_value($model['key']); // User submitted value
}
Note that NinjaForms has added field keys in version 3 after I made the suggestion, as previous versions had no unique field identifier which made exporting/importing fields and forms very problematic.
I'm using DBAL and I want to execute multiple insert query. But I have the problem: bindValue() method not working in loop. This is my code:
$insertQuery = "INSERT INTO `phonebook`(`number`, `company`, `user`) VALUES %s
ON DUPLICATE KEY UPDATE company=VALUES(company), user=VALUES(user)";
for ($i = 0; $i < count($data); $i++) {
$inserted[] = "(':number', ':company', ':user')";
}
$insertQuery = sprintf($insertQuery, implode(",", $inserted));
$result = $db->getConnection()->prepare($insertQuery);
for ($i = 0; $i < count($data); $i++) {
$result->bindValue($data[$i]["number"]);
$result->bindValue($data[$i]["company"]);
$result->bindValue($data[$i]["user"]);
}
$result->execute();
As result I received one-line table with fields: :number, :company, :user.
What am I doing wrong?
Thanks a lot for any help!
The problem you're having is that your binding has no way to determine to which placeholder it should be doing the binding with. To visualize it better, think on the final DBAL query you're generating:
INSERT INTO `phonebook`(`number`, `company`, `user`) VALUES
(':number', ':company', ':user'),
(':number', ':company', ':user'),
(':number', ':company', ':user');
When you do the binding, you're replacing all the parameters at the same time, ending up with a single row inserted.
One possible solution would be to give different parameter names to each row and then replace each one accordingly.
It would look like something similar to this:
public function randomParameterName()
{
return uniqid('param_');
}
...
$parameters = [];
for ($i = 0; $i < count($data); $i++) {
$parameterNames = [
'number' => $this->randomParameterName(),
'company' => $this->randomParameterName(),
'user' => $this->randomParameterName(),
];
$parameters[$i] = $parameterNames;
$inserted[] = sprintf("(':%s', ':%s', ':%s')",
$parameterNames['number'],
$parameterNames['company'],
$parameterNames['user']
);
}
$insertQuery = sprintf($insertQuery, implode(",", $inserted));
$result = $db->getConnection()->prepare($insertQuery);
foreach ($parameters as $i => $parameter) {
$result->bindValue($parameter['number'], $data[$i]["number"]);
$result->bindValue($parameter['company'], $data[$i]["company"]);
$result->bindValue($parameter['user'], $data[$i]["user"]);
}
You could probably extend your $data variable and incorporate the new parameter names into it. This would remove the need of yet another array $parameters to hold reference to the newly created parameter names.
Hope this helps
There is another alternative:
$queryStart = "INSERT INTO {$tableName} (" . implode(', ', array_keys($buffer[0])) . ") VALUES ";
$queryRows = $params = $types = [];
foreach ($rowBuffer as $row) {
$rowQuery = '(' . implode(', ', array_fill(0, count($row), '?')) . ')';
$rowParams = array_values($row);
list($rowQuery, $rowParams, $types) = SQLParserUtils::expandListParameters($rowQuery, $rowParams, $types);
$queryRows[] = $rowQuery;
$params = array_merge($params, $rowParams);
}
$query = $queryStart . implode(', ', $queryRows);
$connection->executeQuery($query, $params, $types);
Current I have WP code like this. I need to make it translateable by poedit. How do I wrap the code to make it work? Im not sure which method is use for this case. Some thing like:
<?php my_e( 'Total sales' ); ?> or __('Total sales', 'my')
This is the code. I need to translate ["Sales amount"], ["Number of sales"]
foreach ($results as $result) {
$date = $result->formatted_post_date;
$statistics[$date]["Sales amount"] += $wp_list_table->column_total_sales($result->ID);
$statistics[$date]["Number of sales"]++;
$statistics[$date]["date"] = $date;
$max_number_of_sales = max(array($max_number_of_sales,$statistics[$date]["Number of sales"] )); }
Thank you for help
You have to use __('string','textdomain') to assign a translated string to some variable. And _e('string','textdomain') to echo a translated string. See I18n_for_WordPress_Developers.
Two observations:
you'll not be able to translate array keys, see php.net/manual/en/language.types.array.php
what you're doing seems wrong. I'd do it like:
$sales_amount = 0;
$sales_number = 0;
foreach ($results as $result) {
$sales_amount += $wp_list_table->column_total_sales($result->ID);
$sales_number++;
$date = $result->formatted_post_date;
$statistics[$date]["sales_amount"] = $sales_amount;
$statistics[$date]["sales_number"] = $sales_number;
}
echo __( 'Sales Amount', 'my' ) . $sales_amount;
I'm creating nodes using a custom modules
$node = new stdClass();
$node->type = $link['content_type'];
node_object_prepare($node);
$node->uid = $user->uid;
$node->name = $user->name;
$node->title = $html['title'];
$node->language = LANGUAGE_NONE;
$node->body[$node->language][0]['value'] = $html['html'];
$node->body[$node->language][0]['summary'] = $html['summary'];
$node->body[$node->language][0]['format'] = 'filtered_html';
$node->menu['enabled'] = 0; // 1 to enable providing a link in main menu
$node->menu['link_title'] = urlencode($html['title']);
$node->menu['description'] = urlencode($html['summary']);
$node->menu['parent'] = 'main-menu:0';
$node->menu['weight'] = 5;
$node->path['alias'] = urlencode($html['title']) . time();
$node->comment = 1;
$node->status = 1; // 1 means published
$node->promote = 0;
$node->revision = 0;
$node->changed = $_SERVER['REQUEST_TIME'];
$node->created = $_SERVER['REQUEST_TIME'];
node_submit($node);
#node_save($node);
$node->path['alias'] .= '+' . $node->nid;
node_submit($node);
#node_save($node);
db_update('node_revision')
->fields(array('uid' => $node->uid))
->condition('vid', $node->vid)
->execute();
But now I need to assign each node a I create a workbench section, so I tried doing this:
$node->workbench_access = array('66');
node_submit($node);
#node_save($node);
$node->path['alias'] .= '+' . $node->nid;
node_submit($node);
#node_save($node);
db_update('node_revision')
->fields(array('uid' => $node->uid))
->condition('vid', $node->vid)
->execute();
This add the workbench access id temporarily, but when the page is refreshed it doesn't apply it. Is there a way to assign a node to a workbench section using php?
I just installed this module for the first time today funnily enough, it looks good :-)
Having a look at the workbench_access_node_insert() function (in the workbench_access.module file) it looks like the node object key it's looking for is workbench_access_id, not workbench_access.
Also you need to provide an access scheme (either menu or taxonomy depending on what access scheme you've chosen at admin/config/workbench/access/settings). I think your code should look a bit like this:
$node->workbench_access_scheme['access_scheme'] = 'taxonomy'; // or 'menu'
$node->workbench_access_id = array('66');
That's untested but looking at the module file it should work.
The following line didn't work for me.
$node->workbench_access_id = array('66');
It worked when I changed it to
$node->workbench_access = array('66');