Reputation: 81
I have an excel file with quantity and price columns which I'm using to create the necessary output for the plugin WooCommerce Dynamic Pricing
's pricing rules.
I have almost figured this out, but the WooCommerce importer is wrapping my meta value with it's own s:1:"...";
. Inside the ...
is the correct value that determines my product's pricing rules (price ranges with fixed price discount). If I manually delete the wrapper, it works. The problem is that I have 1500+ products. How can I import exactly the value I have in my CSV?
The meta: _pricing_rules
column has output that looks like this:
a:1:{s:17:"set_9jhpu8nb76h30";a:8:{s:15:"conditions_type";s:3:"all";s:10:"conditions";a:1:{i:1;a:2:{s:4:"type";s:8:"apply_to";s:4:"args";a:1:{s:10:"applies_to";s:8:"everyone";}}}s:9:"collector";a:1:{s:4:"type";s:7:"product";}s:4:"mode";s:10:"continuous";s:9:"date_from";s:0:"";s:7:"date_to";s:0:"";s:5:"rules";a:5:{i:1;a:4:{s:4:"from";s:3:"100";s:2:"to";s:3:"249";s:4:"type";s:11:"fixed_price";s:6:"amount";s:4:"3.65";}i:2;a:4:{s:4:"from";s:3:"250";s:2:"to";s:3:"499";s:4:"type";s:11:"fixed_price";s:6:"amount";s:4:"3.55";}i:3;a:4:{s:4:"from";s:3:"500";s:2:"to";s:3:"999";s:4:"type";s:11:"fixed_price";s:6:"amount";s:4:"3.46";}i:4;a:4:{s:4:"from";s:4:"1000";s:2:"to";s:4:"2499";s:4:"type";s:11:"fixed_price";s:6:"amount";s:4:"3.36";}i:5;a:4:{s:4:"from";s:4:"2500";s:2:"to";s:0:"";s:4:"type";s:11:"fixed_price";s:6:"amount";s:4:"3.27";}}s:10:"blockrules";a:1:{i:1;a:5:{s:4:"from";s:0:"";s:6:"adjust";s:0:"";s:4:"type";s:16:"fixed_adjustment";s:6:"amount";s:0:"";s:9:"repeating";s:2:"no";}}}}
Upvotes: 2
Views: 1994
Reputation: 38318
The values are being double-serialized on import. This is by design in WordPress. When saving post or object meta WordPress will serialize and unserialize objects and arrays automatically. However, sometimes developers (mis)use the Metadata API and try to handle serialization manually. To maintain backwards compatibility with these developers and their code the Metadata API will return any serialized value exactly as it was passed in. To accomplish this the value stored in the database must be wrapped in the s:99:"..."
format.
The work around is to ensure WooCommerce imports unserialized values when saving product meta.
The woocommerce_product_importer_parsed_data
filter along with the maybe_unserialize
function can be used to conditionally unserialize meta values to arrays/objects. The Metadata API will then store these fields without the double-serialization.
<?php
// Step through import meta fields.
// Use `maybe_unserialize` to conditionally expand any serialized
// values to their corresponding objects/arrays.
function csv_import_serialized($data, $importer) {
if (isset($data["meta_data"]) && is_array($data["meta_data"])) {
foreach (array_keys($data["meta_data"]) as $k) {
$data["meta_data"][$k]["value"] = maybe_unserialize($data["meta_data"][$k]["value"]);
}
}
return $data;
}
// Hook into the filter
add_filter("woocommerce_product_importer_parsed_data", "csv_import_serialized", 10, 2);
Upvotes: 2