Can´t get custom attribute on WooCommerce product - wordpress

I try to save custom meta data after creating an order in WooCommerce. I try it with the woocommerce_new_order_item hook and in general it works. But I need to store a custom attribute from the ordered product..but I can´t get it.
What I tried:
add_action('woocommerce_new_order_item','add_basic_meta_for_new_quote',10,3); // add extra order metas
function add_basic_meta_for_new_quote($item_id, $values, $cart_item_key)
{
$angebotstext = get_post_meta($item_id, 'angebotstext', false);
wc_add_order_item_meta($item_id, 'angebotstext', $angebotstext);
}
or
add_action('woocommerce_new_order_item','add_basic_meta_for_new_quote',10,3); // add extra order metas
function add_basic_meta_for_new_quote($item_id, $values, $cart_item_key)
{
global $product;
$angebotstext = $product->get_attribute( 'pa_angebotstext' );
wc_add_order_item_meta($item_id, 'angebotstext', $angebotstext);
}
The attribute is saved here:
... and the result for meta_value is always empty, NULL or a:0:{}
Do you have any idea?
-----EDIT----
It worked with this way:
add_action('woocommerce_new_order_item','add_basic_meta_for_new_quote',10,3); // add extra order metas
function add_basic_meta_for_new_quote($item_id, $item, $order_id )
{
if ($order_id) {
$order = wc_get_order( $order_id );
}
# Iterating through each order items (WC_Order_Item_Product objects in WC 3+)
if ($order) {
foreach ( $order->get_items() as $item_id => $item_values ) {
// Product_id
$product_id = $item_values->get_product_id();
$product = wc_get_product($product_id);
$angebotstext = $product->get_attribute('Angebotstext');
if ( !empty($angebotstext) && $angebotstext != NULL ) {
wc_add_order_item_meta($item_id, 'Angebotstext' , $angebotstext);
}
}
}
}
BUT: This code adds my custom attribute two times as meta.... why that? 🤔

The main issue here is if the order contains multiple products, so you're going to face the issue of multiple updates for the same order.
To avoid that or to choose to deal with it the right way, here are some ideas:
To make this works for multiple products:
Changed this part:
wc_add_order_item_meta($item_id, 'Angebotstext _'.$product_id , $angebotstext);
This will add a new custom meta for each product by adding the product ID at the end of each meta_key and the meta_value will be the product unique value for that attribute.
so you're going to find this custom fields on the order page:
Angebotstext_123 = product attribute.
Angebotstext_485 = product
attribute. Angebotstext_951 = product attribute.
the '123', '485' are the product id and the meta value for that meta_key will be that product attribute.
To prevent it from updating multiple times:
if ($order) {
$angebotstext ='';
foreach ( $order->get_items() as $item_id => $item_values ) {
// Product_id
$product_id = $item_values->get_product_id();
$product = wc_get_product($product_id);
$angebotstext = $product->get_attribute('Angebotstext');
$prev_angebotstext = $angebotstext;
if ( !empty($angebotstext) && $angebotstext != NULL && $angebotstext != $prev_angebotstext ) {
wc_add_order_item_meta($item_id, 'Angebotstext' , $angebotstext);
}
}
}
"$prev_angebotstext" is the previous item attribute.
if ( !empty($angebotstext) && $angebotstext != NULL && $angebotstext != $prev_angebotstext )
This condition will check if the current product attribute is equal to the previous product attribute, if yes it will not update the order again.
Thank you!

Related

woocommerce conditional email template if product type in order

I try to send a customized email template when a customer has a ticket (custom product type) in cart.
I have the following:
function bc_customer_completed_order_template($template, $template_name, $template_path)
{
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = wc_get_product( $cart_item['product_id'] );
$type = get_class($product);
if ( $type == 'WC_Product_Tickets' && 'customer-completed-order.php' === basename($template) ) {
$template = trailingslashit(plugin_dir_path( __FILE__ )) . 'templates/customer-completed-order.php';
}
}
return $template;
}
add_filter('woocommerce_locate_template', 'bc_customer_completed_order_template', 10, 3);
The conditionals are working (on cart and checkout page for example), but when the order is placed, the new template is not used.
Anybody?
Your email template will look like this:
Code snippets:
// Suppose orders don't have ticket products.
$has_tickets = false;
// Loop order items.
foreach ( $order->get_items() as $item_id => $item ) {
// Get product object from order item.
$_product = $item->get_product();
// Check if the product object is valid and the class is `WC_Product_Tickets`
if ( $_product && 'WC_Product_Tickets' === get_class( $_product ) ) {
// Change the flag.
$has_tickets = true;
// Break the loop as we alreay have true flag.
break;
}
}
// Check if order have tickets items.
if ( $has_tickets ) {
// Load custom email template.
wc_get_template( 'templates/custom-customer-completed-order.php' );
// Return as we don't need the below code.
return;
}
It turned out, although the above solution is correct in its idea, in reality one cannot load a template and make use of the $order without extending the woocommerce email class.
Therefore i loaded the function inside the email template itself and made an if - else statement so for situation A the layout is different then for situation b.
Like so:
$has_tickets = false;
// Loop order items.
foreach ( $order->get_items() as $item_id => $item ) {
// Get product object from order item.
$_product = $item->get_product();
// Check if the product object is valid and the class is `WC_Product_Tickets`
if ( $_product && 'WC_Product_Tickets' === get_class( $_product ) ) {
// Change the flag.
$has_tickets = true;
// Break the loop as we alreay have true flag.
break;
}
}
// Check if order have tickets items.
if ( $has_tickets ) {
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
//custom email layout here//
}
else{
//Regular email template here
}

