Trying to access order items details - wordpress

I'm trying to write a function to display different custom thank you pages according to which product you purchased. The shop is simple, you can make a one-time donation or register for monthly donations to a non-profit. I want to display different thank you pages depending on whether which you did. I have several monthly donation products and one for one-time, separated into differnt categories. What I want to do is get the category of the products just purchased (one can only order either, not both, at a time), and use that to display the correct message.
function get_order_cat($order_id) {
$order = wc_get_order($order_id);
$items = $order->get_items();
print_r($items); // Returns the data, but it's protected !
}
This returns an array of objects, but the data is :protected. Any tips on how to access this, or get the result I'm looking for another way?

you can retrieve categories with that :
$order = wc_get_order($order_id);
foreach ($order->get_items("line_item") as $id_line_item => $item) {
$product_ID = $item->get_product_id();
$categories = get_the_terms($product_ID, "product_cat");
}

Related

woocommerce payfast split payment to merchant id based on product purchased?

based off this link Payfast split payments which appears to work. How does one run a multi vendor woocommerce store and then split the payment to the associated merchant based on the product purchased?
Typically, this information is stored at the order item level. So, you need to loop through the items, get the vendor id and do your splitting payment action.
E.g. like this on the admin order page:
global $order;
$items = $order->get_items();
foreach ( $items as $item_key => $item ) {
$vendor_id = wc_get_order_item_meta( $item_key, '_vendor_id', true );
// Now get the merchant id with $vendor_id (=user id) and do split payment.
// You can also create an array with $merchant_id => $item->get_subtotal()
// and do the split payment after the loop.
}
The meta key "_vendor_id" depends on your plugin - I used WCMP (Multivendor Marketplace Solution for WooCommerce – WC Marketplace). You may need to look it up in your database how the field is called.

WooCommerce - How to check if an order has physical products?

I need to determine if the order is an order that needs shipping of physical goods.
So I can check if the order has physical goods in it or if the order has only digital/virtual products.
How can I do this in the best way?
I get all the items of the order with:
$order = new WC_Order($order_id);
$items = $order->get_items();
You could do something like this. You need to loop through the order items, and get the product ids to use the method WC_Product::is_virtual();
$order = new WC_Order($order_id);
foreach ($order->get_items() as $order_item){
$item = wc_get_product($order_item->get_product_id());
if (!$item->is_virtual()) {
// this order contains a physical product do what you want here or return false
}
}

How to retrieve the latest categories assigned to a post currently under editing?

I need to limit the post tile length of a post belonging to a specific category while editing. So I need to check what categories have been assigned to the post under editing and decide whether to limit or not its post title.
I use "wp_insert_post_data" to do the job
add_filter( 'wp_insert_post_data' , 'limit_title_length' , '99', 2 );
But what I found is that the categories returned from passed $postarr are existing categories. Not the latest categories. So it would not work for new post or if categories being changed while editing.
$post_category = $postarr['post_category'];
I also checked get_the_category() inside the function, and that also returns existing categories, not the latest categories if category assignment changed.
My codes so far...
function limit_title_length( $data, $postarr ) {
// set up variables like max length, category id to limit
...
// get post id, title and categories from $data and $postarr passed
$title = $data['post_title'];
$id = $postarr['ID'];
$post_category = $postarr['post_category'];
// check if the specified category exists in the categories assigned to this post
...
// process $title, reset $post_title in $data
...
$data['post_title'] = $title;
return $data;
}
add_filter( 'wp_insert_post_data' , 'limit_title_length' , '99', 2 );
wp_insert_post_data fires in the very late stage of post publishing. I expected to get the latest categories from $postarr['post_category'];
but it's not in my case here. Any solutions or alternatives?
So just to be clear - You want to identify the most recent category added to a post?
If this is the case you will be hard pressed to do so as Wordpress does not save meta for added categories. If you are skilled enough you could script such a function and save the data in a custom table. Then retrieve it for use.
If you know the name of the category you are looking for you can use has_category! .

How can I get the order date, in WooCommerce?

I can see inside class-wc-admin-cpt-shop_order.php there are some functions that are pulling together the order information for display in WooCommerce. However, I don't see anywhere where the date can be used ...
Because WooCommerce uses wp_posts to store the data, can I assume that the post_date field is the correct one to use?
Also, anyone know whether there is a function in WooCommerce to get this, or whether there is a way of getting the date to come out in class-wc-admin-cpt-shop_order.php.
// Get $order object from order ID
$order = wc_get_order( $order_id );
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
Source: https://businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
Additionally
$order->get_date_created();
Is the "order date", which you can change within WooCommerce ("Edit Order")
You can use the WC_Order object, if you have the order ID:
$order = new WC_Order($order_id);
$order_date = $order->order_date;
Order properties should not be accessed directly. Best way is $order->get_date_completed()

How do I get product category information using collections in Magento

I am trying to output all the products from our Magento shop - the following code works, however I also need to grab the category id & the parent category name too. Can anyone suggest how I can do this?
$product = Mage::getModel('catalog/product');
$productCollection = $product->getCollection()
->addAttributeToSelect('*');
foreach ( $productCollection as $_product ) {
echo $_product->getName().'<br/>';
}
In some instances $_product->getCategory() can return empty and cause an error.
A better solution is to fetch categories by ID:
$categoryIds = $_product->getCategoryIds();
foreach($categoryIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
echo $category->getName();
echo $category->getUrlPath();
}
Since products can be assigned to multiple categories, I think your concept may be a bit off unless you are loading a collection for each category. What do you anticipate seeing if there are multiple categories for a given product?
Regardless, from within a category page, you can use the following:
$currentCat = $_product->getCategory();
To get all categories to which this product belongs:
$categories = $_product->getCategoryCollection();
foreach($categories as $_category) {
// do something
}
Hope that helps. Thanks,
Joe

Resources