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);
}
}
}
}
Related
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 need to create a custom module in vTiger, but it should have all kind of variations in terms of field data types, default values, dependent picklists, relation fields [UI Type 10], etc.
I have installed the latest stable version of vTiger7, but I'm unable to start the entry point of this code, so let me know how to start.
Here is script where you can create new custom module in vTiger7. Keep Coding!!
You need to execute this script through url.
<?php
include_once 'vtlib/Vtiger/Module.php';
include_once 'vtlib/Vtiger/Package.php';
include_once 'includes/main/WebUI.php';
include_once 'include/Webservices/Utils.php';
$Vtiger_Utils_Log = true;
$MODULENAME = 'CustomModule';
$moduleInstance = new Vtiger_Module();
$moduleInstance->name = $MODULENAME;
$moduleInstance->parent = "Tools";
$moduleInstance->save();
// Schema Setup
$moduleInstance->initTables();
// Webservice Setup
$moduleInstance->initWebservice();
// Field Setup
$block1 = new Vtiger_Block();
$block1->label = 'LBL_' . strtoupper($moduleInstance->name) . '_INFORMATION';
$moduleInstance->addBlock($block1);
// Filter Setup
$filter1 = new Vtiger_Filter();
$filter1->name = 'All';
$filter1->isdefault = true;
$moduleInstance->addFilter($filter1);
// Add field here using normal defination
$field = new Vtiger_Field();
$field->name = 'custom_name';
$field->table = $moduleInstance->basetable;
$field->label = 'Custom Name';
$field->column = $field->name;
$field->columntype = 'VARCHAR(100)';
$field->uitype = 1;
$field->displaytype = 1;
$field->presence = 2;
$field->typeofdata = 'V~M';
$block1->addField($field);
$filter1->addField($field, 2);
$field = new Vtiger_Field();
$field->name = 'assigned_user_id';
$field->label = 'Assigned To';
$field->table = 'vtiger_crmentity';
$field->column = 'smownerid';
$field->uitype = 53;
$field->displaytype = 1;
$field->presence = 2;
$field->typeofdata = 'V~M';
$block1->addField($field);
$field = new Vtiger_Field();
$field->name = 'createdtime';
$field->label = 'Created Time';
$field->table = 'vtiger_crmentity';
$field->column = 'createdtime';
$field->displaytype = 2;
$field->uitype = 70;
$field->typeofdata = 'D~O';
$block1->addField($field);
$field = new Vtiger_Field();
$field->name = 'modifiedtime';
$field->label = 'Modified Time';
$field->table = 'vtiger_crmentity';
$field->column = 'modifiedtime';
$field->displaytype = 2;
$field->uitype = 70;
$field->typeofdata = 'D~O';
$block1->addField($field);
// Sharing Access Setup
$moduleInstance->setDefaultSharing('Public');
$targetpath = 'modules/' . $moduleInstance->name;
if (! is_file($targetpath)) {
mkdir($targetpath);
$templatepath = 'vtlib/ModuleDir/6.0.0';
$moduleFileContents = file_get_contents($templatepath . '/ModuleName.php');
$replacevars = array(
'ModuleName' => $moduleInstance->name,
'<modulename>' => strtolower($moduleInstance->name),
'<entityfieldlabel>' => $field1->label,
'<entitycolumn>' => $field1->column,
'<entityfieldname>' => $field1->name
);
foreach ($replacevars as $key => $value) {
$moduleFileContents = str_replace($key, $value, $moduleFileContents);
}
file_put_contents($targetpath . '/' . $moduleInstance->name . '.php', $moduleFileContents);
}
if (! file_exists('languages/en_us/ModuleName.php')) {
$ModuleLanguageContents = file_get_contents($templatepath . '/languages/en_us/ModuleName.php');
$replaceparams = array(
'Module Name' => $moduleInstance->name,
'Custom' => $moduleInstance->name,
'ModuleBlock' => $moduleInstance->name,
'ModuleFieldLabel Text' => $field1->label
);
foreach ($replaceparams as $key => $value) {
$ModuleLanguageContents = str_replace($key, $value, $ModuleLanguageContents);
}
$languagePath = 'languages/en_us';
file_put_contents($languagePath . '/' . $moduleInstance->name . '.php', $ModuleLanguageContents);
}
Settings_MenuEditor_Module_Model::addModuleToApp($moduleInstance->name, $moduleInstance->parent);
echo $moduleInstance->name." is Created";
?>
Use free modules like Vtiger Module Builder. these modules use vtlib libary and you can create new modules through Vtiger UI.
I want to insert 50k products in woocommerce using cron job. Data will come from json API. SO please guide me how can I do this job ?
$fileContents = file_get_contents(ABSPATH.'json_array.txt');
if ($fileContents === false)
{
echo 'ERROR!';
}
else
{
$data = json_decode($fileContents, true);
// count($data['DataList']; output 50000
for($i=0;$i<count($data['DataList']);$i++)
{
$Shape = $data->DataList[$i]->Shape;
$Size = $data->DataList[$i]->Size;
$Color = $data->DataList[$i]->Color;
$Clarity = $data->DataList[$i]->Clarity;
$objProduct = new WC_Product();
$objProduct->set_name($ReportNo); //Set product name.
$objProduct->set_status('publish');
$objProduct->set_featured(TRUE);
$objProduct->set_catalog_visibility('visible');
$new_product_id = $objProduct->save();
}
}
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);
A client has requested that I make the characters "S" and "$" interchangeable in the search function, i.e. "Search Query" and "$earch Query" should return identical results.
Is there a way to accomplish this?
Change it before WP queries the database:
$the_replacements = array(
'$' => 'S',
);
function modify_search_vars($search_vars) {
global $the_replacements;
if (!empty($search_vars['s']) && !empty($the_replacements[$search_vars['s']])) {
$search_vars['s'] = $the_replacements[$search_vars['s']];
}
return $search_vars;
}
add_filter('request', 'modify_search_vars', 99);
This seems to work well enough I guess..
add_action('pre_get_posts', 'modified_search');
function modified_search($query){
global $wp_query;
if($query->is_search){
global $wpdb;
$original_query = get_search_query();
$modified_query = preg_replace("/(s|S)/", "$", $original_query);
$new_query = "
SELECT $wpdb->posts.ID
FROM $wpdb->posts
WHERE $wpdb->posts.post_status = 'publish'
AND (($wpdb->posts.post_title LIKE '%$original_query%') OR ($wpdb->posts.post_content LIKE '%$original_query%') OR ($wpdb->posts.post_title LIKE '%$modified_query%') OR ($wpdb->posts.post_content LIKE '%$modified_query%'))
ORDER BY $wpdb->posts.post_date DESC
LIMIT 0, 10
";
$results = $wpdb->get_results($new_query);
$post_ids = array();
foreach ($results as $post_id){
$post_ids[] = $post_id->ID;
}
$query->set('post__in', $post_ids);
}
}
I'm sure there are ways to improve that, and I'm more than happy to implement suggestions, but this appears to return results using both search terms, so it's good enough.