How do I access product attributes?

[ad_1]

        foreach ( $order->get_items() as $item_id => $item ) {
            // Get the product
            $product = $item->get_product();
            //fwrite($logfile, var_export($product, true));
            
            // Check if product exists and has attributes
            if ( $product && $product->has_attributes() ) {
                // Get the product attributes
                $attributes = $product->get_attributes();
                                
                // Loop through each attribute
                foreach ( $attributes as $attribute ) {
                    // Check if attribute is 'Delivery' and is visible
                    $attrname = strtolower($attribute->get_name());
                    fwrite($logfile, $attrname);
                    if ( $attribute->get_visible() && strtolower($attribute->get_name()) == 'class / delivery' ) {
                        // Get the attribute values
                        $values = $attribute->get_options();

                        // Check if any value equals 'Delivery'
                        foreach ( $values as $value ) {
                            $valuename = strtolower($value);
                            fwrite($logfile, $valuename);
                            if ( strtolower($value) == 'delivery' ) {
                                $delivery_attribute_found = true;
                                break;

                            }
                        }
                    }
    
                    if ($delivery_attribute_found) {
                        break;
                    }
                }
            }

Hello, the above code snippet is from the filter woocommerce_email_additional_content_customer_processing_order.

I am able to access the product object but am unable to access the product attributes. Can someone please advise me on how to correct my code?

Thank you.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer