Woocommerce产品变化分页问题

时间:2018-02-19 14:27:15

标签: wordpress woocommerce html-table pagination

我为单个产品变化做了一个表,我试着做分页。 我正在使用几乎没有自定义属性的变量产品。所以问题在于分页不起作用并显示太多页面。

例如,对于一种产品,我在第一页上有15种变体,但总数却分页显示总页数为14。此外,如果我将posts_per_page设置为示例2,则所有变体都将乘以2(重复),依此类推。

这是我的表

enter image description here

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 个答案:

答案 0 :(得分:0)

我的一个项目遇到了类似的问题。我使用jQuery DataTables插件解决了它。试着看看它是否能解决你的问题。