Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter robertstaddon

    (@robertstaddon)

    It appears that the problem is in the way that the get_product_query_string() function calculates the total amount to be charged.

    It successfully adds the product items to an order array along with the coupon discounts. However, it never actually removes the discount amount from the “total” field which it is trying to charge through Mijireh. The Mijireh API expects the total field to equal the amount of all of the items minus the discount (https://www.mijireh.com/docs/api/#creatingneworders).

    Adding a simple “$total = $total – $discount” line will solve this problem:

    private static function get_product_query_string($form, $entry, $mj_order){
            $fields = "";
            $products = GFCommon::get_product_fields($form, $entry, true);
            $product_index = 1;
            $total = 0;
            $discount = 0;
    
            foreach($products["products"] as $product){
                $option_fields = "";
                $price = GFCommon::to_number($product["price"]);
                if(is_array(rgar($product,"options"))){
                    $option_index = 1;
                    foreach($product["options"] as $option){
                        $field_label = urlencode($option["field_label"]);
                        $option_name = urlencode($option["option_name"]);
                        $option_fields .= "&on{$option_index}_{$product_index}={$field_label}&os{$option_index}_{$product_index}={$option_name}";
                        $price += GFCommon::to_number($option["price"]);
                        $option_index++;
                    }
                }
    
                $name = $product["name"];
                if($price > 0)
                {
    				$mj_order->add_item( $name, $price, $product["quantity"], '' );
                    $total += $price * $product['quantity'];
                    $product_index++;
                }
                else{
                    $discount += abs($price) * $product['quantity'];
                }
    
            }
    
            if($discount > 0){
    			$mj_order->discount 		= $discount;
    			$total = $total - $discount; // MISSING LINE! Remove the discount from the total
            }
    
            $shipping = !empty($products["shipping"]["price"]) ? $products["shipping"]["price"] : "0.00";
    		$mj_order->shipping 		= $shipping;
    		$mj_order->show_tax			= false;
    
            return $total > 0 && $total > $discount ? $total : '0.00';
        }
    Thread Starter robertstaddon

    (@robertstaddon)

    I have rolled out an updated version of the plugin with this change on GitHub: https://github.com/robertstaddon/mijireh-checkout-for-gravity-forms

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Incompatible with Gravity Forms Coupons’ is closed to new replies.