Sometimes you need a way to see which add-on a user selected in addition to their selection. This tutorial will show you how to get this information easily.

Finding the Addon Group from the Cart Loop

Finding the Addon Group from the Cart Loop

Occasionally you may have a product with several add-ons, and these add-ons might have similar or identical options.

For example, say you had a product with a t-shirt and a sweatshirt as add-ons. Conceivably, the options for both of these add-ons would overlap:

  • Small
  • Medium
  • Large

If a user only selected one of them, it would be important to know which add-on they were selecting.

In this case, if you were to use the standard shopp('cartitem','addons-list') API tag, you would find that the user selection is ambiguous both in the cart and on the resulting order. In a future perfect version of Shopp, we can be confident this will no longer be a concern, but for here and now, we need a solution.

The following code will allow you to find the addon group for each selected addon. I’m posting the full cartitem loop so you can see the context.

<?php while( shopp('cart','items') ): ?>
    <tr>
        <td>
            <a href="<?php shopp('cartitem','url'); ?>"><?php shopp('cartitem','name'); ?></a>
            <?php shopp('cartitem','options'); ?>
            <?php shopp('cartitem','inputs-list'); ?>
            <!-- BEGIN NEW ADDON CODE -->
            <?php if(shopp('cartitem','has-addons')): ?>
                <ul>
                <?php
                $item_addons = ShoppOrder()->Cart->contents[shopp('cartitem','get-id')]->addons;
                $options = shopp_meta(shopp('cartitem','get-product'),'product','options');

                foreach($item_addons as $addon) {
                    $groupname = false;
                    foreach ($options['a'] as $groups) {
                        foreach ($groups['options'] as $option) {
                            if ($option['id'] == $addon->options) {
                                $groupname = $groups['name'];
                                break; // Found it!
                            }
                        }
                    }
                    if ( $groupname )
                        echo "<li>$groupname: $addon->label</li>";
                }
                ?>
                </ul>
            <?php endif; ?>
            <!-- END NEW ADDON CODE -->
        </td>
        <td><?php shopp('cartitem','quantity','input=text'); ?>
            <?php shopp('cartitem','remove','input=button'); ?></td>
        <td class="money"><?php shopp('cartitem','unitprice'); ?></td>
        <td class="money"><?php shopp('cartitem','total'); ?></td>
    </tr>
<?php endwhile; ?>

That’s it! This code will output an unordered list just like the default addon-list code.

There are certainly cleaner, more abstract ways to implement this for the code warriors among you. One might add it as a theme function, or even better, expand the shopp() theme tag API as an example. Hopefully you find it a useful starting point.

You must be logged in to post a comment.

© Ingenesis Limited. Shopp™ is a registered trademark of Ingenesis Limited.

Skip to toolbar