Skip to content

Instantly share code, notes, and snippets.

@jamesgol
Created September 5, 2017 02:51
Show Gist options
  • Save jamesgol/33eea04760b47c8bcd62132ac7108904 to your computer and use it in GitHub Desktop.
Save jamesgol/33eea04760b47c8bcd62132ac7108904 to your computer and use it in GitHub Desktop.
Allow WooCommerce variations to be added to cart via URL
add_action( 'init', 'woo_add_to_cart_fixup' );
function woo_add_to_cart_fixup() {
// Duplicate WooCommerce variation attributes from $_GET to $_POST because WC_Form_Handler->add_to_cart_handler_variable only looks in $_POST
if ( isset( $_GET, $_GET['add-to-cart'] ) ) {
foreach ( $_GET as $key => $value ) {
if ( false !== strpos( $key, 'attribute' ) ) {
$_POST[ $key ] = $value;
}
}
}
}
@jamesgol
Copy link
Author

jamesgol commented Sep 5, 2017

@jamesgol
Copy link
Author

jamesgol commented Sep 6, 2017

This will be unnecessary with the next WooCommerce release. PR was just merged in woocommerce/woocommerce#16715

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment