We are setting up an admin page that shows information about Woocommerce orders that the club treasurer needs to see. I have done this before with another client of mine that you guys helped with a lot and I was able to emulate everything done for that client on this effort. But we have one additional need here that I can't figure out.
For the previous client, you got us custom code to display the titles of the products ordered within a Woocommerce order. That's working perfectly here.
What I need now is the addition of a field that will show the selection on a product attribute that needs to be seen. I can't figure out how to do that.
Our view is displayed here:
hidden link
But this page is set to private so only admins can see it. You'd have to have me send login credentials for you to see it if necessary. It may not be necessary here.
The view we have set up so far is here:
hidden link
So if we can just get the product attribute(s) chosen for the product(s) in each order, we'll have exactly what we need here.
Hi there,
We do not have access to the admin and the page so you will need to give us the login information. I'd appreciate it if you could give me the URL/User/Pass of your WordPress dashboard after you make sure that you have a backup of your website.
It is absolutely important that you give us a guarantee that you have a backup so if something happens you will have a point of restore.
Make sure you set the next reply as private.
But please consider that we will not be able to give you custom code as it is outside of our support scope:
https://toolset.com/toolset-support-policy/
Having said that, we will do our best to nudge you on the correct path.
To retrieve the attributes (variations) of the products in a WooCommerce order, you'll need to iterate through the order items and use the available WooCommerce methods.
Here is an example code snippet to do this in a WordPress PHP file or a custom plugin. Please adapt it as necessary for your specific use case.
// Assume $order_id is the ID of the order you are interested in.
$order_id = YOUR_ORDER_ID; // Replace YOUR_ORDER_ID with the actual order ID.
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_id => $item) {
// Get the product object.
$product = $item->get_product();
// Check if the product has variations.
if ($product->is_type('variable')) {
$variation_attributes = $item->get_variation_attributes();
// Display the variation attributes.
foreach ($variation_attributes as $attr_key => $attr_value) {
echo $attr_key . ': ' . $attr_value . '<br>';
}
}
}
This code retrieves an order by its ID, iterates through the items in the order, and for each item, if it is a variable product, it prints the chosen variation attributes.
The code above is an example that you can use to add proper code to your use-case. The code above is not meant to be copy-pasted as it is not out support intend to do
Thanks.
Hi there,
I added a new custom code in Toolset settings named "retrieve-order-attributes" and her is the code:
add_shortcode('get_ordered_product_attributes', 'get_ordered_product_attributes_func');
function get_ordered_product_attributes_func() {
$order_id = do_shortcode('[wpv-post-id]');
$order = wc_get_order( $order_id ); //returns WC_Order if valid order
$items = $order->get_items(); //returns an array of WC_Order_item or a child class (i.e. WC_Order_Item_Product)
foreach( $items as $item ) {
//returns the type (i.e. variable or simple)
$type = $item->get_type();
//the product id, always, no matter what type of product
$product_id = $item->get_product_id();
//a default
$variation_id = false;
//check if this is a variation using is_type
if( $item->is_type('variable') ) {
$variation_id = $item->get_variation_id();
}
return $item['pa_membership-type'];
}
}
It creates a shortcode that I added in the view named [get_ordered_product_attributes]
It retrieves the order and the product and then finds the pa_membership_type for that product and returns the result.
Thanks.
Excellent! Thanks so much!
One thing left to ask is to see if it can use the attribute name instead of the slug. This is highly minor, but if we use the name the attribute will be capitalized on the first letter instead of lowercase. If this is difficult to do, don't worry about it as this shows what we really need to see. But if it's a matter of just quickly changing one setting, getting the name version would be better just from a being nitpicky on aesthetics perspective.
Hi there.
I went to the view and added a style (inline CSS) to capitalize the words. Here is the code:
<td style="text-align: center; text-transform: capitalize;">[get_ordered_product_attributes]</td>
text-transform: capitalize; is the part that did the magic.
Thanks.
Perfect! Thanks again! I wasn't even thinking in terms of something that easy. 😉
My issue is resolved now. Thank you! Awesome job as always.