Skip to content

Instantly share code, notes, and snippets.

@dazecoop
Last active April 16, 2024 02:57
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save dazecoop/548b2621e86fb030da8e5adb1bfe484f to your computer and use it in GitHub Desktop.
Save dazecoop/548b2621e86fb030da8e5adb1bfe484f to your computer and use it in GitHub Desktop.
WooCommerce product attributes as selectable options without variations
<?php
/**
* List available attributes on product page in a drop-down selection
*/
add_action('woocommerce_before_add_to_cart_button', 'list_attributes_on_product_page');
function list_attributes_on_product_page() {
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
if ($product->is_type( 'variable' )) {
return;
}
echo '<div style="padding-bottom:15px;">';
foreach ( $attributes as $attribute ) {
$taxonomy = get_taxonomy($attribute['name']);
$options = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'all' ) );
$label = str_replace('Product ', '', $taxonomy->label);
?>
<div style="padding-bottom:8px;">
<label for="attribute[<?php echo $attribute['id']; ?>]"><?php echo $label; ?></label>
<br />
<select required name="attribute[<?php echo $attribute['id']; ?>]" id="attribute[<?php echo $attribute['id']; ?>]">
<option value disabled selected>Choose an option</option>
<?php foreach ( $options as $pa ): ?>
<option value="<?php echo $pa->name; ?>"><?php echo $pa->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<?php
}
echo '</div>';
}
/**
* Add selected attributes to cart items
*/
add_filter('woocommerce_add_cart_item_data', 'add_attributes_to_cart_item', 10, 3 );
function add_attributes_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$attributes = $_POST['attribute'];
if (empty( $attributes ) ) {
return $cart_item_data;
}
$cart_item_data['attributes'] = serialize($attributes);
return $cart_item_data;
}
/**
* Display attributes in cart
*/
add_filter( 'woocommerce_get_item_data', 'display_attributes_in_cart', 10, 2 );
function display_attributes_in_cart( $item_data, $cart_item ) {
if ( empty( $cart_item['attributes'] ) ) {
return $item_data;
}
foreach (unserialize($cart_item['attributes']) as $attributeID => $value) {
$attribute = wc_get_attribute($attributeID);
$item_data[] = array(
'key' => $attribute->name,
'value' => $value,
'display' => '',
);
}
return $item_data;
}
/**
* Add attribute data to order items
*/
add_action( 'woocommerce_checkout_create_order_line_item', 'add_attributes_to_order_items', 10, 4 );
function add_attributes_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( empty( $values['attributes'] ) ) {
return;
}
foreach (unserialize($values['attributes']) as $attributeID => $value) {
$attribute = wc_get_attribute($attributeID);
$item->add_meta_data( $attribute->name, $value );
}
}
@alexemstudio
Copy link

THANK YOU! Exactly what I needed. Works like a charm!

@dazecoop
Copy link
Author

THANK YOU! Exactly what I needed. Works like a charm!

You're very welcome!

@DilieCat
Copy link

Can I just inlcude this in the functions.php in my theme? Because when I include this and add attributes on my product it doesn't show up as a dropdown.

@dazecoop
Copy link
Author

dazecoop commented Feb 18, 2021

Can I just inlcude this in the functions.php in my theme? Because when I include this and add attributes on my product it doesn't show up as a dropdown.

Yes, you should be able to throw all this code into functions.php and it'll just work. This was built with Storefront WooCommerce theme - off the top of my head, I can't remember if that may have anything to do with why it may not be working for you. The hook on line 7 may have to be tweaked perhaps.

@jamajamajaaama
Copy link

Can it be adapted for variable products too? I need to show for all variable products the month when to start deliver the variable product. I created a Variables, but it shows the dropdown only for non variable products, If I will remove this
if ($product->is_type( 'variable' )) {
return;
}
Will there be any other issues?

@sevenkader
Copy link

Can this be configured or amended to work with the Custom Attributes option as well? Is there anything I can add to it for it to work with Custom Attributes

