How to get cart item key from order item in WooCommerce - php

I have created product addons for products in my cart. I am associating each product with an image saved to the filesystem, so when you go to view an order you can see the image that was created with the product.
When an image is saved, the cart item gets a custom key. This custom key is used in a cookie to carry the path to the image through the checkout process
if (!function_exists('force_individual_cart_items')) {
function force_individual_cart_items( $cart_item_data, $product_id ){
$unique_cart_item_key = md5( microtime().rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
if (isset($_COOKIE['CustomImagePath'])) {// if image path exists that needs to be saved
$imagePath = $_COOKIE['CustomImagePath']; // get image path
$imagePaths = (isset($_COOKIE['ImagePaths']) ? json_decode(stripslashes(html_entity_decode($_COOKIE['ImagePaths'])), true) : array());
$imagePaths[$unique_cart_item_key] = $imagePath; //asscoiate image path with product cart key
setcookie('ImagePaths', json_encode($imagePaths),0,'/'); // save association in image paths cookie
unset($_COOKIE['CustomImagePath']);
setcookie('CustomImagePath', null, -1, '/');
}
return $cart_item_data;
}
}
add_filter( 'woocommerce_add_cart_item_data','force_individual_cart_items', 10, 2 );
When the order is created I then add a new row into the woocommerce_item_meta table with the meta_key of "Custom Image". The issue I am running into is associating the order item with the cart_item_key.
(in the woocommerce_thankyou hook)
global $wpdb, $wp;
$query = "SELECT order_item_id FROM wp_woocommerce_order_items WHERE order_id = $order_id";
$result = $wpdb->get_results($query, true);
$imagePaths = json_decode(stripslashes(html_entity_decode($_COOKIE['ImagePaths'])), true);
foreach ($result as $order_item) {
$item_id = $order_item->order_item_id;
}
$cart_item_custom_key = NOT AVAILABLE IN THE ORDER ITEM
$filePath = $_COOKIE[$cart_item_custom_key];
$wpdb->insert('wp_woocommerce_order_itemmeta', array('order_item_id'=>$post->ID, "meta_key"=>'Custom Image', 'meta_value'=>$filePath))
For example let's say a user selects 3 images. In the cart the first product will have a custom key of 1, the second will have a custom key of 2, and the third a custom key of 3. Using
$woocommerce->cart->get_cart_contents()[cart_item_key]['unique_key'];
I can get that unique key while I have access to the cart. Once the order is created however, order_item does not have that key. Order one, two, and three no longer have custom keys. So I cannot get the image from the cookie with the associated key.
$filePath = $_COOKIE[ ? KEY NO LONGER EXISTS ? ];
$wpdb->insert('wp_woocommerce_order_itemmeta', array('order_item_id'=>$post->ID, "meta_key"=>'Custom Image', 'meta_value'=>$filePath));
is there a way to retrieve the key that that cart item had, because the order item does not seem to have it?

If you just need to get cart item key, save it as custom hidden order item meta data using:
add_action('woocommerce_checkout_create_order_line_item', 'save_cart_item_key_as_custom_order_item_metadata', 10, 4 );
function save_cart_item_key_as_custom_order_item_metadata( $item, $cart_item_key, $values, $order ) {
// Save the cart item key as hidden order item meta data
$item->update_meta_data( '_cart_item_key', $cart_item_key );
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
To get this cart item key from the order item you will use something like:
// Get the WC_Order Object from order ID (if needed)
$order = wc_get_order( $order_id );
// Loop though order items
foreach ( $order->get_items() as $item ){
// Get the corresponding cart item key
$cart_item_key = $item->get_meta( '_cart_item_key' );
}
My opinion: You are making things much more complicated than they should be:
Instead of using cookies, you should better use WC_Sessions (if this is really needed). But it should be better to add all required data in hidden input fields on the product page…
You should better set all required data as custom cart item data when the product is added to cart with woocommerce_add_cart_item_data hook (so no need of cookie or something else).
Also you should not use woocommerce_thankyou action hook to add order item custom metadata for many reasons as there is specifically dedicated hooks for that: Once the order is created you can use woocommerce_checkout_create_order_line_item action hook, to save your custom cart item data as order item meta data.
With your provided code I can't help more than that for instance.

Related

Show extra sections on a product page ONLY to users who already purchased that product (Elementor, Crocoblock, JetEngine)

I have a website with few WooCommerce products, and the product page is created with Elementor Pro. I want users to be able to click Add to Cart and buy the product normally.
But then when the user goes back to the product page (after buying the product), they would not see the the Add to Cart button (if will be hidden for those that purchased the product) and they will see another extra section.
How can I do this?
I also have Crocoblock and JetEngine that have a Dynamic Visibility functionality built in Elementor.
The easiest way to achieve this would be by creating a child theme. The child theme should contain 2 added functionalities.
For this to fully work you will need all users to be registered and NOT have checkout as a guest enabled. Also, this will only work for new orders. If you want to add compatibility for old orders you'd have to create a script that sets the meta value for all users that have purchased this product before the code was implemented.
First of we create a function that adds metadata to the purchasing user account if the product was in the cart. Add the following code to your child theme functions.php
add_action( 'woocommerce_thankyou', 'custom_add_user_meta');
function custom_add_user_meta( $order_id ){
$user_id = get_current_user_id();
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id == 12345 ) {
add_user_meta( $user_id, '_product_xyz_purchased', "true");
}
}
}
Now we add the following code to the same functions.php file to render additional content on the single product page of WooCommerce. NOTE: This step is for a more complete overview that can be implemented outside of Crocoblocks/Jetegine. The following step can be skipped because Jetengine allows setting conditions based on user_meta
add_action("woocommerce_after_single_product", 'render_additional_content', 50);
function render_additional_content() {
$user_id = get_current_user_id();
$product_purchased = get_user_meta( $user_id, '_product_xyz_purchased', true);
if($product_purchased == "true"){
echo "here you can put the HTML you want to display";
}
}

