shopp_register_collection()

registers a smart collection of products

shopp_register_collection ( $name )

@param string $name Class name of the smart collection
@return void

Description

registers a smart collection of products

Note: This register function should generally be called on the shopp_storefront_init action.

It is recommended that your Smart collection extend the SmartCollection class. Also, remember to designate a slug or list of alternative slugs for your Smart Collection as a static property named $slugs, as this will be used to build your collection permastruct and rewrite tag. To provide a public facing name for your smart collection, used in page titles and menu links, provide a static function name() that returns a string.

At a minimum, your class must extend the ProductCollection class to register it using this function. See core/model/Collection.php for some examples.

If registration is successful, you can use shopp('storefront.{your-slug}-products') to load your collection for use as the working collection context. That sets up the Shopp runtime environment with your smart collection and enables shopp('collection…') Theme API tags to work using your collection.

Note that shopp('catalog') has been deprecated in favor of shopp('storefront'), and shopp('category') has been deprecated in favor of shopp('collection')

You will also be able to request your collection by URL. For instance, if you registered the collection with the slug first-choice for a store url of `http://example.com/store/“, you would be able to access your first-choice collection at:

http://example.com/store/collection/first-choice/.

Example

Registering your smart collections, perhaps in your theme’s functions.php file:

add_action('shopp_init', 'my_smart_collections');

function my_smart_collections() {
    shopp_register_collection('MyFirstSmartProductCollection');
    shopp_register_collection('MySecondSmartProductCollection');
    shopp_register_collection('HalfDeadGaghAndYamokSauce');
}

class MyFirstSmartProductCollection extends SmartCollection {

    public static $slugs = array('first-choice');

    public static function name () {
        return 'My First Products';
    }

    public function smart () {
        // load products with ids 1,2,3
        $this->loading = array(
            'where'=>array( "0 < FIND_IN_SET(p.ID, '1,2,3')" )
        );
    }

}

class MySecondSmartProductCollection extends SmartCollection {

    public static $slugs = array('second-choice');

    public static function name () {
        return 'My Second Products';
    }

    public function smart () {
        // load products with ids 4,5,6
        $this->loading = array(
            'where'=>array( "0 < FIND_IN_SET(p.ID, '4,5,6')" )
        );
    }

}

class HalfDeadGaghAndYamokSauce extends SmartCollection {

    public static $slugs = array('reasons-to-kill-mog');

    public static function name () {
        return 'Got Self-Sealing Stembolts?';
    }

    public function smart () {

        $stock_level = min(100, shopp_setting('lowstock_level') * 2.5);

        $this->loading = array(
            'where'=> array("s.stock >= $stock_level")
        );

    }

}

Loading and using your smart collection in your catalog.php template:

<?php shopp('storefront.reasons-to-kill-mog-collection'); ?>

See Also

You must be logged in to post a comment.

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

Skip to toolbar