WordPress Woocommerce post id - wordpress

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.

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.
}
}

Woocommerce Product Vendor extension - Loading ACF fields into a vendor's store front

I am using ACF to add fields to my vendors' dashboard profile pages. I currently have a test ACF field loading the field from only the WP Admin profile page on all the vendors' product listing page using this simple hook in my child theme's functions.php:
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() { ?>
<?php if(get_field('founded_on')) { ?>
<?php the_field('founded_on'); ?>
<?php }
}
Perhaps I'm pulling the wrong hook, but I can't seem to find the right hook in the Product Vendor frontend page.
I need help customizing it so that when you are on a vendor's product page, it pulls the ACF fields from that particular vendor's profile. Currently, it partially works; however it only pulls the WP main admin's data for all the different vendors'.
I know there is a way to pull the vendor's ID for each particular vendor's page and have it load their data for their page, but my php knowledge is very limited. I usually just hunt for existing code and tweak it. Unfortunately I haven't found any solutions that have worked, and this is the closest I've come to getting custom fields to work on a vendor's page.
Or if anyone can point me to a better solution to allow me to create customer fields for a vendor to fill out that will be loaded on their front end page, that would be great. I've tried Nicola Mustone's solution ( here ), which would have been perfect, except I couldn't get it to load the new custom fields on the vendor's store profile form page, nor have it load the fields into that vendor's storefront page. Based on comments, it only shows up for the site's Admin and only they can edit it. There's no visible way to have it load on the storefront, which defeats the purpose.
I imagine that the providers are users with a certain level within the WordPress system?
Considering that this is your case, the ACF fields need some additional parameters to become visible:
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
$value1 = the_field( 'my_field', $post_id );
$value2 = get_field( 'my_field', $post_id );
take some examples found in the ACF documentation, but in your particular case you have to pass the user's ID
the_field('founded_on', 'user_' . $user->ID );
echo get_field('founded_on', 'user_' . $user->ID );
function documentation the_field()
You need to pull the user ID of the user and then use the format user_{$user->ID} for the post ID as the second parameter of the ACF field.
If I understand your question, then this should work.
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() {
$user = wp_get_current_user();
if ( get_field( 'founded_on', 'user_' . $user->ID ) ) {
the_field( 'founded_on', 'user_' . $user->ID );
}
}

Woocommerce product category filter dosn't update ACF

There is custom filed created to display second place for Seo text just under products on product category archive pages.
When entering category from menu bar - the different description in ACF correctly changes according to chosen category.
The problem is - WHEN - we are in any of these product categories and want to use product category filter from the side bar. Then nothing is changing. Category changes but description from ACF stays from previous category. That should work like this, we ar in A category, but want to filter products with B category filter, after applying that filter there should be updated page with b category products and updated ACF Seo description. How can we resolve that?
My code:
add_action( 'woocommerce_after_main_content', 'my_extra_description' );
function my_extra_description() {
global $wp_query;
apply_filters( 'acf/update_value', $value, $post_id, $field, $original );
# get the query object
$category = $wp_query->get_queried_object();
#get the cat ID
$category_ID = $category->term_id;
#get the field
$contenu_categorie = get_field( 'contenu_categorie', 'category_'.$category_ID );
#if we have data, show it
if( $contenu_categorie ){
echo $contenu_categorie;
}
}
Plase help :)

Retrieve Checkout Add-Ons Field Value

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/

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).

Resources