How do I access product attributes?
-
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.
- The topic ‘How do I access product attributes?’ is closed to new replies.