How to update WooCommerce order item quantity

I need to update the order item meta in a woocommerce oder on checkout page or while woocommerce creates the order.
I'm using the plugin visual product configurator and it is not passing the right quantity of some items of the order to woocommerce order meta, especially when I use multiple variations on the same product.
Is there a hook for me to use to update the item quantity for a certain order item and how can I use it?
The plugin returns me an array with all the cart information and I can only check if an item of the order appears multiple times - if yes I need to change the quantity of that item to that number in the woocommerce order/database.
I was thinking of adding the following hook to my functions.php
add_action('woocommerce_checkout_create_order', 'change_qty', 1,1);
function change_qty($item_qty){
foreach($item_qty as $qty) {
$qty['product_id'] = $id;
$qty['qty'] = $new_qty
$order->update_meta_data('quantity', $new_qty, $id)
}
}
Whereas $item_qty is be an multi-dimensional array containing the item_ids and adjusted quantities.
Another problem I'm facing is that I dont know when I need to call that function because I get the array from the plugin on the checkout page, but I think WooCommerce has not yet created an order at that moment?
The result should be an adjusted item quantity in the woocommerce order summary in the backend.
To update the order item quantity, you can use WC_Order_Item_Product set_quantity() method.
The correct hook to update order items (line items) is woocommerce_checkout_create_order_line_item action hook, that is triggered during order creation, before data is saved to databased.
add_action('woocommerce_checkout_create_order_line_item', 'change_order_line_item_quantity', 10, 4 );
function change_order_line_item_quantity( $item, $cart_item_key, $cart_item, $order ) {
// Your code goes below
// Get order item quantity
$quantity = $item->get_quantity();
$new_qty = $quantity + 2;
// Update order item quantity
$item->set_quantity( $new_qty );
}
The function arguments (variables) are defined and usable:
$item is the WC_Order_Item_Product Object (not saved yet to database)
$cart_item_key is the related cart item key
$cart_item is the related cart item data
$order is the WC_Order Object (not saved yet to database)
Related:
Get Order items and WC_Order_Item_Product in Woocommerce 3
WC_Order_Item_Product Class and methods API Documentation
WC_Checkout and woocommerce_checkout_create_order_line_item action hook located in the create_order_line_items() method
This can help you (we hook into payment completed notification from the payment provider). If you want to update the _qty just after the order was created, I can change my function. But for now I would update it only when the payment was successful.:
/**
* Update order item qty after payment successful
*/
add_filter( 'woocommerce_payment_complete_order_status', 'update_order_item_qty', 10, 2 );
function update_order_item_qty( $order_status, $order_id ) {
//Get the order and items
$order = new WC_Order( $order_id );
$items = $order->get_items();
//New qty
$new_qty = 0;
foreach ( $items as $item_id => $item_data ) {
update_meta_data( '_qty', $new_qty, $item_id );
}
}
Please try if this is what you'r looking for.

Auto populate excluded products in WooCommerce coupons

