WPDB not updating row properly

[ad_1]

I’m working on a plugin for my WC store and can’t figure out why wpdb is not updating the row and the data remains the same. The result shows 1 row change and the query looks correct. I’ve even tested manually executing the query produced by $wpdb->last\_query directly in the DB and it works fine.

I’ll post the function below, but here’s an example of an update query that the function has prepared:

UPDATE `tb_postmeta` SET `meta_value` = ‘a:4:{s:3:\”fit\”;a:6:{s:4:\”name\”;s:3:\”fit\”;s:5:\”value\”;s:21:\”Male Fit | Female Fit\”;s:8:\”position\”;i:0;s:10:\”is_visible\”;i:1;s:12:\”is_variation\”;i:1;s:11:\”is_taxonomy\”;i:0;}s:6:\”colors\”;a:6:{s:4:\”name\”;s:6:\”colors\”;s:5:\”value\”;s:60:\”White | Black | Dark Chocolate | Heather Royal | Heather Red\”;s:8:\”position\”;i:1;s:10:\”is_visible\”;i:1;s:12:\”is_variation\”;i:1;s:11:\”is_taxonomy\”;i:0;}s:5:\”sizes\”;a:6:{s:4:\”name\”;s:5:\”sizes\”;s:5:\”value\”;s:26:\”S | M | L | XL | 2XL | 3XL\”;s:8:\”position\”;i:2;s:10:\”is_visible\”;i:1;s:12:\”is_variation\”;i:1;s:11:\”is_taxonomy\”;i:0;}s:3:\”pid\”;a:6:{s:4:\”name\”;s:3:\”pid\”;s:5:\”value\”;s:11:\”3524 | 3586\”;s:8:\”position\”;i:3;s:10:\”is_visible\”;i:1;s:12:\”is_variation\”;i:1;s:11:\”is_taxonomy\”;i:0;}}’ WHERE `post_id` = 2221 AND `meta_key` = ‘_product_attributes’

**Function:**

If needed I can provide more context about the function, but the key thing is that it works fine when inserting just not updating the row even though the query itself looks correct.

function copy_update_variations($parent_id, $child_ids) {
global $wpdb;

$parent_product = wc_get_product($parent_id);
if (!$parent_product) {
error_log(“Parent product not found: {$parent_id}”);
return;
}

$parent_attributes = $parent_product->get_attributes();
error_log(“Initial parent attributes: ” . print_r($parent_attributes, true));

foreach ($child_ids as $child_id) {
$child_product = wc_get_product($child_id);
if (!$child_product || ‘variable’ !== $child_product->get_type()) continue;

error_log(“Processing child product: {$child_id}”);

foreach ($child_product->get_attributes() as $attribute_name => $attribute) {
if (!($attribute instanceof WC_Product_Attribute)) {
error_log(“Invalid attribute: {$attribute_name} from child product: {$child_id}”);
continue;
}

$child_options = $attribute->get_options();
$visible = $attribute->get_visible() ? 1 : 0;
$position = $attribute->get_position();
$variation = $attribute->get_variation() ? 1 : 0;

if (isset($parent_attributes[$attribute_name])) {
$parent_attribute = $parent_attributes[$attribute_name];
$parent_options = $parent_attribute->get_options();
$merged_options = array_unique(array_merge($parent_options, $child_options));
$parent_attribute->set_options($merged_options);
} else {
$new_attribute = new WC_Product_Attribute();
$new_attribute->set_name($attribute_name);
$new_attribute->set_options($child_options);
$new_attribute->set_visible($visible);
$new_attribute->set_position($position);
$new_attribute->set_variation($variation);
$parent_attributes[$attribute_name] = $new_attribute;
}
}
}

$attributes_data = array();
foreach ($parent_attributes as $key => $attribute) {
if ($attribute instanceof WC_Product_Attribute) {
$attributes_data[$key] = array(
‘name’ => $attribute->get_name(),
‘value’ => implode(‘ | ‘, $attribute->get_options()),
‘position’ => $attribute->get_position(),
‘is_visible’ => $attribute->get_visible() ? 1 : 0,
‘is_variation’ => $attribute->get_variation() ? 1 : 0,
‘is_taxonomy’ => $attribute->is_taxonomy() ? 1 : 0,
);
}
}

$serialized_attributes = maybe_serialize($attributes_data);
error_log(“Serialized updated attributes data: ” . $serialized_attributes);

// Prepare the SQL query using wpdb
$table_name = $wpdb->postmeta;
$meta_exists = $wpdb->get_var($wpdb->prepare(
“SELECT meta_id FROM $table_name WHERE post_id = %d AND meta_key = ‘_product_attributes'”,
$parent_id
));

if ($meta_exists) {
// Update the ‘_product_attributes’ row
$result = $wpdb->update(
$table_name,
array(‘meta_value’ => $serialized_attributes),
array(‘post_id’ => $parent_id, ‘meta_key’ => ‘_product_attributes’),
array(‘%s’),
array(‘%d’, ‘%s’)
);
} else {
// Insert the ‘_product_attributes’ row
$result = $wpdb->insert(
$table_name,
array(
‘post_id’ => $parent_id,
‘meta_key’ => ‘_product_attributes’,
‘meta_value’ => $serialized_attributes
),
array(‘%d’, ‘%s’, ‘%s’)
);
}

if ($result === false) {
error_log(“Failed to insert/update ‘_product_attributes’: ” . $wpdb->last_error);
} else {
error_log(“Successfully processed ‘_product_attributes’ for product ID {$parent_id}”);
error_log(“SQL Query: ” . $wpdb->last_query);
}
}

​

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer