• Resolved riew

    (@riew)


    Hi

    I am trying to add a “View product” button to the category page in WooCommerce. For variable products I would like the chosen variable (users can choose it in dropdown) to follow along, so when you click the link, you go to the chosen variant.

    I have this to show the button:

    
    function mycode_add_more_details_button_to_single() {
    	add_action( 'woocommerce_after_shop_loop_item', 'mycode_display_more_details_button_to_single' );
    }
    
    add_action( 'init', 'mycode_add_more_details_button_to_single' );
    
    function mycode_display_more_details_button_to_single() {
    
    	global $product;
    
    	$url   = $product->get_permalink();
    	$text  = __( 'Se varen', 'mycode' );
    	$title = sprintf( __( 'Se mere om %s', 'mycode' ), $product->get_name() );
    	$class = sprintf( 'button add_to_cart_button product_type_%s', $product->get_type() );
    
    	$format = '<a href="%1$s" title="%2$s" class="single_add_to_cart_button button alt">%4$s</a>';
    
    	printf( $format, esc_url( $url ), esc_attr( $title ), esc_attr( $class ), esc_html( $text ) );
    }
    

    How can I add the chosen variable id?

    I hope you can help. :o)

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @riew ,

    Thanks for reaching out!

    I do not have the exact solution with your code but you are trying to show variation option in the archive pages and then use those selections for the cart, you can try this plugin – Show Variations On Shop & Category WooCommerce

    I hope you find it useful.

    Cheers ??

    Thread Starter riew

    (@riew)

    Thanks for the suggestion! :o)

    I would prefer not to install a plugin; I have a feeling that this can be done with a few lines of code… I just need to add the value from the dropdown to my link. :o)

    I use this code to display the price of each variation in the dropdown:

    
    add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
    
    function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;
    
    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
    
    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
    
    $query = "SELECT postmeta.post_id AS product_id
    FROM {$wpdb->prefix}postmeta AS postmeta
    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
    WHERE postmeta.meta_key LIKE 'attribute_%'
    AND postmeta.meta_value = '$term_slug'
    AND products.post_parent = $product->id";
    
    $variation_id = $wpdb->get_col( $query );
    
    $parent = wp_get_post_parent_id( $variation_id[0] );
    
    if ( $parent > 0 ) {
    $_product = new WC_Product_Variation( $variation_id[0] );
    
    $itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
    //this is where you can actually customize how the price is displayed
    return $term . ' (' . $itemPrice . ')';
    }
    return $term;
    }
    

    Can’t I grab some information (chosen ID) from this dropdown and add it to the “View product” button? The code for that button is:

    
    function mycode_add_more_details_button_to_single() {
    	add_action( 'woocommerce_after_shop_loop_item', 'mycode_display_more_details_button_to_single' );
    }
    
    add_action( 'init', 'mycode_add_more_details_button_to_single' );
    
    function mycode_display_more_details_button_to_single() {
    
    	global $product;
    
    	$url   = $product->get_permalink();
    	$text  = __( 'Se varen', 'mycode' );
    	$title = sprintf( __( 'Se mere om %s', 'mycode' ), $product->get_name() );
    	$class = sprintf( 'button add_to_cart_button product_type_%s', $product->get_type() );
    
    	$format = '<a href="%1$s" title="%2$s" class="single_add_to_cart_button button alt">%4$s</a>';
    
    	printf( $format, esc_url( $url ), esc_attr( $title ), esc_attr( $class ), esc_html( $text ) );
    }
    

    Any ideas…? :o)

    Hello @riew ,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    If you need a paid solution, you can consider taking help from one of our partner’s website.

    Thank you ??

    Hi there,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Thank you ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Link to chosen variable product from category page’ is closed to new replies.