Hi everyone,
I'm using Woocommerce and I'm running into some trouble I've been hurting my head on :)
I've customized the Woocommerce code to add an extra property to a product, basically just a string called <strong>_levnummer</strong>
Now, after a purchase is made by the customer a function called <strong>generateOrderXML</strong> runs. All this funtion does is gather the info in the order, generate an XML and send it to the supplier so they can finalize the order.
Within this function I have the code shown below:
global $woocommerce;
$order_info = new WC_Order($order_id);
$items = $order_info->get_items();
foreach($items as $item) {
$howmanyItems = $item['qty'];
$product_id = $item['id'];
$product_distnumber = get_post_meta( $product_id, '_levnummer', true );
while ($howmanyItems > 0) {
// If it's not a variation, run this.
if ($product_distnumber !== '') {
$levNummer = $product_distnumber;
$artnr = $orderXML->createElement('artnr');
$artnr = $products->appendChild($artnr);
$customerProducts = $orderXML->createTextNode($levNummer);
$customerProducts = $artnr->appendChild($customerProducts);
$howmanyItems--;
// If it's a variation, run this.
} elseif ($product_distnumber == '') {
$levNummer = "variatie";
$artnr = $orderXML->createElement('artnr');
$artnr = $products->appendChild($artnr);
$customerProducts = $orderXML->createTextNode($levNummer);
$customerProducts = $artnr->appendChild($customerProducts);
$howmanyItems--;
}
}
}
What this does is grab the order info, extract the amount of items and for each item get the <strong>_levnummer</strong> string.
This works great for <strong>Simple Products</strong>, but not for <strong>Variable Products</strong>.
<em>The while statement is for when there are more than 1 items of a product. The supplier would like to receive multiple items this way.</em>
I've managed to let the script detect when the item is a variation (hence the output of <em>variatie</em>), but all I need now is to have the <strong>_levnummer</strong> string extracted for the variation item... and I can't seem to figure it out...
Hope someone can help me!
ps; I'm quite new to PHP, this code might be disturbing and inefficient :D
<strong>pps; I've stumbled across the solution! Apparently you can use the same method as getting an the id, you can use variation_id.</strong>
My updated code that works:
global $woocommerce;
$order_info = new WC_Order($order_id);
$items = $order_info->get_items();
foreach($items as $item) {
$howmanyItems = $item['qty'];
$product_id = $item['id'];
$var_id = $item['variation_id'];
$product_distnumber = get_post_meta( $product_id, '_levnummer', true );
$product_varnumber = get_post_meta( $var_id, '_levnummer', true );
while ($howmanyItems > 0) {
// If it's not a variation, run this.
if ($product_distnumber !== '') {
$levNummer = $product_distnumber;
$artnr = $orderXML->createElement('artnr');
$artnr = $products->appendChild($artnr);
$customerProducts = $orderXML->createTextNode($levNummer);
$customerProducts = $artnr->appendChild($customerProducts);
$howmanyItems--;
// If it's a variation, run this.
} elseif ($product_distnumber == '') {
$levNummer = $product_varnumber;
$levNummer = "variatie";
$artnr = $orderXML->createElement('artnr');
$artnr = $products->appendChild($artnr);
$customerProducts = $orderXML->createTextNode($levNummer);
$customerProducts = $artnr->appendChild($customerProducts);
$howmanyItems--;
}
}
}
I've donated the prizemoney to the Community Pot!
Michael Caputo answers:
if you want to display the _levnummer string display for the levNumber, why are you putting "variatie", instead of calling the variable $product_distnumber?
Perhaps im' not understanding your question.
Martijn Bouwman comments:
You're right, I've should've mentioned that.
If I use $product_distnumber the variable stays empty and nothing is posted.
Michael Caputo comments:
right after this line, can you add this and tell me what the output is:
$product_distnumber = get_post_meta( $product_id, '_levnummer', true );
print_r($product_distnumber);
Martijn Bouwman comments:
I've found the solution, thanks for your replies though! I've donated the prizemoney to the Community Pot!
Michael Caputo comments:
If you dont mind sharing the solution, i'd appreciate it, I think the community would appreciate it as well.
Was the $product_distnumber variable being stored as an array?
Martijn Bouwman comments:
The solution is posted with the question (including code) as a PPS :)
Martijn Bouwman comments:
global $woocommerce;
$order_info = new WC_Order($order_id);
$items = $order_info->get_items();
foreach($items as $item) {
$howmanyItems = $item['qty'];
$product_id = $item['id'];
$var_id = $item['variation_id'];
$product_distnumber = get_post_meta( $product_id, '_levnummer', true );
$product_varnumber = get_post_meta( $var_id, '_levnummer', true );
while ($howmanyItems > 0) {
// If it's not a variation, run this.
if ($product_distnumber !== '') {
$levNummer = $product_distnumber;
$artnr = $orderXML->createElement('artnr');
$artnr = $products->appendChild($artnr);
$customerProducts = $orderXML->createTextNode($levNummer);
$customerProducts = $artnr->appendChild($customerProducts);
$howmanyItems--;
// If it's a variation, run this.
} elseif ($product_distnumber == '') {
$levNummer = $product_varnumber;
$levNummer = "variatie";
$artnr = $orderXML->createElement('artnr');
$artnr = $products->appendChild($artnr);
$customerProducts = $orderXML->createTextNode($levNummer);
$customerProducts = $artnr->appendChild($customerProducts);
$howmanyItems--;
}
}
}