• Resolved Md Abul Bashar

    (@hmbashar)


    I want to query/show woocommerce variation lists on variable product, I had a custom query using while() and I have already queried/showed some information what I need, like, price, product name, etc. Now I want to show product variation lists, quantity and add to cart button, but I can’t query that data.
    Here is my full query

       <div class="mproduct-area fix">
    
        <?php
    $mproduct = new WP_Query(array(
        'post_type' => 'product',
        'posts_per_page' => 10,
    ));
    
    if ($mproduct->have_posts()):
        while ($mproduct->have_posts()):
            $mproduct->the_post(); ?>
    
                        <!--Single Product for Mobile Page-->
                        <div class="msingle-product fix">
                            <div class="msingle-product-image">
                                <a href="<?php
            the_permalink(); ?>">
                                <?php
            if (has_post_thumbnail(get_the_ID()))
                {
                echo get_the_post_thumbnail(get_the_ID() , 'com_product_img');
                }
              else
                {
                echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
                }
    
    ?></a>
                            </div>
                            <div class="mproduct-price">
                                <h2><strong>Price : </strong> <?php
            echo $product->get_price_html(); ?></h2>                        
                            </div>
                            <div class="mproduct-name">
                                <h2><a href="<?php
            the_permalink(); ?>" title="<?php
            echo esc_attr($product->get_title()) ?>"><?php
            echo esc_attr($product->get_title()) ?></a></h2>
                            </div>
                            <div class="mproduct-add-to-cart">
                                <?php
            woocommerce_template_loop_add_to_cart(); ?> 
                                <!-- Modal -->
                                <div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel">
                                  <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                      <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="myModalLabel"><?php
            echo esc_attr($product->get_title()) ?></h4>
                                      </div>
                                      <div class="modal-body">
    
                                        <div class="msingle-product-image product_img_model">
                                            <a href="<?php
            the_permalink(); ?>">
                                            <?php
            if (has_post_thumbnail(get_the_ID()))
                {
                echo get_the_post_thumbnail(get_the_ID() , 'com_product_img');
                }
              else
                {
                echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
                }
    
    ?></a>
                                        </div>
    
                                        <div class="mproduct-price">
                                            <h2><strong>Price : </strong> <?php
            echo $product->get_price_html(); ?></h2>                        
                                        </div>
                                        <div class="product_variable_list">
    
                                    </div>
                                  </div>
                                  <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Continue Shipping</button>
                                  </div>
                                </div>
                              </div>
                            </div>
                        </div>                  
                    </div><!--/ Single Product for Mobile Page-->
    
                <?php endwhile; endif;?>
                </div>

    Please check my class=”product_variable_list” div area. I want to show variation lists in this div. I have to tried to called

    <?php wc_get_template_part('content', 'single-product'); ?>
    then show full content like single product page. https://prntscr.com/ifwopb

    But now i need to show product variation lists, quantity and add to cart button
    https://prntscr.com/ifwosm

    I had lots of research on google and woocommerce/templates/single-product/add-to-cart/variable.php page codes. But didn’t success. Please help me, how can I show that data.

    I have tried copy form code from woocommerce/templates/single-product/add-to-cart/variable.php page

    
    <?php 
    $attribute_keys = array_keys( $attributes );
    
    do_action( 'woocommerce_before_add_to_cart_form' ); ?>                      
    
    <form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_json_encode( $available_variations ) ) ?>">
        <?php do_action( 'woocommerce_before_variations_form' ); ?>
    
        <?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
            <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
        <?php else : ?>
            <table class="variations" cellspacing="0">
                <tbody>
                    <?php foreach ( $attributes as $attribute_name => $options ) : ?>
                        <tr>
                            <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
                            <td class="value">
                                <?php
                                    $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( stripslashes( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) ) : $product->get_variation_default_attribute( $attribute_name );
                                    wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
                                    echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) : '';
                                ?>
                            </td>
                        </tr>
                    <?php endforeach;?>
                </tbody>
            </table>
    
            <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
    
            <div class="single_variation_wrap">
                <?php
                    /**
                     * woocommerce_before_single_variation Hook.
                     */
                    do_action( 'woocommerce_before_single_variation' );
    
                    /**
                     * woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
                     * @since 2.4.0
                     * @hooked woocommerce_single_variation - 10 Empty div for variation data.
                     * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
                     */
                    do_action( 'woocommerce_single_variation' );
    
                    /**
                     * woocommerce_after_single_variation Hook.
                     */
                    do_action( 'woocommerce_after_single_variation' );
                ?>
            </div>
    
            <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
        <?php endif; ?>
    
        <?php do_action( 'woocommerce_after_variations_form' ); ?>
    </form>

    then show this errors.
    https://prntscr.com/ifwp0u
    I can’t understand how can i do this. please help me.

    basically i’m not expert in php, can you share code for show variation lists from variable product ?

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to query variation lists from variable products on woocommerce?’ is closed to new replies.