Changing WooCommerce Text
Weird WPDB->Prepare Error

The WPDB->Prepare error was a red herring; I wasn’t using WPDB->Prepare anywhere

Just

the other day I was attempting to add some information to my site’s WordPress database from a JSON file, when I received a ton of ‘$wpdb->prepare’ errors. Now normally when I see these kinds of errors it means I haven’t supplied the required variable placeholders, or I haven’t done so correctly. For example, here’s an example of an incorrect usage of $wpdb->prepare:

 $wpdb->prepare("SELECT * FROM $table  LIMIT 1")

The ‘1’ in that line of code should be replaced with the ‘%d’ placeholder, and there should be a second argument that defines the placeholder, like so:

 $wpdb->prepare("SELECT * FROM $table  LIMIT %d", 1)

Upon execution, the ‘%d’ gets replaced with a ‘1’. To make this work with a string instead of an integer, use ‘%s’ instead. (NOTE: the table name in the prepare statement can be a variable and does not need to use a placeholder).

So naturally, I went looking for any instances of $wpdb->prepare… the only problem was, I hadn’t used $wpdb->prepare anywhere in my code whatsoever.

After more time than I’d like to admit, I finally figured it out – in my $wpdb->insert statement, I had specified a string variable as an integer variable, which ended up throwing $wpdb->prepare errors, oddly enough. I assumed that by using a ‘%d’ placeholder in my $wpdb->insert statement, the data formatting would be taken care of. Guess that’s what I get for assuming. Here’s the offending code:

$file = fopen(plugin_dir_path( __FILE__ ).'assets/json/company_names_formatted_backup.json',"r");
    //Output a line of the file until the end is reached
    $line = fgets($file);
    while(!feof($file)){
        $string = str_replace('\n', '', $line);
        $string = rtrim($string, ',');
        $string = "[" . trim($string) . "]";
        $json = json_decode($string, true);
        $matchingcompid = $json[0]['matchingcompid'];
        $companyname = $json[0]['companyname'];
        $table_name = $wpdb->prefix.'wpgamelist_jre_list_company_names';
        $wpdb->insert( 
                $table_name, 
                array(
                      'matchingcompid' => $matchingcompid, 
                      'companyname' => $companyname,
                ),
                array(
                        '%d',
                        '%s'
                )   
        );
        $line = fgets($file);
    }
    fclose($file);

Specifically, the $matchingcompid variable in line 9 is a string variable, and needed to be an integer. So, I simply cast $matchingcompid to an integer, like so:

$matchingcompid = (int)$json[0]['matchingcompid'];

and everything worked just as expected. Problem solved.

Updated Woocommerce shipping text
Changing WooCommerce Text
Updated Woocommerce shipping text

Updated WooCommerce Checkout Text

Recently

I had a client e-mail me about an issue with her WooCommerce checkout page. She was contacted by a customer whose billing address was in Canada, with a shipping address here in the United States. The customer found the second part of the checkout page where she could input her actual shipping address just fine, but right above this section was a checkbox (checked by default), which read, “Shipping Address Same As Billing Address“.

When the customer unchecked the box, the “ship to a different address” form disappeared. It re-appeared when checked again. In my mind, and apparently the customer and client’s minds as well, that functionality was completely backwards.

I decided the functionality (displaying/hiding the form upon checking the checkbox) was good as it was, but the text needed to be changed to something more intuitive, like, “Check Box To Ship To A Different Address“.

I decided the best way to go about this was to place a PHP function I found elsewhere online in my client’s Child Theme, that would allow us to easily change any default WooCommerce text we want now and in the future. Here’s the code:

add_filter('gettext', 'custom_strings_translation', 20, 3);

function custom_strings_translation( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Shipping address same as billing address' :
            $translated_text = __( 'Check Box to Ship to a Different Address', '__x__' );
        break;
    }
    return $translated_text;
}

Simply place the text you want changed as the case in the switch function, and place the text you’d rather have in the next line, and then you’re done!