Skip to content

Instantly share code, notes, and snippets.

@twoelevenjay
Last active July 28, 2016 00:53
Show Gist options
  • Save twoelevenjay/3b3d312b145b3c63ae6fdbccc089890d to your computer and use it in GitHub Desktop.
Save twoelevenjay/3b3d312b145b3c63ae6fdbccc089890d to your computer and use it in GitHub Desktop.
Schedule of PRs for the update variations from cart feature of WooCommerce

Schedule of PRs for the update variations from cart feature of WooCommerce

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.

PR step 1

  1. Add new files. Don't touch core at all. The cart-update-variation.js file takes the available varitions that are loaded with wp_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. The cart-update-variations-item-data.php file is a variation of the cart-item-data.php template. Instead of showing the current variations associated attributes as static text, it uses wc_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 PR step 1
  2. Update cart.php to pass the $cart_item_key param to WC_Cart::get_item_data(). Update WC_Cart::get_item_data() to accept the param $cart_item_key and pass it to the template by adding it to $item_data[]. Add the conditional WC_Cart::update_variations_enabled() that returns true if the "update variations from cart" feature is enabled from the WooCommerce checkout settings. Update WC_Cart::get_item_data() to load cart-update-variations-item-data.php if WC_Cart::update_variations_enabled() returns true. Add WC_Cart::set_variations() to handle updating attributes => terms and variation_ids, similar to WC_Cart::set_quantity().
  3. Update WC_Form_Handler::update_cart_action(). This will allow it to check if variation_id is set. If variation_id is set, then get the variation_id field value, and the attribute select field values. Then sanitize these values and send them to WC_Cart::set_variations().
  4. Update WC_Frontend_Scripts::load_scripts() to enqueue the cart-update-variation.js file if is_cart() returns true. Update WC_Frontend_Scripts::get_script_data() output an array of CART_ITEM_KEY => AVAILABLE_VARIATIONS pairs as params for cart-update-variation.js. file. Update woocommerce.scss / .css to include styles for .woocommerce td.product-name dl.variation dd select.
  5. Update WC_Settings_Payment_Gateways::get_settings() to include the woocommerce_enable_cart_variations checkbox used for enable the "Update Cart Item Variations" feature. PR step 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment