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.js
file 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.php
file is a variation of thecart-item-data.php
template. 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.php
to pass the$cart_item_key
param toWC_Cart::get_item_data()
. UpdateWC_Cart::get_item_data()
to accept theparam $cart_item_key
and 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.php
ifWC_Cart::update_variations_enabled()
returnstrue
. AddWC_Cart::set_variations()
to handle updatingattributes => term
s andvariation_id
s, similar toWC_Cart::set_quantity()
. - Update WC_Form_Handler::update_cart_action(). This will allow it to check if
variation_id
is set. Ifvariation_id
is set, then get thevariation_id
field 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.js
file ifis_cart()
returnstrue
. UpdateWC_Frontend_Scripts::get_script_data()
output an array ofCART_ITEM_KEY => AVAILABLE_VARIATIONS
pairs 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_variations
checkbox used for enable the "Update Cart Item Variations" feature.