Hide specific products on woocommerce when ACF true/false is true in a product

I have created a custom field true/false, and I want when true is selected in a product not to be displayed in the eshop.
I want to insert the code inside the functions.php
example
if ( in_array( 'subscriber', (array) $user->roles ) || !is_user_logged_in() ) {
$postid = get_the_ID();
$prd_only_for_Customers = get_field('prd_clients', $postid); // The ACF true/false field }
Can anyone help ?
As Howard said your question is incomplete but you can use the following method to set product hidden.
You can use pre_get_posts hook in your functions.php. Since Woocommerce 3 the products visibility is now handled by the 'product_visibility' custom taxonomy for the terms 'exclude-from-catalog' and 'exclude-from-search'… See this thread or this one too.
So you should use instead the WC_Product CRUD setter methods set_catalog_visibility() this way:
function get_post_ids_by_meta_key_and_value($key, $value) {
global $wpdb;
$meta = $wpdb->get_results("SELECT post_id FROM `".$wpdb->postmeta."` WHERE meta_key='".$wpdb->escape($key)."' AND meta_value='".$wpdb->escape($value)."'");
$post_ids = [];
foreach( $meta as $m ) {
$post_ids[] = $m->post_id;
}
return $post_ids;
}
add_action('pre_get_posts', function( $query ){
if ( $query->is_main_query() && is_woocommerce() && !is_user_logged_in() ) {
$product_ids = get_post_ids_by_meta_key_and_value('prd_clients', 1);
foreach($product_ids as $id){
// Get an instance of the product
$product = wc_get_product($id);
// Change the product visibility
$product->set_catalog_visibility('hidden');
// Save and sync the product visibility
$product->save();
}
}
});
This code isn't tested, let me know if it worked or you faced any problem.
This is my final code if anyone needs something like this
// Specific products show only for Customer and administrator role
add_action('pre_get_posts', function( $query ){
$user = wp_get_current_user();
if ( $query->is_main_query() && is_woocommerce()) {
if (!check_user_role(array('customer','administrator')) || !is_user_logged_in() ) {
$product_ids = get_post_ids_by_meta_key_and_value('prd_clients', 1);
foreach($product_ids as $id){
// Get an instance of the product
$product = wc_get_product($id);
// Change the product visibility
$product->set_catalog_visibility('hidden');
// Save and sync the product visibility
$product->save();
}
}
else{
$product_ids = get_post_ids_by_meta_key_and_value('prd_clients', 1);
foreach($product_ids as $id){
// Get an instance of the product
$product = wc_get_product($id);
// Change the product visibility
$product->set_catalog_visibility('visible');
// Save and sync the product visibility
$product->save();
}
}
}
});

Struggling to get product attribute for variable product

I'm currently having an issue with WooCommerce that randomly a product attribute is or is not written to the database. Result is that in the order for some items I can't see the attributes. Hence, I thought about having an intermediate solution by dumping the cart into an email to ourselves as soon as the customer hits the "order now" button.
But doing so, I'm struggling to get the attributes right.
We're selling coffee for which I have 2 attributes: coffee_bag_size and coffee_ground_for. Both attributes are set for variations, but I have created 2 only variations based on coffee_bag_size (for 250 grammes and 500 grammes bag) with different prices whereas the coffee_ground_for can be any value. I just have to know what the customer chooses.
I read in another post that in case an attribute is not used in variations, one has to get it from the parent, so I did:
foreach ( WC()->cart->get_cart() as $cart_item )
{
$product = $cart_item['data'];
$bagsize = $product->get_attribute('pa_coffee_bag_size') ;
// For Product Variation type
if( $product->get_variation_id() > 0 )
{
$parent = wc_get_product($product->get_parent_id());
$ground = $parent->get_attribute('pa_coffee_ground_for') ;
}
// For other Product types
else
{
$ground = $product->get_attribute('pa_coffee_ground_for') ;
}
}
Getting the coffee_bag_size attribute is no problem.
Getting the coffee_ground_for is the problem. If I get it from the product it's empty, if I get it from the parent then I get a comma-separated list of all possible values. But I only need the value that was chosen. How do I do that?
I tried a few things more .... they all give me an empty string back:
$ground = $product->get_meta('pa_coffee_ground_for',true);
$ground = $cart_item['variation']['pa_coffee_ground_for'];
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute => $attribute_term )
{
$term = get_term_by('name','pa_coffee_ground_for', $attribute);
$ground = $term->term;
}
Check out slug of you attribute it may be 'pa_coffee-ground-for'
$product = $cart_item['data'];
if( $product->is_type( 'variation' ) )
{
$product = wc_get_product($product->get_parent_id());
}
$ground = $product->get_attribute( 'pa_coffee-ground-for' );
Tested and working

