Skip to content

Instantly share code, notes, and snippets.

@webaware
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webaware/931c8c216a90a16b9c62 to your computer and use it in GitHub Desktop.
Save webaware/931c8c216a90a16b9c62 to your computer and use it in GitHub Desktop.
Add product attributes underneath the product title on an order form from Order Form for WooCommerce. Requires version 1.0.5 or higher. Save this file as a simple plugin in the WordPress plugins folder. http://orderform-woo.webaware.net.au/
<?php
/*
Plugin Name: Order Form for WooCommerce Product Attributes
Plugin URI: https://gist.github.com/webaware/931c8c216a90a16b9c62
Description: add product attributes to Order Form for WooCommerce product
Author: WebAware
Author URI: http://webaware.com.au/
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* add product attributes to Order Form for WooCommerce product
* @param WC_Product $product
* @param array $attrs
*/
add_action('orderform_woocommerce_order_item_end', function($product, $attrs) {
$attributes = $product->get_attributes();
foreach ($attributes as $attribute) {
// must be ticked as visible
if (empty($attribute['is_visible'])) {
continue;
}
// must not be a variation
if (!empty($attribute['is_variation'])) {
continue;
}
if (empty($attribute['is_taxonomy'])) {
// attribute values stored locally on the product
$class = strtolower(sanitize_title($attribute['name']));
$label = $attribute['name'];
$values = explode('|', $attribute['value']);
$values = array_map('trim', $values);
}
else {
// global product attributes, stored as terms of a taxonomy
$taxonomy = get_taxonomy($attribute['name']);
$class = sanitize_title($taxonomy->name);
$values = wp_get_post_terms($product->id, $taxonomy->name, array('fields' => 'names'));
// get tax label
if (isset($taxonomy->labels->name)) {
$label = $taxonomy->labels->name;
}
elseif (isset($taxonomy->label)) {
$label = $taxonomy->label;
}
else {
$label = $taxonomy->name;
}
}
printf('<p class="attribute-%s"><strong>%s</strong><br />', esc_attr($class), esc_html($label));
$separator = '';
foreach ($values as $value) {
printf('%s<span>%s</span>', $separator, esc_html($value));
$separator = '<br/>';
}
echo "</p>\n";
}
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment