-
-
Save lukecav/ec4345a2cfb1d9bb7e5eabd2d8fb7611 to your computer and use it in GitHub Desktop.
woocommerce-bulk-discount 2.4.5 performance improvements in gather_discount_coeffs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Performance improved gather_discount_coeffs function | |
| * code adapted from woocommerce-bulk-discount 2.4.5 | |
| * by Nils Magnus Englund <nme@stepone.no> | |
| * | |
| * NOTE: The code below is only compatible with WooCommerce 3.0 as-is. | |
| * Should be easy enough to backport. | |
| * | |
| * Two modifications: | |
| * - Cache result from quantity calculation for each product variation. | |
| * This value will necessarily be the same for each variation. | |
| * - Avoid nested unnecessary calls to get_product where just the ID will do. | |
| */ | |
| /** | |
| * Gather discount information to the array $this->discount_coeffs | |
| */ | |
| protected function gather_discount_coeffs() { | |
| global $woocommerce; | |
| $cart = $woocommerce->cart; | |
| $this->discount_coeffs = array(); | |
| $cached_quantity_by_parent_id = array(); | |
| if ( sizeof( $cart->cart_contents ) > 0 ) { | |
| foreach ( $cart->cart_contents as $cart_item_key => $values ) { | |
| $_product = $values['data']; | |
| $quantity = 0; | |
| if ( get_option( 'woocommerce_t4m_variations_separate', 'yes' ) == 'no' && $_product instanceof WC_Product_Variation && $this->get_parent($_product) ) { | |
| $_parent = $_product->get_parent_id(); | |
| $_id = $_product->get_id(); | |
| if (!isset($cached_quantity_by_parent_id[$_parent])) { | |
| // Loop through all products in the cart again to group all products with the same parent ID as this one | |
| $qt = 0; | |
| foreach ($cart->cart_contents as $valuesInner) { | |
| $p = $valuesInner['data']; | |
| if ($p instanceof WC_Product_Variation && $p->get_parent_id() == $_parent) { | |
| $qt += $valuesInner['quantity']; | |
| } | |
| } | |
| $cached_quantity_by_parent_id[$_parent] = $qt; | |
| } | |
| $quantity = $cached_quantity_by_parent_id[$_parent]; | |
| } else { | |
| $quantity = $values['quantity']; | |
| } | |
| $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] = $this->get_discounted_coeff( $this->get_product_id($_product), $quantity ); | |
| $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] = $_product->get_price(); | |
| $this->discount_coeffs[$this->get_actual_id( $_product )]['quantity'] = $quantity; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment