Title: amicar's Replies | WordPress.org

---

# amicar

  [  ](https://wordpress.org/support/users/amicar/)

 *   [Profile](https://wordpress.org/support/users/amicar/)
 *   [Topics Started](https://wordpress.org/support/users/amicar/topics/)
 *   [Replies Created](https://wordpress.org/support/users/amicar/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/amicar/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/amicar/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/amicar/engagements/)
 *   [Favorites](https://wordpress.org/support/users/amicar/favorites/)

 Search replies:

## Forum Replies Created

Viewing 1 replies (of 1 total)

 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[WooCommerce Bulk Discount] Flat Discount becomes Fixed Discount on variable products](https://wordpress.org/support/topic/flat-discount-becomes-fixed-discount-on-variable-products/)
 *  [amicar](https://wordpress.org/support/users/amicar/)
 * (@amicar)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/flat-discount-becomes-fixed-discount-on-variable-products/#post-10117755)
 * I noticed this ‘feature’ today as well. I edited a couple of the functions to
   make it work for me. It works for ‘fixed rate’ and variable items. The problem
   is the discount is multiplied by the parent quantity on every variation.
    I’m
   not sure if this breaks other bulk discount options, but it works for me. …. 
   I added a 3rd variable to pass in to the function get_discounted_coeff that is
   the parent quantity.
 *     ```
       	protected function get_discounted_coeff( $product_id, $quantity, $parent_quantity=0 ) {
           //added a third parameter of parent_quantity--if there is no parent quantity, it can be the same as the quantity.
           if ($parent_quantity==0) $parent_quantity=$quantity;
   
               $q = array( 0.0 );
       	$d = array( 0.0 );
   
       	$configurer = get_page_by_title( 'wc_bulk_discount_configurer', OBJECT, 'product' );
       	if ( $configurer && $configurer->ID && $configurer->post_status == 'private' ) {
       	    $product_id = $configurer->ID;
       	}
   
       	$product = $this->get_product($product_id);
       	if ($product instanceof WC_Product_Variation) {
       	    //keep child_id and use the parent_id to determine the discount
       	    $child_id=$product_id;
       	    $product_id=$product->get_parent_id();
       	}
   
       	/* Find the appropriate discount coefficient by looping through up to the five discount settings */
       	for ( $i = 1; $i <= 5; $i++ ) {
       	    array_push( $q, get_post_meta( $product_id, "_bulkdiscount_quantity_$i", true ) );
       	    if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) {
       		array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) : 0.0 );
       	    } else if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'fixed' ) {
       		array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_fixed_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_fixed_$i", true ) : 0.0 );
         	    } else {
       		array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) : 0.0 );
       	    }
       	    //changed $quantity to $parent_quantity--get discount		
       	    if ( $parent_quantity >= $q[$i] && $q[$i] > $q[0] ) {
       	        $q[0] = $q[$i];
       		$d[0] = $d[$i];
       	    }
       	}
   
       	// for percentage discount convert the resulting discount from % to the multiplying coefficient
       	if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'fixed' ) {
       	    return max( 0, $d[0] * $quantity );
       	}
       	return ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ? max( 0, $d[0] ) : min( 1.0, max( 0, ( 100.0 - round( $d[0], 2 ) ) / 100.0 ) );
           }
       }
       ```
   
 * Then I changed the function that calls this function.
 *     ```
       protected function gather_discount_coeffs() {
           global $woocommerce;
   
           $cart = $woocommerce->cart;
           $this->discount_coeffs = array();
   
           if ( sizeof( $cart->cart_contents ) > 0 ) {
               foreach ( $cart->cart_contents as $cart_item_key => $values ) {
       	    $_product = $values['data'];
       	    $quantity = 0;
                   //need to count parent separate from current item
       	    $parent_quantity=0;
       	    if ( get_option( 'woocommerce_t4m_variations_separate', 'yes' ) == 'no' && $_product instanceof WC_Product_Variation && $this->get_parent($_product) ) {
       		$parent = $this->get_parent($_product);
       		foreach ( $cart->cart_contents as $valuesInner ) {
       		    $p = $valuesInner['data'];
       		    if ( $p instanceof WC_Product_Variation && $this->get_parent($p) && $this->get_product_id($this->get_parent($p)) == $this->get_product_id($parent) ) {
                               //increase parent not the child quantity
       			$parent_quantity += $valuesInner['quantity'];
         			//moved this line outside of the foreach loop   $this->discount_coeffs...
       		    }
       		}
       		$quantity = $values['quantity'];
       		$this->discount_coeffs[$this->get_actual_id( $_product )]['quantity'] = $parent_quantity;
                   } else {
       	        $quantity = $values['quantity'];
       	        $this->discount_coeffs[$this->get_actual_id( $_product )]['quantity'] = $quantity;
       	    }
                   //here's where the first function is called using the parent quantity
       	    $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] = $this->get_discounted_coeff( $this->get_product_id($_product), $quantity,$parent_quantity );
       	    $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] = $_product->get_price();
                  //set this earlier
       	   //$this->discount_coeffs[$this->get_actual_id( $_product )]['quantity'] = $quantity;
               }
           }
       }
       ```
   
    -  This reply was modified 8 years, 2 months ago by [amicar](https://wordpress.org/support/users/amicar/).
      Reason: put full code for first edited function
    -  This reply was modified 8 years, 2 months ago by [amicar](https://wordpress.org/support/users/amicar/).

Viewing 1 replies (of 1 total)