@gedipa
Copy link

gedipa commented Dec 14, 2021

Hello, how can I just display one specific attribute instead of all found?

@tomer330
Copy link

tomer330 commented Mar 1, 2022

Worked fine until wordpress 5.9.
After I updated wordpress the attributes are not required, users add products to the cart without selecting any attribute.
Please make a fix for it.

@tflight
Copy link

tflight commented Nov 16, 2022

I tweaked this a bit to work with the current version of WooCommerce, changed the HTML output to more closely match the WooCommerce default for attributes, allowed translation of the "Choose an option" text, prefixed the function names to make collisions less likely, and added some sanitation to the submitted $_POST data. (More work is likely needed there.)

/**
 * List available attributes on product page in a drop-down selection
 */
add_action('woocommerce_before_add_to_cart_button', 'attr_wo_var_list_attributes_on_product_page');
function attr_wo_var_list_attributes_on_product_page()
{
    global $product;
    $attributes = $product->get_attributes();

    if (! $attributes) {
        return;
    }

    if ($product->is_type('variable')) {
        return;
    }

    echo '<table class="variations" cellspacing="0" role="presentation"><tbody>';

    foreach ($attributes as $attribute) {
        $options = $attribute->get_options();
        ?>
        <tr>
            <th class="label">
                <label for="attribute[<?php echo sanitize_title($attribute->get_name()); ?>]"><?php echo $attribute->get_name(); ?></label>
            </th>
            <td class="value">
                <select required name="attribute[<?php echo $attribute->get_name(); ?>]" id="attribute[<?php echo $attribute->get_name(); ?>]">
                    <option value disabled selected><?php echo __('Choose an option', 'woocommerce') ?></option>
                    <?php foreach ($options as $option) : ?>
                        <option value="<?php echo $option; ?>"><?php echo $option; ?></option>
                    <?php endforeach; ?>
                </select>
            </td>
        </tr>
        <?php
    }

    echo '</tbody></table>';
}

/**
 * Add selected attributes to cart items
 */
add_filter('woocommerce_add_cart_item_data', 'attr_wo_var_add_attributes_to_cart_item', 10, 1);
function attr_wo_var_add_attributes_to_cart_item($cartItemData)
{
    $attributes = $_POST['attribute'];

    if (empty($attributes)) {
        return $cartItemData;
    }

    $validatedAttributes = [];
    foreach ($attributes as $attrKey => $attrValue) {
        $validatedAttributes[sanitize_text_field($attrKey)] = sanitize_text_field($attrValue);
    }

    $cartItemData['attributes'] = serialize($validatedAttributes);

    return $cartItemData;
}

/**
 * Display attributes in cart
 */
add_filter('woocommerce_get_item_data', 'attr_wo_var_display_attributes_in_cart', 10, 2);
function attr_wo_var_display_attributes_in_cart($itemData, $cartItem)
{
    if (empty($cartItem['attributes'])) {
        return $itemData;
    }

    foreach (unserialize($cartItem['attributes']) as $attribute => $value) {
        $itemData[] = array(
            'key'     => $attribute,
            'value'   => $value,
            'display' => '',
        );
    }

    return $itemData;
}

/**
 * Add attribute data to order items
 */
add_action('woocommerce_checkout_create_order_line_item', 'attr_wo_var_add_attributes_to_order_items', 10, 3);
function attr_wo_var_add_attributes_to_order_items($item, $cartItemKey, $values)
{
    if (empty($values['attributes'])) {
        return;
    }

    foreach (unserialize($values['attributes']) as $attribute => $value) {
        $item->add_meta_data($attribute, $value);
    }
}

@kiranthory
Copy link

Thank you! Exactly what I needed.

@tflight
Copy link

tflight commented Aug 15, 2023

Something I've noticed is that when customers view an order from their account and click the 'Order again' button, these attributes are not added to the item. I'm trying to work around this now through the woocommerce_order_again_cart_item_data hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment