0

I’m having trouble displaying variations in a custom built template, each time i call the wpsc function have_variation_groups() within my loop i get the following php error

commerce/wpsc-includes/product-template.php on line 1419 [22-Nov-2012 23:27:39] PHP Fatal error: Call to a member function have_variation_groups() on a non-object in /home/tofapost/public_html/sandbox/wp/wp-content/plugins/wp-e-commerce/wpsc-includes/product-template.php on line 1419.

have_variation_groups() is being called inside a WP_Query loop like so;

$args = array('post_type' => 'wpsc-product', 'posts_per_page' => -1);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();

   ... 

<?php if (wpsc_have_variation_groups()) { ?>
<fieldset>
    <legend>
        <?php _e( 'Product Options', 'wpsc'); ?>
    </legend>
    <div class="wpsc_variation_forms">
        <table>
            <?php while (wpsc_have_variation_groups()) : wpsc_the_variation_group();
            ?>
            <tr>
                <td class="col1">
                    <label for="<?php echo wpsc_vargrp_form_id(); ?>">
                        <?php echo wpsc_the_vargrp_name(); ?>:</label>
                </td>
                <?php /** the variation HTML and loop */?>
                <td class="col2">
                    <select class="wpsc_select_variation" name="variation[<?php echo wpsc_vargrp_id(); ?>]"
                    id="<?php echo wpsc_vargrp_form_id(); ?>" <?php while (wpsc_have_variations())
                    : wpsc_the_variation(); ?>
                        <option value="<?php echo wpsc_the_variation_id(); ?>" <?php echo wpsc_the_variation_out_of_stock();
                        ?>>
                            <?php echo wpsc_the_variation_name(); ?>
                        </option>
                        <?php endwhile; ?>
                    </select>
                </td>
            </tr>
            <?php endwhile; ?>
        </table>
    </div>
    <!--close wpsc_variation_forms-->
</fieldset>
<?php } ?> 

    ...

endwhile;

Whats strange is other wpsc functions like, wpsc_the_product_id() and wpsc_product_has_stock() work while no functions related to variations do...

Any help appreciated

Thanks

1 Answer 1

0

If you look at the default wpsc-products_page.php template, it uses

while (wpsc_have_products()) :  wpsc_the_product();

instead of have_posts() and the_posts() to setup the loop.

The wpsc_the_product() function sets up a global $wpsc_variations object that the product variation functions use.

I think in order to use wpsc_the_product() you need your query to be the global $wp_query, but you can setup $wpsc_variations yourself after you open your loop:

while ($loop->have_posts()) : $loop->the_post();
    global $wpsc_variations;
    $wpsc_variations = new wpsc_variations( get_the_ID() );

That hopefully should get all the product variation functions working.

This is the full wpsc_the_product() function. It also sets up global $wpsc_custom_meta, which could potentially have a similar affect on custom meta related functions, so that is something else you might need to consider.

/**
 * wpsc the product function, gets the next product,
 * @return nothing
 */
function wpsc_the_product() {
    global $wpsc_custom_meta, $wpsc_variations;
    the_post();
    $wpsc_custom_meta = new wpsc_custom_meta( get_the_ID() );
    $wpsc_variations = new wpsc_variations( get_the_ID() );
}

Hope this helps.

1
  • brillant, thanks djrmom! i spent a day(+) trying to figure this out. It seems so obvious after reading your answer. Creating a new instance of wpsc_variations() did the trick. i'm using the indispensable 'ACF' plugin for all custom meta, so i don't believe wpsc_custom_meta will be needed.
    – Cam
    Nov 26, 2012 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.