Retrieve Checkout Add-Ons Field Value - woocommerce

I'm using WooCommerce Checkout Add-Ons plugin to allow customers to attach an image and a caption to their order but can't seem to access that information in my theme. Right now I have:
$order_id = get_the_ID();
$order = new WC_Order( $order_id );
$order_items = $order->get_items();
foreach( $order_items as $order_item ):
printr( $order_item );
endforeach;
The trouble is that it's only returning the first line item, in this case the product name. There's two other line items stored here though:
order_item_id order_item_name order_item_type order_id
1 Open Category Entry line_item 329
2 Image Upload fee 329
3 Image Caption fee 329
(in wp_woocommerce_order_items)
How can I retrieve the rest?
What I need to go on to do is recover the value of _wc_checkout_add_on_value from the wp_woocommerce_order_itemmeta table.

Having had a look at the source I found that $order->get_items() accepts a $types parameter and defaults to type='line_item' if none is specified. I added 'fee' to my call for items and got exactly what I was looking for.
http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderget_items/

Related

Run a custom code after a product is sold in woocommerce

I need to know how to assign custom actions (or run custom code) when someone buys something in the website and the payment has been completed. I need this so our system can send order's data to an API.
i am using wordpress and woocommerce for the website.
how can i find the order dynamics variable to put it in my API's code ?
if there,s anyone who use wordpress and woocommerce, please tell me where can i find order's dynamic variables such as:
product id which the customer has been ordered
product count and etc.
You could write a function that connects to the woocommerce_order_status_processing hook. At this point, the payment has been accepted and WooCommerce is waiting for the store to fulfill the order.
add_action( 'woocommerce_order_status_processing', 'my_order_complete_function', 10, 1 );
function my_order_complete_function( $order_id ) {
$order = wc_get_order( $order_id );
foreach($order->get_items() as $item) {
$product_id = $item->get_product_id();
$product = wc_get_product( $product_id );
// Add your API call here.
}
}

Display Woocommerce variable product dimensions

Have Woocommerce setup with a range of variable products. In the variable tab I've setup a unique price, image, description, weight & dimensions for each item.
All variable data displays as expected on the front-end except the dimensions & weight.
Despite hours of searching, I cannot find any documentation, tutorials, hints on how to hook into it.
Have Woocommerce templates setup and know that I will need to hook into the do_action( 'woocommerce_single_variation' ); in variable.php.
Anyone know how to get each variable's dimensions & weight to display beneath the variable description?
If you have the variation ID, you can use it to create a new WC_Product(). This object will then have properties available on it for the $length, $width, and $height. See the docs here (at the bottom under "Magic Properties").
To get the variations for a given product, you can use the global $product and then the get_available_variations() function.
global $product
$variations = $product->get_available_variations();
foreach ( $variations as $variable_array ){
$variation = new WC_Product( $variable_array['variation_id'] );
echo "The length is {$variation->length}.";
}
If you want to display additional information regarding your variable product add this function to your child theme’s function.php (or plugin). You’ll probably want to alter the html tags to fit your theme:
add_filter( 'woocommerce_product_additional_information', 'tim_additional_tab', 9 );
function tim_additional_tab( $product ){
$variations = $product->get_available_variations();
//print the whole array in additional tab and examine it
//echo '<pre>';
//print_r($variations);
//echo '</pre>';
//html and style to your likings
foreach ( $variations as $key ){
echo $key['image']['title'].'<br>';
echo $key['weight_html'].'<br>';
echo $key['dimensions_html'].'<br>';
}
}

Woocommerce: How do I generate unique IDs for each purchased item?

I'm working in WordPress 4.3.1., with WooCommerce 2.4.7. For my employer's current store project, we need to have unique IDs generated for each item purchased, which can never be the same as any other purchase.
For example, if someone buys 3 shirts of the same design and size, each of these shirts, once in the cart, should each have a unique item ID displayed. Regardless of the number of orders we get, the unique item IDs cannot be duplicated.
As for the ID, I was considering mixing the product SKU with the date, and possibly the order ID to generate the date. How exactly would I go about this? Is there a plugin that can handle this, or should I deal in straight PHP?
I assume you want to use the unique id for work order tracking internally
use this hook (I am using processing rather then completed)
add_action( 'woocommerce_order_status_processing', 'add_unique_id' );
Then do this
function add_unique_id($order_id) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item_id => $product ) {
$gen_id = "generate id goes here";
wc_add_order_item_meta($item_id, 'unique_id', $gen_id);
}
You could use a WooCommerce hook like woocommerce_order_status_completed in order to run something after every purchase.
add_action( 'woocommerce_order_status_completed', 'custom_function' );
function custom_function($order_id) {
// completed order object (can be useful)
$order = new WC_Order( $order_id );
// run what you want here
}
I can suggest you to use a post meta or an user meta (based on your needs).

Wordpress: How do I remove the category "uncategorized" from all posts in bulk?

