Skip to content

Instantly share code, notes, and snippets.

@dexit
Forked from benhuson/attributes-as-options.php
Created July 2, 2025 13:09
Show Gist options
  • Select an option

  • Save dexit/5f6ea16e23b589e8281d3f71ea973c3e to your computer and use it in GitHub Desktop.

Select an option

Save dexit/5f6ea16e23b589e8281d3f71ea973c3e to your computer and use it in GitHub Desktop.

Revisions

  1. @dazecoop dazecoop revised this gist Apr 23, 2020. No changes.
  2. @dazecoop dazecoop revised this gist Apr 23, 2020. No changes.
  3. @dazecoop dazecoop renamed this gist Apr 23, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @dazecoop dazecoop created this gist Apr 23, 2020.
    94 changes: 94 additions & 0 deletions test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    <?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 );
    }
    }