Enabling multi-lingual string replacements without a multilingual plugin

WordPress has had support since version 4.7 for admin users to choose the language that they wish to use the admin area in, separate from the language used to display the front-end of the site.

It’s possible for you to run a site where the administration users interact in English, but the front-end of the site is displayed in a different language, e.g. French. This kind of setup doesn’t require the use of a multi-lingual plugin since front-end content only exists in one language.

By default, Say What? Pro only enables per-language string replacements if it detects a compatible multilingual plugin active on the site. However, if you’re running a dual-language setup using the built in WordPress user language selection, you can enable the features by adding a small plugin to your site:

Once this is installed, and active, you’ll be able to set replacements per-language as if you were running a multi-lingual frontend.

Replace words across whole site

Some of our users have asked us what is the best way to replace a word across their whole WordPress site. This isn’t something that our string replacement plugin “Say What? Pro” has traditionally been a good solution for. It normally works on full sentences, rather than individual words, so you would swap one sentence for another.

However, the new “wildcard swaps” feature allows you to do just that. Simply enter part of a string or word, and replacements will be carried out across all translatable strings on the site . These swaps are evaluated for every single string on your site, so they’re not the most efficient things in the world, and we’d always recommend proper full replacements where possible.

They can also lead to some interesting results if you’re overly broad with what you replace. For example here’s what happens to the WordPress “Users” menu if you set up a swap for “Use” to “Choose”. However – pick your strings carefully and it can be a useful way of replacing a wide range of strings without having to set up specific replacements for each sentence.

 

Override Twitter share message per product

Sometimes you may want to customise the sharing message per product when creating sharing links with the Social Checkout for WooCommerce plugin. The plugin includes filters to allow you to change the message however you want.

The snippet below shows how you can provide a custom message for a specific product – of course you can make this logic as simple – or as complex as you like:

 

Exclude specific items from Social Checkout

The Social Checkout for WooCommerce plugin lets you create sharing links for the items in a customer’s cart. Sometimes you might want to exclude specific products from this list.

The plugin includes filters that let you override the list of items to be shown. Here’s a sample that excludes product ID 411 from being shown – obviously you can code up whatever logic you need.

 

Changing the separator in the export files

Sometimes you may want to change the separator in the order export files produced by the WP e-Commerce Export plugin. Fortunately you can override the separator with a simple filter.

Just drop the snippet below into a plugin, or your theme’s functions.php file. This example uses a semi-colon (;) as a separator – but you can use any character you need.

 

Removing shipping dimensions from WooCommerce Google Product Feed

The following snippet will let you remove all shipping dimensions from the WooCommerce Google Product feed.

Simply drop it into your theme’s functions.php file if you’re using a child theme, or add it to a Functionality plugin.

Adding extra parameters to URLS in WooCommerce Google Product Feed

Sometimes you’ll want to add additional information to the URLs that your site submits to Google Merchant Centre. This can be important because you want to track the URLs – or have the page jump to relevant information for users coming through product ads.

There are filters in the plugin that allow you to alter the information that is used for any particular product. The simple example below adds a query argument of “queryArg” with a value of “myValue” to all product URLs.

 

Backwards compatibility for text-domain changes

WordPress.org is currently importing language packs for plugins into their central system, allowing them to be managed and downloaded separately rather than having to bundle everything with plugins. One of the knock on effects to this process is that some plugins are required to change their text-domain.

As an example, easy digital downloads is having to change from a text domain of “edd” to “easy-digital-downloads”. Unfortunately this would ordinarily cause any string replacements you’ve set up to stop working – since the strings being offered for replacement no longer match what you would have set up in Say What?

Fortunately, the Say What? plugin allows domain aliases to be configured so that a plugin can register the fact that their new text domain was previously something else, and then Say What? can continue to use your existing replacements. If you’ve come across a plugin that has changed its text domain and replacements no longer work, then a code snippet like this will allow you (or the plugin author) to map the domains for you.

