Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mikejolley/3498250299c7cf7b24e973cbbce7a80c to your computer and use it in GitHub Desktop.
Save mikejolley/3498250299c7cf7b24e973cbbce7a80c to your computer and use it in GitHub Desktop.
Small drop-in plugin to fix double-serialized product attributes (they are no longer double-unserialized in WC Core)
<?php
/**
* Plugin Name: Double serialized attributes fixer
* Description: Adds a tool to fix double serialized attributes in WooCommerce.
* Version: 0.0.9
* Author: Mike J
* Requires at least: 4.4
* Tested up to: 4.7
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'woocommerce_debug_tools', 'double_serialized_attributes_fixer_tool' );
function double_serialized_attributes_fixer_tool( $tools ) {
$tools[ 'double_serialized_attributes_fixer' ] = array(
'name' => 'Double-serialized attributes',
'button' => 'Fix double-serialized attributes',
'desc' => 'This tool will attept to fix all double-serialized attributes for products.',
'callback' => 'double_serialized_attributes_fixer',
);
return $tools;
}
// Fixes double-serialised values - #14824.
function double_serialized_attributes_fixer() {
global $wpdb;
$product_ids = wp_parse_id_list( $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product';" ) );
foreach ( $product_ids as $product_id ) {
$attributes = get_post_meta( $product_id, '_product_attributes', true );
if ( is_serialized( $attributes ) ) {
$attributes = maybe_unserialize( $attributes );
update_post_meta( $product_id, '_product_attributes', $attributes );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment