Woocommerce Product Variations Pagination issue

2019-08-12 18:32发布

问题:

I made a table for single product variations and I trying to do pagination. I'm using variable products with few custom attributes. So the issue is that pagination is not working and showing too many pages.

For example, for one product I have 15 variations on the first page and total but pagination shows 14 total page. Also if I set posts_per_page for example 2, then all variations are multiplied by 2 (duplicated) and so on.

This is my table

Full Code in functions.php

function woocommerce_variable_add_to_cart(){
    global $product, $post, $woocommerce;

    $attributes = $product->get_attributes();

    $variations = find_valid_variations();

    if ( get_post_meta($post->ID, 'price_grid', true) ) {
        wp_enqueue_script( 'wc-add-to-cart-variation' );

        wc_get_template( 'single-product/add-to-cart/variable.php', array(
                'available_variations'  => $product->get_available_variations(),
                'attributes'            => $product->get_variation_attributes(),
                'selected_attributes'   => $product->get_variation_default_attributes()
            ) );
        return;
    }
    ?>
    <table class="variations variations-grid" cellspacing="0">
    <thead>
        <tr>
            <td> Date</td>
            <td> Location </td>
            <td> Price </td>
            <td> Quantity </td>
            <td> Availability </td>
            <td> </td>
        </tr>
    </thead>
        <tbody>
        <?php
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

            $args = ( array(
            'post_type'     => array('product_variation'),
            'posts_per_page'=> 1,
            'post_status'   => array('private', 'publish'),
            //'post_in' => array('product'),
            'product_cat'   => '',
            'paged'   => $paged,
            'post_parent'   => get_the_ID()
            ) );
            $wp_query = new WP_Query($args);

            while( $wp_query->have_posts() ) : $wp_query->the_post();
        ?>

        <?php
            foreach ($variations as $key => $value) {
                if( !$value['variation_is_visible'] ) continue;
        ?>
            <tr>
                <td class="date">
                    <?php 
                        $i = 0;
                        foreach($value['attributes'] as $key => $val ) {
                            if($i == 0 ) {
                                echo $val;
                            }
                        $i++;
                        } 
                    ?>
                </td>
                <td class="location">
                    <?php
                        $i = 0;
                        foreach($value['attributes'] as $key => $val ) {
                            if($i !== 0) {
                                echo $val;
                            } 
                        $i++;
                        }
                    ?>
                </td>
                <td class="price">
                        <?php echo '<span>&pound;</span>' . $product->get_price(); ?>
                </td>
                <td class="quantity">
                    <?php woocommerce_quantity_input(); ?>
                </td>
                <td class="stock">
                    <?php if (!$value['is_in_stock'] ) { ?>
                      <p class="stock out-of-stock"><?php _e( 'Places Not Available', 'woocommerce' ); ?></p>
                            <?php } else { ?>
                 <p class="stock in-stock"><?php _e( 'Places Available', 'woocommerce' ); ?></p>
                </td>
                <td class="add-to-cart">
                    <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
                        <?php
                        if(!empty($value['attributes'])){
                            foreach ($value['attributes'] as $attr_key => $attr_value) {
                            ?>
                            <input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
                            <?php
                            }
                        }
                        ?>
                        <button type="submit" class="single_add_to_cart_button button alt"><span class="glyphicon glyphicon-tag"></span> Add to cart</button>
                        <input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
                        <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
                        <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />
                    </form>
                    <?php } ?>
                </td>
            </tr>
            <?php } ?>
        </tbody>
    <?php endwhile; ?>
    </table>
    <?php
        if ( function_exists( 'wp_pagenavi' ) ) {
        ?>

        <div id="pagination">
            <?php wp_pagenavi( array( 'query' => $wp_query ) ); ?>
        </div>
    <?php } ?>
    <?php wp_reset_query(); ?>
    <?php
}


function find_valid_variations() {
    global $product;

    $variations = $product->get_available_variations();
    $attributes = $product->get_attributes();
    $new_variants = array();

    foreach( $variations as $variation ) {

        $valid = true;
        foreach( $attributes as $slug => $args ) {
            if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {

            } else {
                $valid = false;

             foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
                    $attribute = trim( $attribute );
                    $new_variant = $variation;
                    $new_variant['attributes']["attribute_$slug"] = $attribute;
                    $new_variants[] = $new_variant;
                }

            }
        }

        if( $valid )
            $new_variants[] = $variation;

    }

    return $new_variants;
}

回答1:

I had a similar issue with one of my projects. I solved it using jQuery DataTables plugin. Try to see if it solves yours.