Scenario:
I have 1000 posts that have the "Uncategorized" category, and I want to remove "Uncategorized" from all of those posts and set a different category for those posts.
In other words– take all Uncategorized posts and move them to another category, in one fell swoop.
Can I do this in bulk without going through each post individually?
What you are looking for is the WordPress bulk editor.
Go to Posts > Categories > Uncategorized
Click the "Screen Options" tab in the top right corner, then change "Number of items per page:" to 1000. (If you are on a really slow server you might consider doing less at a time)
Now select all of the items on the page and click the "Bulk Actions" drop-down above the select all and select the "Edit” option.
Hit Apply
In the bulk editor click the “new category” you want to change all of the posts to and hit update.
Once you have added all of the posts to the “new category” you need to remove the “Uncategorized” category. To do this:
Go to Settings > Writing
Now change the “Default Post Category” to something besides “Uncategorized”
Go back to Posts > Categories and delete the “Uncategorized” category
Now you can create the “Uncategorized” category again if you like and change it back to the default.
Once you delete the “Uncategorized” category it will remove it from all of your posts.
If you have some posts that you want to remain in “Uncategorized” then create a new category called “temp” and assign all of the posts you want to remain to that category. Once you delete “Uncategorized” create it again and assign the posts in “temp” back to that category.
As you've discovered, the bulk editor only allows the ADDITION of categories to multiple posts - it's not possible to REMOVE categories from multiple posts. The best option i found was to temporarily install this plug-in https://wordpress.org/plugins/bulk-remove-posts-from-category/ (in the WP repository) which adds the ability to REMOVE categories from multiple posts using the same bulk edit method. it simply adds an additional 'remove' checkbox under the category list.
The uncategorized category has an ID of 1. What our worksflow will be is,
Get all posts which is assigned to the uncategorized category.
We will only get post ID's which will make our query up to 1000 times faster on a site with thousands of posts. This will also help that our query does not time out or hit a maximum memory fatal error.
Use wp_set_object_terms() to remove and set our new tems
NOTE:
The code below requires PHP 5.4+ and any changes will be non reversable, so back up your database first
$args = [
'nopaging' => true, // Gets all posts
'cat' => 1, // Only gets posts assigned to category 1 which is the uncategorized category
'fields' => 'ids', // Only get post ID's, make query up 1000 times faster on huge databases
];
$q = get_posts( $args );
if ( $q ) {
foreach ( $q as $v ) {
// Get all the post categories
$categories = get_the_category( $v );
$category_ids = [];
foreach ( $categories as $category ) {
// Replace all uncategorized category instances with our new category id and build new array
if ( $category->term_id == 1 ) {
$category_ids[] = (int) 21; // REPLACE WITH THE CORRECT NEW CATEGORY ID
} else {
$category_ids[] = (int) $category->term_id;
}
}
// Set our new categories to the post
if ( $category_ids ) // Unnecessary check for categories, but just in case should something fail
wp_set_object_terms( $v, $category_ids, 'category' );
}
}
Note, nowhere have we changed the $post global or setup postdata, so we don't need to call wp_reset_postdata() :-)
Wordpress stores the parent/child relationships between categories and posts in the wp_term_relationships table, which is documented here. As #Pieter Goosen noted, the "Uncategoried" category has a ID of 1. So you can backup your SQL database, then connect to it with a SQL command line client (sudo mysql wordpress works for me), and run this SQL command:
delete from wp_term_relationships where term_taxonomy_id = 1;
If you want to move all uncategorized posts to another category, you can use the Bulk Move plugin.
If you want to remove a category from some posts, Bulk remove posts from category might be a better option.
As Melebius said, it is much, much quicker (and easier) to use https://wordpress.org/plugins/bulk-remove-posts-from-category/ ! It works with products as well as posts. Brilliant!
You can add this to your theme's functions.php file, then refresh any page on your site once, then remove the function.
Use at your own risk and backup the database first! There's no UNDO button on this.
<?php
$args = array(
'posts_per_page' => -1
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
setup_postdata( $post );
$categories = get_the_category();
$catcount = count($categories);
$postid = $post->ID;
$catlist = array();
//Building a list of categories for each post and EXCLUDING "uncategorized"
foreach( $categories as $category ) {
if($category->name == 'Uncategorized') {
continue;
}
$catlist[] = $category->term_id;
}
// If there's just one category, and that category is "Uncategorized", move the category to one of your choosing
if($catcount == 1 && $categories[0]->name == "Uncategorized") {
// This is the category ID that you want to move uncategorized posts to
$catlist = array(189);
}
wp_set_object_terms( $postid, $catlist, 'category' );
endforeach;
wp_reset_postdata();
?>

WordPress Woocommerce post id

In WooCommerce, on view-order page is there a way to get post id of product (or an other column) ?
I tried to display data of $_product and $item_meta but it's displaying the id of the product created in the admin of WordPress, not the id of the current product in the order.
The following code will get you all the details of the order:
<?php
global $woocommerce;
$order = new WC_Order( $order_id );
$order->get_items();
?>
After testing all methods of woocommerce, the only way to do that is to create a custom function that takes in the database the post_title (in wp_posts) that starts with the order id.

Resources