Marketpress $full_cart object

I am trying to hook into the Marketpress shopping cart and would like to be able to edit the product names similar to how variations append $name = $product->post_title . ': ' . $var_names[$variation]; . In stead of appending a variation name, I would like to append a string from a php session variable. When I attempted to edit the $name variable, for the current product being added, it appended the session variable to every product in the cart. Is there a way to append this value to only the product name of the product that was just added?

  • Seth
    • Flash Drive

    It sounds like you might have made an edit inside of a loop, either a "for each" or "while" loop or to the actual php function which manipulates the cart items. You might have to create a conditional loop which only applies the session variable to specific products.

    If the forum allows it, I could give a better answer if I saw the actual php code that you made the change to.

  • Sean Johnson
    • Site Builder, Child of Zeus


    Thanks for the response. Aye, if only there was such a add_to_cart function :slight_smile:. I am getting closer, the structure of the marketpress plugin has essentially two carts, a small one dealing with just the quantities and a larger one called full_cart holding all the data. So, I have to append the code in a couple spots and I think I will have it...

    I will post the solution when I am done. Thanks all.

  • Sean Johnson
    • Site Builder, Child of Zeus

    as requested by PaulM:

    okay, so the ultimate goal is to get marketpress to have allow customized data to be added to products before they are added to the cart. If the same item is purchased again by the user, new customized data must be attached now to the second item without losing the data from the first.

    I would then like to retain the customized data info for each item in the cart by appending it to the 'name' field similar to how a variation name is appended.

    What I have tried to do these past few days has been to edit the cart upon an item being added to the cart and attach the custom data to the item in the $cart object.

    My best attempt has been to add a value to the $cart object like this:
    $cart[$product_id][$variation] = array($quantity,$custom_value);
    But, in order for this to work, i would need to edit every place in marketpress.php that handles the quantities and have it grab the first array item '$quantity'.

    I think I could have handled this, but I could not seem to find where the $cart object is first structured to make sure it is in this format where the quantity is $cart[$product_id][$variation][0] and the custom_data is

    then when the shopping cart is displayed and the $full_cart object is created with its associative array, I would append the custom data to the 'name' field.

    $name = $name . ': ' . $custom_data;
    						$full_cart[$bid][$product_id][$variation] = array('SKU' => $skus[$variation], 'name' => $name, 'url' => get_permalink($product_id), 'price' => $this->product_price($product_id, $variation), 'quantity' => $quantity, 'download' => $download);
  • Paul
    • The Green Incsub

    @Sean Johnson, Not really seeing a good or clean solution for this. The add to cart process start with the AJAX action setup by MP. The action calls the MP function update_cart(); starting line 2471 in marketpress/marketpress.php

    If you follow from the start of that function to around line 2499 you will see the else if

    } else if (isset($_POST['product_id'])) { //add a product to cart

    It is inside this logical section you can further follow the logic where it checks to make sure the product_id is valid as well as the variation, etc. Around line 2570 you will see where the cart array is saved as the cookie.

    Other than direct hacks to MP I don't see a way to use any of the available hooks to accomplish this.

  • Paul
    • The Green Incsub

    The only usable filter I can see is line 2502 of that same function

    $product_id = apply_filters('mp_product_id_add_to_cart', intval($_POST['product_id']));

    What you could do it setup your own function to trap on this filter. Then inside the function setup your own logic to store the product_id+variation+quantity+custom meta into your own session variable. You will not be able to update/insert this into the $cart just yet. So it gets stuffed into your own SESSION var. But during some later checkout hook you can add this to the full cart which should then be store as part of the order details.

  • Sean Johnson
    • Site Builder, Child of Zeus

    @PaulM and the community!

    Thanks for the direction. We ended up going another route. See attached plugin.

    So, we decided to create a product variation on-the-fly in order to insert custom data before the item was added to the cart.

    At this point, the we create a clone variation that gets destroyed upon order completion. We were able to do all this with only adding one hook to the marketpress.php file on line 2512.

    If this would help anyone here are some things to NOTE:


    1. place THIS php file in a folder and upload to your plugins folder.
    2. insert this hook on line 2512 just after the the $variation variable is grabbed by $_POST:
    $variation = apply_filters('dls_before_add_to_cart_filter',$product_id, $variation); //DLS added 4pm 7/6/2012 for hook

    TODO / known bugs (will be fixing someday soon):
    1. add function to erase the cloned variation when the item has been removed from the cart
    2. add function to erase all cloned variations when the cart has been abandoned
    3. this will not work properly with items with inventory
    4. i'm sure there will be more :slight_smile: send suggestions to


  • Sean Johnson
    • Site Builder, Child of Zeus

    I will be changing directions slightly with the plugin:

    1) I will be cloning the entire product each time a customized item is added to the cart
    (this will get rid of the problem of dealing with the extra variations when items are removed from the cart -- and also when two people are purchasing items at the same time)
    2) because I will be cloning the entire product, I will need to adapt Marketpress to only show products that have a certain category in the frontend (this way, no one will ever see the customized product accidentally show up in a product listing)
    3) I will then be working on ensuring these products get removed when the cart is emptied, or when a specific item is removed from the cart, or even cart abandonment

    I will post the latest updates on my plugin as soon as I can and make the file available to anyone that could adapt it for their use.

    I would love input on other issues you believe may occur with my new direction; all comments are appreciated.
    Great community here, thanks.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.