Tag Archives: wordpress

WooCommerce Multilingual translated variations’ quantity out of sync fix

This PHP script may help if WooCommerce Multilingual product variation quantities are getting out of sync. Basically what it does is it deletes old “post_product_variation” relations from icl_translations table and creates new ones (and the ones which were already missing – that’s where the problem is) using pure PHP mysqli connection rather than any WordPress/WooCommerce functionalities so it doesn’t depend on those if they had any bugs causing the out of sync situation in first place.

  1. Try WooCommerce Multilingual built-in troubleshooting feature at WooCommerce > WooCommerce Multilingual > Status > Troubleshooting > check all the boxes except duplicate term (or check it if you need) and Start. Might happen it already solves the chaos or for some but not all products.
  2. Download the script to your WordPress site root directory where there is also wp-config.php (required). You’ll find the .zip at the end of this post. Make backups and read the script code first, use at your own risk! Yes, it deletes old relations from the DB, so have a backup. Run the script directly (it is not a plugin).
  3. Go back to point 1 and Sync variables products again. This step is necessary!
  4. (Bonus) Now the variation quantities should be already synced. However if the quantities are now same in different languages but you edit (or order) a variation quantity and it doesn’t update the other translations:
    1. WPML > Translation Management > Multilingual Content Setup. Custom Field Translation > Show system fields, _stock has to be listed and set to “copy”.
    2. If _stock is not listed nor set to “copy” examine WooCommerce > WooCommerce Multilingual > Status for _stock. And plugins/woocommerce-multilingual/wpml-config.xml file and optionally same file in your theme folder. Edit XML to “copy” and Restore default at WPML > Languages > Reset settings.
    3. Everything ok but still not synchronizing automatically? Add the following code to your themes functions.php, requires points 4.1 and 4.2 to be resolved (_stock listed and set to “copy”):

// WooCommerce Sync variation quantity in translations
add_action('updated_post_meta', 'wc_sync_variation_quantity_in_translations', 10, 4);
function wc_sync_variation_quantity_in_translations($meta_id, $post_id, $meta_key, $meta_value)
{
    if ('_stock' == $meta_key) {
		do_action('wpml_sync_custom_field', $post_id, $meta_key);
	}
}
// /

Tested/solution for:
* WooCommerce 2.6.14, the latest WooCommerce2 version.
* WooCommerce Mutlilingual 4.2.0
* WPML Translation Management 2.2.6

Even if the script was written for WooCommerce2 this kind of trouble exists on newer 3+ versions also. Still give it a try.

Add custom fields under wp general settings

To add custom fields under wordpress general settings without hacking core code use this as example. Of course you can make it better and write some fancy class for doing this.

add_filter('admin_init', 'my_general_settings_register_fields');

function my_general_settings_register_fields()
{
	register_setting('general', 'my_field', 'esc_attr');
	add_settings_field('my_field', '<label for="my_field">'.__('My Field' , 'my_field' ).'</label>' , 'my_general_settings_fields_html', 'general');
}

function my_general_settings_fields_html()
{
	$value = get_option( 'my_field', '' );
	echo '<input type="text" id="my_field" name="my_field" value="' . $value . '" />';
}

WordPress custom menu display

Just a small example of how to build custom menu output in WordPress.

$menu_args		= array('menu' => 'Main menu', 'echo' => 0) ;
$menu_string	= wp_nav_menu($menu_args);
$xml	= new SimpleXMLElement($menu_string);
$out	= false;

foreach ($xml->ul[0] as $li) 
{
	$classes = array('current-page-ancestor', 'current-menu-item', 'current-menu-parent');
	if (strstr($li['class'], 'current-page-ancestor') || 
		strstr($li['class'], 'current-menu-item') || 
		strstr($li['class'], 'current-menu-parent')) 
	{
		$out = $li->ul;      
		$i = 0;
		if (isset($out->li) && !empty($out->li))
		{
			$out['id']		= 'sub_menu';
			$out['class']	= 'clear';
			foreach ($out->li as $li_sub) 
			{      
				if (!strstr($li_sub['class'], 'current-page-ancestor') && 
					!strstr($li_sub['class'], 'current-menu-item') && 
					!strstr($li_sub['class'], 'current-menu-parent')) 
				{
					unset($out->li[$i]->ul);
				}
				$i++;
			}
		}
		break;;
	}
}
if (!empty($out)) {
	 echo $out->asXML(); 
}

Disable wordpress core updates display

Ever had a situation where you have given admin right to the client?  What usally happens is that client will update the wordpress core. If we are talking about custom solutions, its most definitely not safe to update the core. Here’s a solution for disabling  the opportunity to update WordPress core.

add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );

# 2.8 to 3.0:
remove_action( 'wp_version_check', 'wp_version_check' );
remove_action( 'admin_init', '_maybe_update_core' );
add_filter( 'pre_transient_update_core', create_function( '$a', "return null;" ) );

# 3.0:
add_filter( 'pre_site_transient_update_core', create_function( '$a', "return null;" ) );

XHTML SEO friendly sample header

Standard solution for developing WordPress header. SEO friendly and can be copied every time new template is created.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<title><?php wp_title('|', true, 'right'); ?> <?php bloginfo('name'); ?></title>
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<?php wp_head(); ?>
<link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_directory'); ?>/img/styles.css" media="all" />
<link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_directory'); ?>/img/styles_screen.css" media="screen, projection" />
<link rel="shortcut icon" type="image/ico" href="<?php bloginfo('stylesheet_directory'); ?>/favicon.ico" />
<script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery.js"></script>
<script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/scripts.js"></script>
</head>