V
V
vagono2020-05-06 20:16:04
css
vagono, 2020-05-06 20:16:04

How to add variations to the catalog page?

Good day!

The task is as follows: display product variations on the category/catalog page, as well as a button for selecting their number.

The following helped to display the variations

code in functions.php
/**
 * Replace add to cart button in the loop.
 */
function iconic_change_loop_add_to_cart() {
  remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
  add_action( 'woocommerce_after_shop_loop_item', 'iconic_template_loop_add_to_cart', 10 );
}
 
add_action( 'init', 'iconic_change_loop_add_to_cart', 10 );
 
/**
 * Use single add to cart button for variable products.
 */
function iconic_template_loop_add_to_cart() {
  global $product;
 
  if ( ! $product->is_type( 'variable' ) ) {
    woocommerce_template_loop_add_to_cart();
    return;
  }
 
  remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
  add_action( 'woocommerce_single_variation', 'iconic_loop_variation_add_to_cart_button', 20 );
 
  woocommerce_template_single_add_to_cart();
}
 
/**
 * Customise variable add to cart button for loop.
 *
 * Remove qty selector and simplify.
 */
function iconic_loop_variation_add_to_cart_button() {
  global $product;
 
  ?>
  <div class="woocommerce-variation-add-to-cart variations_button">
    <button type="submit" class="single_add_to_cart_button button"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
    <input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
    <input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
    <input type="hidden" name="variation_id" class="variation_id" value="0" />
  </div>
  <?php
}


And the following helped to display the number of goods
code in functions.php
/**
 * Добавляем поле количества на страницу архивов.
 */
function custom_quantity_field_archive() {
  $product = wc_get_product( get_the_ID() );
  if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() ) {
    woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
  }
}
add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 0, 9 );
/**
 * Добавляем необходимый JavaScript.
 */
function custom_add_to_cart_quantity_handler() {
  wc_enqueue_js( '
    jQuery( ".post-type-archive-product" ).on( "click", ".quantity input", function() {
      return false;
    });
    jQuery( ".post-type-archive-product" ).on( "change input", ".quantity .qty", function() {
      var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );
      // Для работы добавления в корзину с помощью AJAX
      add_to_cart_button.data( "quantity", jQuery( this ).val() );
      // Для работы добавления в корзину БЕЗ AJAX
      add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() );
    });
  ' );
}
add_action( 'init', 'custom_add_to_cart_quantity_handler' );


I got the following: on a variable product there is no field for entering a quantity, but in a regular product it is, as it should be.
It is necessary that the variable product also has a quantity input field. Please help!5eb2f02c8ba07313725689.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question