Calculate multiple prices in XML file and import only one in WP All Import
I’m having trouble in XML file that contains about 10k products and i want to import them to woocommerse with WP All import. There is one node for price i.e.
<price>92.50</price>
, one node for currency
<currency>EUR</currency>
, but problem is 3 values for currencies "EUR", "USD" and "BGN". I want to import only BGN currency and others to calculate and convert to BGN. Rate for EUR is 1 EUR = 1.9583BGN, 1 USD = 1.762BGN. How to import only BGN price and calculate others to BGN in WP All Import?
I use this function in Function editor in WP All Import:
<?php $currency = array( "EUR", "USD", "BGN" ); function rate_price( $price = null, $multiplier_eur = 2, $multiplier_usd = 1 ) { if ( $currency[0] ) { // strip any extra characters from price return $price * $multiplier_eur; } else if ( $currency[1] ) { return $price * $multiplier_usd; } else { return $price; } } ?>
and this for Regular Price (BGN) xPath in wooCommerce add-on,
[rate_price({price[1]},"1.9583","1.762")]
but it’s not working, showing me only the original price for product, 92.50 EUR in the case, please, HELP!
As suggested by @Nigel Ren in the comment, xpath is the way to go in these cases.
Something along these lines:
$prods = "<units>" . "<unit><price>10.00</price><currency>EUR</currency></unit>" . "<unit><price>20.00</price><currency>BGN</currency></unit>" . "<unit><price>30.00</price><currency>USD</currency></unit>" . "</units>"; $prod_xml = simplexml_load_string($prods); $prod_dat = $prod_xml->xpath('//unit'); function rate_price($price, $cur) { if ($cur == "USD") { return $price * 3; } else if ($cur == "EUR") { return $price * 2; } else { return $price * 1; } } foreach ($prod_dat as $datum) { $unit_price = $datum->xpath('.//price')[0]; $unit_cur = $datum->xpath('.//currency')[0]; $final_price = rate_price($unit_price, $unit_cur); echo "Unit Currency: " . $unit_cur . " Orig unit price: " . $unit_price . " BGN price: " . $final_price; echo "</br>"; }
Output:
Unit Currency: EUR Orig unit price: 10.00 BGN price: 20 Unit Currency: BGN Orig unit price: 20.00 BGN price: 20 Unit Currency: USD Orig unit price: 30.00 BGN price: 90