I am looking for a way to not have to manually exclude products from coupons based upon the product ID that the coupon should apply to. Thus, build an array of all product IDs in the shop that will then populate the excluded products field minus the product ID (or IDs) that the coupon does apply to.
I was thinking to use "Disable coupons & discounts from applying to defined Woocommerce products in cart" answer making some changes to it, but as I am new to WP/WC queries and functions, I didn't get something functional yet.
Try the following, where:
you will define in the first function the array of product ids to be excluded from coupons.
the 2nd function will remove included products ids from the array of excluded product ids and will set that in the coupon when saved.
The code:
function my_coupons_excl_product_ids() {
// HERE set in the array your product IDs to be excluded
return array(17, 37, 52, 123, 124, 152, 154);
}
// On coupon save
add_action('woocommerce_coupon_options_save', 'action_coupon_options_save_callback', 10, 2);
function action_coupon_options_save_callback( $post_id, $coupon ) {
$included_ids = (array) $coupon->get_product_ids();
if( size_of($included_ids) > 0 ) {
$excl_product_ids = array_diff( my_coupons_excl_product_ids(), $included_ids ); // Get the difference
$coupon->set_excluded_product_ids( array_filter( array_map( 'intval', (array) $excl_product_ids ) ) );
$coupon->save();
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.

Prevent customers from ordering from more than 3 Vendors in Woocommerce

I have found similar solutions to this, for example: "How to limit orders to one category". I have tried to modify the code but it's not specific enough.
In my case, each Vendor is defined by a product attribute value, 8 Terms at all. If the cart contains products from more than 3 different Terms, I need to set up a message that says "Sorry, you may only order from 3 different Vendors at a time".
This is what I'm using as a starting point:
add_action( 'woocommerce_add_to_cart', 'three_vendors' );
function three_vendors() {
if ATTRIBUTE = SELECT A VENDOR; NUMBER OF TERMS > 3 {
echo "Sorry! You can only order from 3 Vendors at a time.”;
}
}
The middle line is me filling in the blanks using non-php language.
I am looking for a way to define the amount of variations in the cart. If this is not possible to do with Attributes, I am open to use Categories instead.
Does anyone know how to do this?
Update: Is not possible to manage variations with woocommerce_add_to_cart_valisation hook
Instead we can use a custom function hooked in woocommerce_add_to_cart filter hook, removing the last added cart item when more than 3 vendors (items) are in cart:
// Remove the cart item and display a notice when more than 3 values for "pa_vendor" attibute.
add_action( 'woocommerce_add_to_cart', 'no_more_than_three_vendors', 10, 6 );
function no_more_than_three_vendors( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// Check only when there is more than 3 cart items
if ( WC()->cart->get_cart_contents_count() < 4 ) return;
// SET BELOW your attribute slug… always begins by "pa_"
$attribute_slug = 'pa_vendor'; // (like for "Color" attribute the slug is "pa_color")
// The current product object
$product = wc_get_product( $variation_id );
// the current attribute value of the product
$curr_attr_value = $product->get_attribute( $attribute_slug );
// We store that value in an indexed array (as key /value)
$attribute_values[ $curr_attr_value ] = $curr_attr_value;
//Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// The attribute value for the current cart item
$attr_value = $cart_item[ 'data' ]->get_attribute( $attribute_slug );
// We store the values in an array: Each different value will be stored only one time
$attribute_values[ $attr_value ] = $attr_value;
}
// We count the "different" values stored
$count = count($attribute_values);
// if there is more than 3 different values
if( $count > 3 ){
// We remove last cart item
WC()->cart->remove_cart_item( $cart_item_key );
// We display an error message
wc_clear_notices();
wc_add_notice( __( "Sorry, you may only order from 3 different Vendors at a time. This item has been removed", "woocommerce" ), 'error' );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and should works for you. It will check for the attribute values regarding your specific attribute and will remove last added cart item for more than 3 attribute values.
The only annoying thing is that I can't remove the classic added to cart notice for the moment. I will try to find another way…

Displaying product custom fields values in the order once processed

On woocommerce, I am using the code below to render some product custom fields, on cart and on checkout:
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['wccpf_enter_product_id'] ) ) {
$diamond = $cart_item['wccpf_enter_product_id'];
$pacolor = get_the_terms($diamond, 'pa_color');
foreach ( $pacolor as $pacolor ) {
$color= $pacolor ->name;
}
$custom_items[] = array( "name" => "color", "value" => $color);
}
return $custom_items;
}
How can I display that custom product fields wccpf_enter_product_id' value in orders?
Thanks.
You can use a custom function hooked in woocommerce_add_order_item_meta action hook to achieve this.
You will need first to add an attribute in your products to get a "readable clean label" for your custom field value that is going to appear as order items meta data.
For that you have to create an attribute first and then set it in your product with any value (as you will replace this value in the code below).
See HERE some more explanations about that process…
You will have to replace in my code the 'custom_field_key' by your specific custom key that you will find on wp_woocommerce_order_itemmeta MySQL table for the corresponding item ID for your specific Order ID.
To find the corresponding item ID for the order, you can search in wp_woocommerce_order_items MySQL table with the Order ID…
You will have also to set your correct attribute slug instead of 'pa_your_attribute' to display in your orders the correct label text for this custom field value.
(see below other similar answers references).
So your code will be something like this:
// ADD THE INFORMATION AS META DATA SO THAT IT CAN BE SEEN AS PART OF THE ORDER
add_action('woocommerce_add_order_item_meta','add_and_update_values_to_order_item_meta', 1, 3 );
function add_and_update_values_to_order_item_meta( $item_id, $item_values, $item_key ) {
// Getting your custom product ID value from order item meta
$custom_value = wc_get_order_item_meta( $item_id, 'custom_field_key', true );
// Here you update the attribute value set in your simple product
wc_update_order_item_meta( $item_id, 'pa_your_attribute', $custom_value );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work
Related answers:
Adding user custom field value to order items details
Add custom Product data dynamically as item meta data on the Order
Displaying custom product data in Order items view

Categories