Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Programmer095/6be4bc2b2a9c649f875dfe50f29fbca2 to your computer and use it in GitHub Desktop.
Save Programmer095/6be4bc2b2a9c649f875dfe50f29fbca2 to your computer and use it in GitHub Desktop.

XPath doesn't allow you to make queries with Cyrillic symbols. Solutions:

Scenario One

If the values that you're importing do not contain commas, you can use a PHP function to query them.

File Structure

<?xml version="1.0" encoding="utf-8"?>
<products>
	<product>
		<title>Product A</title>
		<sku>PRODA</sku>
		<price>10</price>
		<param name="Рулевая колонка">Example value here</param>
		<param name="Педали">Another value here</param>
	</product>
</products>

Function Code

function map_params( $names, $values, $key ) {
	if ( empty( $key ) ) return ''; 

	$result = '';
	$names_arr = explode( ",", $names );
	$values_arr = explode( ",", $values );

	if ( ! empty( $names_arr ) ) {
		foreach ( $names_arr as $i => $name ) { 
			if ( trim( pmxi_convert_encoding( $name, "UTF-8" ) ) == $key ) {
				$result = isset( $values_arr[ $i ] ) ? trim( $values_arr[ $i ] ) : '';
				break;
			}
		}
	}
	return $result;
}

Usage example

[map_params({param/@name},{param},"Рулевая колонка")]

Scenario Two

If the values contain commas, you'll need to use the wpallimport_xml_row hook to query them.

File Structure

<?xml version="1.0" encoding="utf-8"?>
<products>
	<product>
		<title>Product A</title>
		<sku>PRODA</sku>
		<price>10</price>
		<param name="Рулевая колонка">Example, values, here</param>
		<param name="Педали">More, values, here</param>
	</product>
</products>

Code

function add_param_nodes( $node ) {
    $results = $node->xpath( '//param' );
	  $att = 'name';
    if ( !empty( $results ) ) {
        foreach( $results as $result ) {
			$atts = (string) $result->attributes();
			$atts = trim( pmxi_convert_encoding( $atts, "UTF-8" ) );
			if ( !empty( $atts ) && $atts == 'Рулевая колонка' ) {
				$node->addChild( 'Рулеваяколонка', $result->__toString() );
			} elseif ( !empty( $atts ) && $atts == 'Педали' ) {
				$node->addChild( 'Педали', $result->__toString() );
			}
		}
    }
    return $node;
}
add_filter( 'wpallimport_xml_row', 'add_param_nodes', 10, 1 );

Usage Example

This would add 2 new XPath elements that you can use in your import template:

{Рулеваяколонка[1]}
{Педали[1]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment