在类别页面上显示产品变体

时间:2014-09-13 18:03:36

标签: woocommerce

如何在WooCommerce类别页面上显示所有产品变体?

我已经设置了一个包含三种变体的变量产品。我希望每个变体(不是变量产品本身)在产品类别页面上显示为单独的产品。用户应该能够打开变量产品并将其添加到购物车中。

这可能吗?

3 个答案:

答案 0 :(得分:2)

示例:http://bennyhendrikx.be/product-categorie/clothing/page/2/

在youre functions.php中添加此代码

   if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {

  function woocommerce_template_loop_add_to_cart() {
    global $product;

    if ($product->product_type == "variable" && (is_product() || is_product_category() || is_product_tag())) {
      woocommerce_variable_add_to_cart();
    }

    else {
      woocommerce_get_template( 'loop/add-to-cart.php' );
    }
  }

}

答案 1 :(得分:0)

add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_variation', 5);
function woocommerce_template_single_variation() {
            global $product;
            if ($product->product_type == "variable" && (is_product_category() || is_product_tag())) {
              echo woocommerce_variable_add_to_cart();
            }

 }

答案 2 :(得分:0)

嗨,您只需将此代码粘贴到主题函数中即可。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
}