Fortunately for Easy Digital Downloads users – the plugin already includes this for you – so the whole process should be seamless as long as all of your plugins are up to date. Hopefully this post will let you map any changed domains yourself, or get changes implemented into plugins of your choosing.

Remove sale price from WooCommerce Google Product Feed

There are some situations where you don’t want to include the sale prices for products – for example where legal, or contractual restrictions mean that you can only advertise the products at full price.

The standard behaviour of the WooCommerce Product Feed plugin is to include both the regular price, and sale price – however with a small filter you can exclude the sale  price from being sent. Just drop the snippet below into your theme’s functions.php file – or a Functionality Plugin.

 

Setting default shipping option with WooCommerce Pro Shipping

WooCommerce Pro Shipping lets you set up multiple different rates, each of which can have different pricing rules, availability rules, destinations and minimum / maximum thresholds. This means that WooCommerce’s built in default “default shipping” choices – which only let you choose “Pro Shipping” aren’t quite sufficient.

However, WooCommerce does include a filter that can be used to set a default shipping choice. In our example, we’ve got a number of different rates set up.

Screenshot 2015-04-27 18.35.23

 

We’re going to make WooCommerce choose “Value Pricing” (rate ID 4) as the default if it’s available, otherwise falling back to “Quantity Pricing” (rate ID 3), or just WooCommerce’s normal “cheapest rate” choice if neither are available. The following code should be added to your theme’s functions.php file or a functionality plugin.

 

Excluding individual bundled products

The Product Bundles add-on from WooCommerce allows you to bundle multiple products into a group for sale. Usually, when these are purchased, the Social Checkout for WooCommerce plugin will show sharing links for the bundle product, and all of the individual items in the bundle as well. If you would prefer to just show the main bundle product, and not the individual items, then adding the following snippet to your theme’s functions.php file, or a functionality plugin will exclude them from having sharing links shown.

 

Overriding shared product information

By default, the Social Checkout for WooCommerce plugin shares the main product image, the product title, and the standard product URL. In some situations you may want to override those values with something else. Perhaps you want to provide a more engaging product title – or direct customers to a special Social Media focussed landing page for a product.

The plugin provides filters for you to change the information that is used. The filter receives the calculated values for the item, the product that’s being shown (Note: this may be empty if an order contains more than five items and a single share link is being shown), and the full WooCommerce order.

In the following example, we’re overriding the product URL to send shares to a specific product landing page.

You can change any of the following properties on the item object:

  • image
  • title
  • permalink

Adding custom field to payment export

The EDD Payment Export & Reports plugin allows you to export details of your customers orders to a CSV. The plugin contains support for most of the standard Easy Digital Download payment fields out of the box, as well as supporting information from well-known EDD extensions such as Software Licensing.

Sometimes however, you may have custom checkout fields that you want to export alongside the standard fields. In this case, you can alter the behaviour of the plugin to include your fields via a set of WordPress filters. These will let you register additional fields, and specify how they the output for those fields should be gathered / calculated. Simply drop these into your theme’s functions.php file or a functionality plugin, then you’ll be free to keep updating the main plugin without fear of losing your changes.

In the example below we’re going to create a “Full name” field that will simply concatenate the first, and last name of the purchases.

There are two functions you’ll need to define. The first registers the field with the plugin, making it available to choose for export. This function also tells the plugin what function it should call to generate the output for this field.

Here – we’ve defined a new field that will show up as “Customer Full Name” on the export screen, and in the heading of the column in the CSV. When the plugin generates the CSV, it will call the function lw_render_my_field() to generate the output for our field.

Our “rendering” function will be passed a $payment variable which will contain the basic information about the payment already. Here we’re picking up the first and last name, and returning them as a single string. Of course – you could load up a custom value here, and return that if you have something specific you want to grab.

Restricting WooCommerce shipping rates by weight

Sometimes you want to provide rates but limit their availability based on the weight of the order – for example if your carrier only carries packages up to a certain weight.

