Search code examples
phpwordpresswoocommerceurl-routingurl-parameters

WooCommerce Composite Products: How to use URL parameters to change the default product option for a component?


I'm using the WooCommerce Composite Products plugin in my WordPress/WooCommerce website and would like to be able to control what the default chosen product option is for just the first component via URL parameters.

For example, say I create a composite product that allows you to build a skateboard (deck component + wheels component + trucks component). If I create a link that says "Build your own Skateboard using this deck", that link should have URL parameters of some sort that pre-selects the right deck out of the available deck options upon opening the skateboard composite product (the link's URL might look something like this: www.mydomain.com/shop/skateboard-composite-product?deck-option=red-deck , where that last bolded part of the URL pre-selects the given deck option. The WooCommerce Composite Products plugin has the ability to select a default product option for any component. So I'd assume targeting that and changing the default option to whatever you say in the URL is the right way of tackling this.

I'm a bit of a noob at coding, especially php. Does anyone have an idea of how to accomplish this? WordPress/WooCommerce/plugins are all updated to latest versions as of this posting date. I'm using the Storefront theme for my shop.

Thanks in advance!


Solution

  • I figured it out myself, and it's pretty simple. For those who may be wondering how to do it:

    If you create a composite product and then add it to the cart and view it in the cart, the composite product in the cart will have a link that will take you back to the composite product page with it pre-filled with all your previous choices. That link's URL will have parameters like this: ?wccp_component_selection[1495658223]=8 , where the number after the equal sign (8 in this case) is the product ID of the product that you'd like pre-selected upon opening the composite product page. I'm not exactly sure what the other large number is (1495658223), but I think it might be the component's id or something-doesn't really matter though as you will be able to see it after you add a composite to the cart.

    Note: you can have multiple parameters in one URL in order to pre-select products for multiple different components. Again, just add a composite to the cart to see what that URL would look like.