S
S
sergeyviktorovich2021-07-22 20:56:47
WooCommerce
sergeyviktorovich, 2021-07-22 20:56:47

Woocommerce display variations of individual products?

I want to display variations in small product cards as a separate product, but when I click on it to go to the detail with all the variations, I tried it, but it didn’t work out How to display variations in individual products using php in Woocommerce?
tried it like this ? but it only works in the catalog, but in the top products slider on the main page there is no
need like /product/gradient/?attribute_pa_size=500-m&attribute_pa_gradient=orange-navy

add_action( 'woocommerce_product_query', 'custom_woocommerce_product_query' );
function custom_woocommerce_product_query ($q) {
    $q->set( 'post_type', array( 'product', 'product_variation' ) );
    return $q;
}


how to get this function

protected function parse_query_args() {
    $query_args = array(
      'post_type'           => 'product',
      'post_status'         => 'publish',
      'ignore_sticky_posts' => true,
      'no_found_rows'       => false === wc_string_to_bool( $this->attributes['paginate'] ),
      'orderby'             => empty( $_GET['orderby'] ) ? $this->attributes['orderby'] : wc_clean( wp_unslash( $_GET['orderby'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    );

    $orderby_value         = explode( '-', $query_args['orderby'] );
    $orderby               = esc_attr( $orderby_value[0] );
    $order                 = ! empty( $orderby_value[1] ) ? $orderby_value[1] : strtoupper( $this->attributes['order'] );
    $query_args['orderby'] = $orderby;
    $query_args['order']   = $order;

    if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
      $this->attributes['page'] = absint( empty( $_GET['product-page'] ) ? 1 : $_GET['product-page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    }

    if ( ! empty( $this->attributes['rows'] ) ) {
      $this->attributes['limit'] = $this->attributes['columns'] * $this->attributes['rows'];
    }

    $ordering_args         = WC()->query->get_catalog_ordering_args( $query_args['orderby'], $query_args['order'] );
    $query_args['orderby'] = $ordering_args['orderby'];
    $query_args['order']   = $ordering_args['order'];
    if ( $ordering_args['meta_key'] ) {
      $query_args['meta_key'] = $ordering_args['meta_key']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
    }
    $query_args['posts_per_page'] = intval( $this->attributes['limit'] );
    if ( 1 < $this->attributes['page'] ) {
      $query_args['paged'] = absint( $this->attributes['page'] );
    }
    $query_args['meta_query'] = WC()->query->get_meta_query(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
    $query_args['tax_query']  = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query

    // Visibility.
    $this->set_visibility_query_args( $query_args );

    // SKUs.
    $this->set_skus_query_args( $query_args );

    // IDs.
    $this->set_ids_query_args( $query_args );

    // Set specific types query args.
    if ( method_exists( $this, "set_{$this->type}_query_args" ) ) {
      $this->{"set_{$this->type}_query_args"}( $query_args );
    }

    // Attributes.
    $this->set_attributes_query_args( $query_args );

    // Categories.
    $this->set_categories_query_args( $query_args );

    // Tags.
    $this->set_tags_query_args( $query_args );

    $query_args = apply_filters( 'woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->type );

    // Always query only IDs.
    $query_args['fields'] = 'ids';

    return $query_args;
  }

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