The WooCommerce Pro Shipping plugin already lets you set up tiered rates by weight, or item quantity, but there’s no way to set a maximum weight for a particular rate. Fortunately there are filters in place that let you achieve that fairly easily.

The example below limits a specific rate to a maximum weight of “5” – the units are whatever your store base weight unit is set to, e.g. if your store is set up to use kilograms, this will block the specific rate (ID 1 in our example below) over 5kg.

Make scheduled exports include all data

When running scheduled exports with the WP e-Commerce dashboard & export plugin, the default behaviour is that the export will include all data created sine the export last ran. Sometime you may want your exports to include a full picture of all data, not just those items changed since the last run.

The plugin allows you to control the time used for the start of the data, so by overriding that you can make your sales export include all data. Here’s an example code snippet that can go in your theme’s functions.php or in a functionality plugin.

 

Defaulting the shipping country at checkout with WP e-Commerce

The latest versions of WP e-Commerce no longer default the shipping country when a customer first arrives at checkout. If you want to reproduce this behaviour you can still do so by adding a small snippet of code to your theme’s functions.php file, or by using a functionality plugin. Here’s a snippet that defaults the country to the store base country.

 

Limiting orders included in Freshdesk

The EDD Payment status is included in the order view in Freshdesk, but sometimes you may want to limit the orders that are shown to those in particular statuses.

By default, orders in any of the following statuses are included:

  • Pending
  • Complete
  • Refunded

The EDD Freshdesk plugin contains a filter that lets you control which order statuses are included in the feed to Freshdesk, so you can include code like the below in your theme’s functions.php file, or a functionality plugin to limit the orders that are included. For example, if you want to limit just to Complete orders, you can do the following:

Shipping cost not passed to PayPal

PayPal appear to be experiencing a temporary issue where shipping / handling costs passed across to them aren’t taken into account – leading to orders effectively having no shipping charge when PayPal process the order.

If you’re using WP e-Commerce with PayPal and are hitting this issue, then adding the following snippet to your theme’s functions.php file should work around the issue for now.

For the technical among you – PayPal seem to be ignoring costs passed in the handling_cart variable. This snippet simply adds what should be in that variable to the shipping cost of the first product in the cart, and zeroes the handling_cart. It’s only intended as a temporary fix, and should be removed when PayPal have resolved the issue.

 

Replacing WordPress strings with “context”

When writing a WordPress plugin, programmers occasionally have strings that they want to mark for translation, but the same string can mean different things in different places.

For example you might want to refer to “Pounds” as a unit of weight, and “Pounds” as the shorthand for the British currency. In that case you can provide additional context that translators can use to determine which “pounds” you mean in case the translation is different.

So, while a simple use of a translated string includes only the string, and the text domain, and doesn’t require you to provide a context:

If you wanted to provide context in different use cases, you can use the _x() function and provide the context as an extra argument:

Translators can provide different translations for each of these strings.

sw-wcontextIf you’re looking to provide an alternative string using the Say What string replacement plugin then you need to know the domain, and the context of the string you’re looking to translate. So – adding a replacement string with a context of “Unit of weight” as per this screenshow, would only replace the first string in our example – the currency string would remain unaffected.

If you wanted to replace the second string, then you’d add it as a second replacement, using “The British currency” as the context value.

Of course – if the string you’re trying to replace using the standard _() or _e() functions, and doesn’t have a specific context, you can just leave the text context field blank.

Sending extra fields to MailChimp after purchase

The WP e-Commerce MailChimp plugin automatically sends customer information, and e-Commerce data to MailChimp straight at point of purchase. Depending on your precise business needs, you may want to send other information along with the subscriber information.

The plugin contains a filter scmi_custom_fields which passes you the current list of fields being sent to MailChimp, and the purchase log ID. You can attach to this filter, and modify the existing fields, or add additional fields.

The code below shows how you can grab additional information from the WP e-Commerce purchase log and send it to MailChimp along with the subscriber information. In this example, we’re going to grab the customer’s postcode and send it along into a MailChimp field called POSTCODE.