Stop WooCommerce For Reducing Stock On Abandoned Admin New Order

If an admin goes to create an order but abandons it, the stock level is still reduced.
Steps to reproduce:
install WordPress
install WooCommerce
create simple product and tick "manage stock?" and set stock level to 10
view on front-end (see screenshot before.png)
as admin create new order but don't save it (new -> order -> add item -> exit page)
view on front-end (see screenshot after.png)
Notice stock level has been reduced even those order wasn't saved.
Is there anyway to avoid this?
Before.png:
After.png
I have worked on this issue and wrote a basic code for that.
Hooks used: "woocommerce_order_item_add_action_buttons" for Admin Order Add Item(s) and "woocommerce_process_shop_order_meta" for Admin Order Creation/Update.
First part: Stop reducing stocks of items that added while the order has not been created.
// define the woocommerce_order_item_add_action_buttons callback
function action_woocommerce_order_item_add_action_buttons( $order ) {
$orderID = $order->ID;
//check if this is the admin manual order creation
if(get_post_status($orderID) == "auto-draft" && get_post_type($orderID) == "shop_order")
{
foreach( $order->get_items() as $item_id => $item )
{
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product_quantity = $item->get_quantity();
if($variation_id == 0)
{
$product = wc_get_product($product_id);
wc_update_product_stock($product, $product_quantity, 'increase');
}
else
{
$variation = wc_get_product($variation_id);
wc_update_product_stock($variation, $product_quantity, 'increase' );
}
// The text for the note
$note = __("Stock incremented due to the auto draft post type. Stock for each item will be decremented when this order created.");
// Add the note
$order->add_order_note( $note );
}
}
};
// add the action
add_action( 'woocommerce_order_item_add_action_buttons', 'action_woocommerce_order_item_add_action_buttons', 10, 1 );
Second Part: Reduce stocks of items that added if order is created.
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 10, 2 );
function woocommerce_process_shop_order ( $post_id, $post ) {
$order = wc_get_order( $post_id );
//check if this is order create action, not an update action
if(get_post_status($post_id) == "draft" && get_post_type($post_id) == "shop_order")
{
foreach( $order->get_items() as $item_id => $item )
{
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product_quantity = $item->get_quantity();
if($variation_id == 0)
{
$product = wc_get_product($product_id);
wc_update_product_stock($product, $product_quantity, 'decrease');
}
else
{
$variation = wc_get_product($variation_id);
wc_update_product_stock($variation, $product_quantity, 'decrease' );
}
// The text for the note
$note = __("Stock decremented for all items in this order.");
// Add the note
$order->add_order_note( $note );
}
}
}
Tested and works fine. I hope this will help you. Have a good day.

Get Woocommerce variation attributes

I am trying to get variations of a variable product on custom product page. I have two attributes, one for sizes as select and the other for colors as swatches. The problem that I cannot display the attribute that I need to display, and when I use the following code it returns a text names of sizes or colors not the select dropdown for sizes or the colors swatches. Any help please ?!
echo implode(', ', wc_get_product_terms( $product_id, 'pa_colors' ));
This is a brief code to solve your question, I let you entire code, you can use only that you need.
The first is check if get_product function exists, and check the product type, to create a correct product object with the id (in my case $idProduct).
It work on woocommerce 3.x, I don't test it on woocommerce < 3.x.
if( function_exists('get_product') ) {
$product = get_product( $idProduct );
if ( $product->is_type( 'variable' ) ) {
$product = new WC_Product_Variable( $idProduct );
$available_variations = $product->get_available_variations(); //get all child variations
$variation_variations = $product- >get_variation_attributes(); // get all attributes by variations
// taxonomy => terms
// pa_attribute-color => array('blue', 'red', green)
// Use ex: get_taxonomy('pa_attribute-color')->labels; to get the Name and not the slug to attributes, it can be the taxonomy
// Use ex: get_term_by('name', 'pa_attribute-color', 'pa_attribute-color); to get the Name/label
$result = array( $available_variations , $attributes); // only to see the result you can use var_dump, error_log, etc.
//...
//...
}elseif ( $product->is_type( 'bundle' ) && class_exists( 'WC_Product_Bundle' ) ) {
$product = new WC_Product_Bundle( $idProduct );
}else{
$product = new WC_Product( $idProduct );
}
}
Also you try with:
$product->get_attribute( $key );
wc_attribute_label($key);
where $key can be pa_color , pa_size, etc
I hope help you.

Resources