Providing parsed cart item details in the $posts array.

Issue #27 closed
Drew Angell repo owner created an issue

Just like what I mentioned in #24 about providing a $posts['mass_payments'] array for developers to use, I'd like to do the same thing with cart item details for IPNs that have them.

Here is the snippet I used in my old IPN template to accomplish this.

$i = 1;
$cart_items = array();   
while(isset($_POST['item_number' . $i]))   
{   
    $item_number = isset($_POST['item_number' . $i]) ? $_POST['item_number' . $i] : '';   
    $item_name = isset($_POST['item_name' . $i]) ? $_POST['item_name' . $i] : '';   
    $quantity = isset($_POST['quantity' . $i]) ? $_POST['quantity' . $i] : '';  
    $mc_gross = isset($_POST['mc_gross_' . $i]) ? $_POST['mc_gross_' . $i] : 0;
    $mc_handling = isset($_POST['mc_handling' . $i]) ? $_POST['mc_handling' . $i] : 0;
    $mc_shipping = isset($_POST['mc_shipping' . $i]) ? $_POST['mc_shipping' . $i] : 0;
    $custom = isset($_POST['custom' . $i]) ? $_POST['custom' . $i] : '';   
    $option_name1 = isset($_POST['option_name1_' . $i]) ? $_POST['option_name1_' . $i] : '';   
    $option_selection1 = isset($_POST['option_selection1_' . $i]) ? $_POST['option_selection1_' . $i] : '';   
    $option_name2 = isset($_POST['option_name2_' . $i]) ? $_POST['option_name2_' . $i] : '';   
    $option_selection2 = isset($_POST['option_selection2_' . $i]) ? $_POST['option_selection2_' . $i] : '';
    $btn_id = isset($_POST['btn_id' . $i]) ? $_POST['btn_id' . $i] : '';

    $current_item = array(   
                           'item_number' => $item_number,   
                           'item_name' => $item_name,   
                           'quantity' => $quantity, 
                           'mc_gross' => $mc_gross, 
                           'mc_handling' => $mc_handling, 
                           'mc_shipping' => $mc_shipping, 
                           'custom' => $custom,   
                           'option_name1' => $option_name1,   
                           'option_selection1' => $option_selection1,   
                           'option_name2' => $option_name2,   
                           'option_selection2' => $option_selection2, 
                           'btn_id' => $btn_id
                          );   

    array_push($cart_items, $current_item);   
    $i++;   
}  

$cart_items can then be added to $posts so that developers can have that available for easy access when building their own extensions.

If no cart items are included in the particular IPN then $cart_items simply ends up as an empty array, which is just fine.

So $posts['cart_items'] will always exist, but it won't always have data depending on the IPN type.

Comments (6)

  1. jignesh kaila

    Currently IPN response display like below:

    Array
    (
        [mc_gross] => 198.00
        [invoice] => WC-424
        [protection_eligibility] => Ineligible
        [item_number1] => 
        [tax] => 0.00
        [item_number2] => 
        [payer_id] => P7KPW33EV2ZLL
        [item_number3] => 
        [payment_date] => 05:21:41 Jan 20, 2015 PST
        [payment_status] => Completed
        [charset] => windows-1252
        [mc_shipping] => 0.00
        [mc_handling] => 0.00
        [first_name] => Aslam
        [mc_fee] => 6.04
        [notify_version] => 3.8
        [custom] => a:2:{i:0;i:424;i:1;s:22:"wc_order_54be55fd40508";}
        [payer_status] => verified
        [business] => nishit.langaliya@multidots.in
        [num_cart_items] => 3
        [mc_handling1] => 0.00
        [mc_handling2] => 0.00
        [mc_handling3] => 0.00
        [verify_sign] => AOaSuVKtPqTANGYchef8KChZxNScAZZE7ktemqHKHQziq15hA4UHpaD6
        [payer_email] => aslam@multidots.in
        [mc_shipping1] => 0.00
        [mc_shipping2] => 0.00
        [mc_shipping3] => 0.00
        [tax1] => 0.00
        [tax2] => 0.00
        [tax3] => 0.00
        [txn_id] => 3S534510CG508801E
        [payment_type] => instant
        [last_name] => Multani
        [item_name1] => test
        [receiver_email] => nishit.langaliya@multidots.in
        [item_name2] => test2
        [payment_fee] => 6.04
        [item_name3] => test3
        [quantity1] => 1
        [quantity2] => 1
        [receiver_id] => U3HEQQ7JF2X76
        [quantity3] => 1
        [txn_type] => cart
        [mc_gross_1] => 50.00
        [mc_currency] => USD
        [mc_gross_2] => 89.00
        [mc_gross_3] => 59.00
        [residence_country] => US
        [test_ipn] => 1
        [transaction_subject] => a:2:{i:0;i:424;i:1;s:22:"wc_order_54be55fd40508";}
        [payment_gross] => 198.00
        [ipn_track_id] => 11cd8c396835
        [IPN_status] => Verified
    )
    

    Also above same data available in developer hook, Please let me know your thoughts.

  2. Drew Angell reporter

    Right, so there are 3 items included on this payment, but a developer building an extension would have to parse out the cart items on their own since the IPN sends them as item_name1, item_name2, item_name3, mc_gross_1, mc_gross_2, mc_gross_3, etc.

    I've seen a lot of threads in forums like Experts Exchange and StackOverflow where people are confused about how to loop through the items when they're passed like this. That's why I went ahead and included a $cart_items array with my original template that already takes care of this for them. That way, all they have to do is...

    foreach($posted['cart_items'] as $cart_item)
    {
         $item_name = $cart_item['item_name'];
         $item_price = $cart_item['mc_gross'];
         // etc.
    }
    

    I'd like to do the same thing here so that developers can simply loop through $posted['cart_items'] and won't have to worry about generating that array on their own. It's just one extra step that we can eliminate for them.

    So all we need to do is add the snippet I provided to generate the $cart_items array, and then push that into the $posted array so that it's available like the other IPN parameters.

  3. Log in to comment