• Resolved hossham2020

    (@hossham2020)


    Hello,
    I keep my issue short and straightforward.
    I am looking for a solution to select a product attribute before loading the product page. Currently, I am using URL parameters to pass data. like this :

    myshop.com/product/pro-a/attribute_pa_color=red

    But I want another solution to select attributes without using query string params.

    My scenario is to pre-select product attributes (here, just color) based on some conditions: for example, select red products by default because it’s the user’s preferred color. On the other hand, if the user’s favorite color is blue, the blue color attribute would be selected automatically for all products that might be visited.

    Any solution is highly appreciated.

    • This topic was modified 4 years, 6 months ago by hossham2020.
    • This topic was modified 4 years, 6 months ago by hossham2020.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @hossham2020 !

    You may find this article I found helpful:
    https://quadlayers.com/default-product-attributes-woocommerce/

    However, Since this requires custom coding, I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    For additional assistance on this topic, we recommend getting in touch with one of the customization experts listed on the WooCommerce Customizations Page.

    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.

    I hope this helps!

    Thread Starter hossham2020

    (@hossham2020)

    Thanks, Juan; your links inspired me to implement my solution. I like to share it here. Maybe somebody needs it as well.

    add_action('woocommerce_dropdown_variation_attribute_options_args', auto_select_color',10,1);
    
    function auto_select_color($args) 
    {
    	
    	if ($favorateColor  === "blue")
    		{
    			
    			if( 'pa_color' == $args['attribute'])
    			{
    				$args['selected'] = $args['options'][1];
                                    // Blue color index is 1; 
    			}
    			return $args;
    			
    		}
            else if ($favorateColor  === "red") {
    			if( 'pa_location' == $args['attribute'] )
    			{
    				$args['selected'] = $args['options'][0];
                                    // Red color index is 0; 
    			}
    
    			return $args; 
    		}
    	
    }
    • This reply was modified 4 years, 6 months ago by hossham2020.
    Mirko P.

    (@rainfallnixfig)

    Awesome! Thanks for sharing your solution with the WooCommerce community.

    It’ll be helpful for others facing a similar issue.

    Feel free to create a new topic if you have any further questions.

    Cheers.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Select product attribute conditionally’ is closed to new replies.