Set automatically as "featured" every purchased product in Woocommerce - php

In Woocommerce, I would like when an order is mad, to set automatically as "Featured" the purchased products (my default status for all kinds of orders is "on-hold").
So basically, if order is "on-hold", the ordered products should turn as "Featured".
Why I want this? Because I am using the "Featured" thing not in the way as intended by WC, but instead I am displaying a custom label for the sold out product (since I keep my sold out products visible in Shop for a good number of days after the purchase).
Thus, I don't want to manually mark as Featured every product once it is sold, instead I want this to be done automatically.
Could this be done using some WC hooks in my child theme's functions.php? Any help is welcome.

The following code will set to "featured" all the purchased products when the order status is set to "on-hold":
add_action('woocommerce_order_status_on-hold', 'order_status_on_hold_featured_products', 20, 2);
function order_status_on_hold_featured_products( $order_id, $order ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
$product->set_featured(true);
$product->save();
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.

Related

How to remove a custom taxonomy and add a new one after product has been ordered and is out of stock in WooCommerce?

I have set up a custom taxonomy using CPT UI in WordPress. This custom taxonomy is used to show the location of a product in our mini warehouse, the taxonomy is called "Stock Location". I want to be able to remove the Stock Location taxonomy after a product has gone out of stock but also after the order has been completed (otherwise we would lose the shelf before the product is picked) and then add a the next taxonomy which is "Sold".
I was able to find this
function action_woocommerce_no_stock( $wc_get_product ) {
// Product set tag ids
$wc_get_product->set_tag_ids( array( 40 ) );
// Save
$wc_get_product->save();
}
add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );
But I am unsure what to use as the taxonomies don't have IDs. I am relatively new to PHP so any help would be greatly appreciated. Thanks.

Add the discount total for each item as WooCommerce order item metadata

I need to add in order metadata the amount of the discount for each product individually. For example, There are ten products in the order. The user has applied a coupon discount to the products. The total amount of the discount was calculated (if there are products on_sale, they were not taken into discount calculate).
But in the end, we got a total discount. And I need to split this discount for all the products that received it and even write this value into order item metadata.
Why don't I even have a sample code? I just don't know where to start. The only thing I think is to take the total discount, divide it by the number of products that can have a discount (these are all products in the order except on_sale), And add the resulting number to the product meta. But there is a problem, I'm not sure if this is the right solution. Could you please share any advice on how to solve this?
You can start with woocommerce_checkout_create_order_line_item action hook where you will be able to set custom order item meta data. This hook has 4 arguments:
$item the order item (object),
$cart_item_key the cart item key (string),
$values the cart item (array),
$order the order (object).
So you can get from the cart item $values the discounted line item totals using:
$line_discount = $value['line_subtotal'] - $value['line_total'];
$line_discount_tax = $value['line_subtotal_tax'] - $value['line_tax'];
Then for example all together to save the line discount total as custom order item meta data:
// Save Line item discount as custom order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'action_checkout_create_order_line_item', 10, 4 );
function action_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
$line_discount = $value['line_subtotal'] - $value['line_total'];
$line_discount_tax = $value['line_subtotal_tax'] - $value['line_tax'];
$item->update_meta_data( '_line_discount', $line_discount + $line_discount_tax );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Then you can get it from an order item object $item using the WC_Data method get_meta() like:
$line_discount = $item->get_meta('_line_discount');
Related: Get Order items and WC_Order_Item_Product in WooCommerce 3

How to remove cart items from WooCommerce Session for a specific category

I am working on WooCommerce based Website. I have many products in it and all products are assigned to different product categories.
Now I have the requirement to completely remove products of one the category say for eg "woo-cat".
There are many custom plugins and theme in which this category's id/slug is used conditionally , So I decided not to remove category , products of that category or related code of that category.
But I redirect every possible occurrence of URL to the shop page , in which that category is include
Like I redirect - Single page of products , Category Listing Page , also hide from live search and so on ...
My problem is if any user have added products from that category in the cart , and just close browser without purchase , then it will remain in cart session , how do I remove those products of that particular category that are already in cart session.
Check screen-shot below , This is appear in top of my site :
PS : I can not do like , when user login then empty cart using _woocommerce_persistent_cart_ , because the guest user can also purchase products without login and by registering at a time of checkout page.
To remove items from session from a specific product category, you will use the following:
add_filter( 'woocommerce_pre_remove_cart_item_from_session', 'pre_remove_items_from_a_specific_category', 10, 3 );
function pre_remove_items_from_a_specific_category( $remove, $key, $values ){
// Here define your product category(ies) - can be a term id, slug orname
$categories = array('t-shirts');
if ( has_term( $categories, 'product_cat', $values['product_id'] ) ) {
$remove = true;
}
return $remove;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Prepend WooCommerce Brand name to all Shipping methods names

I am trying to work out if it is possible to prepend a brand name to any shipping options available. I will be setting up a store whereby you can only order one brand at a time, I will have cart limitations built in to prevent mixing two brands together.
I have an external ordering system called Veeqo and without going into too much detail I need to prepend the brand name of the products in the cart to any shipping method selected so that I can filter orders by these shipping options. E.g.
BRAND-NAME UK Next Day
BRAND-NAME UK 3-5 Days
Can this be done ? If so, how ?
Realise I am asking a lot here but if anyone knows of a way to do this that would be much appreciated! :)
Perhaps a function which searches the first line item for "Brand" and then prepends this value to all shipping method titles. WooCommerce would only display relevant shopping methods to them depending on country etc.
Using a custom function hooked in woocommerce_package_rates filter hook, you will be able to prepend the cart item brand name to the shipping method label name.
As there are multiple ways to enable brands in WooCommerce, you will need to define the taxonomy used by the brand plugin that you have enabled in WooCommerce…
For the taxonomy to use, see: How to get the brand name of product in WooCommerce
add_filter( 'woocommerce_package_rates', 'prepend_brand_to_shipping_methods', 10, 2 );
function prepend_brand_to_shipping_methods( $rates, $package ){
// HERE define the taxonomy for product brand (depend of used plugin)
$taxonomy ='product_brand';
// Get the first cart item
$cart_item = reset($package['contents']);
// Get the product brand term name
$brand_name = wp_get_post_terms( $cart_item['product_id'], $taxonomy, ['fields' =>'names']);
$brand_name = reset($brand_name);
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ){
// Changing shipping method label name
$rates[$rate_key]->label = $brand_name . ' ' . $rate->label;
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.

Display product attributes for variations everywhere (backend + frontend + email notifications) in woocommerce 3

How do I display a product category and it's variations everywhere on orders (backend + frontend + email notifications)?
I am writing about this question:
Display product attributes for variations on cart page in woocommerce 3
I found one problem after applying these changes:
Product variations attributes as cart items shows up differently in WooCommerce
After adding a line:
add_filter ('woocommerce_product_variation_title_include_attributes', '__return_false');
It stops displaying the attributes on the back-end order page.
After removing this line, it shows the attributes on the back-end order page but stops displaying them in the cart page.
I need to get that display everywhere on orders (backend + frontend + email notifications).
Try the following:
add_filter ('woocommerce_product_variation_title_include_attributes', function( $should_include_attributes, $product ){
// Only on front-end
if( ! is_admin() )
$should_include_attributes = false;
return $should_include_attributes;
}, 20, 2 );
Code goes in function.php file of your active child theme (or active theme). It should work.

Categories