I believe these are the best steps of implementing the changes to WooCommerce needed to add the ability for customers to update the variations of products that have already been added to the cart. These steps represent individual smaller pull requests per @mikejolley.
You can see all of the changes together by comparing here.
- Add new files. Don't touch core at all. The
cart-update-variation.jsfile takes the available varitions that are loaded withwp_localize_script()and compares them to the selected values of the attribute select fields. When a match is found the it will update a hidden field with variation id. This field is used when updating the cart. This vsariation id update is fired on change of an attribute select field. Thecart-update-variations-item-data.phpfile is a variation of thecart-item-data.phptemplate. Instead of showing the current variations associated attributes as static text, it useswc_dropdown_variation_attribute_options()to display them as select fields with the products other attribute options. This template is loaded when the "update variations from cart" feature is enabled from the WooCommerce checkout settings. PR #11468 - Update
cart.phpto pass the$cart_item_keyparam toWC_Cart::get_item_data(). UpdateWC_Cart::get_item_data()to accept theparam $cart_item_keyand pass it to the template by adding it to$item_data[]. Add the conditionalWC_Cart::update_variations_enabled()that returns true if the "update variations from cart" feature is enabled from the WooCommerce checkout settings. UpdateWC_Cart::get_item_data()to loadcart-update-variations-item-data.phpifWC_Cart::update_variations_enabled()returnstrue. AddWC_Cart::set_variations()to handle updatingattributes => terms andvariation_ids, similar toWC_Cart::set_quantity(). - Update WC_Form_Handler::update_cart_action(). This will allow it to check if
variation_idis set. Ifvariation_idis set, then get thevariation_idfield value, and the attribute select field values. Then sanitize these values and send them toWC_Cart::set_variations(). - Update
WC_Frontend_Scripts::load_scripts()to enqueue thecart-update-variation.jsfile ifis_cart()returnstrue. UpdateWC_Frontend_Scripts::get_script_data()output an array ofCART_ITEM_KEY => AVAILABLE_VARIATIONSpairs as params forcart-update-variation.js. file. Update woocommerce.scss / .css to include styles for.woocommerce td.product-name dl.variation dd select. - Update WC_Settings_Payment_Gateways::get_settings() to include the
woocommerce_enable_cart_variationscheckbox used for enable the "Update Cart Item Variations" feature.


