WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[resolved] How to treat cart items individually rather than grouped product (9 posts)

  1. skoskie
    Member
    Posted 1 year ago #

    I have a site that allows a person to personalize each item (like engraving an iPod) they add to the cart. Often a person will add the same sku to the cart but have different personalization for each item. The cart only shows a single line item per sku and a quantity. How can I make each item be displayed on a single line?

    Please do not point me to a plugin. If the plugin can accomplish this, then the question is how?

    I don't mind re-developing the cart so that it shows each item on it's own line, but when I access the cart object, it also only shows the contents grouped by product:

    array (size=1)
      'ec8956637a99787bd197eacd77acce5e' =>
        array (size=5)
          'product_id' => int 102
          'variation_id' => string '' (length=0)
          'variation' => string '' (length=0)
          'quantity' => string '3' (length=1)
          'data' =>
            object(WC_Product_Simple)[188]
              public 'id' => int 102
              public 'post' =>
                object(WP_Post)[190]
                  ...
              public 'product_type' => string 'simple' (length=6)

    Ideally, I'd like to create a variation that doesn't require pre-determined values, instead allowing the end user to provide the value for the variation, but I don't think this is possible. I'm open to suggestions.

    http://wordpress.org/extend/plugins/woocommerce/

  2. skoskie
    Member
    Posted 1 year ago #

    Answering the question for anyone else who might come across this issue.

    When an item gets added to the cart, a 'key' for that individual item in the cart is created based on the item being added and it's associated meta data. If the item and it's meta data are identical to another item in the cart, then the generated key will be identical too, and the the quantity of the item already in the cart will simply be incremented by the quantity being added.

    To fix this, you have to hook into the add-to-cart function and add some unique meta data, like so:

    add_filter('woocommerce_add_to_cart_item_data','namespace_force_individual_cart_items',10,2);
    function namespace_force_individual_cart_items($cart_item_data, $product_id)
    {
    	$unique_cart_item_key = md5(microtime().rand()."Hi Mom!");
    	$cart_item_data['unique_key'] = $unique_cart_item_key;
    
    	return $cart_item_data;
    }

    It is important to note that this cart item data (unique_cart_item_key) you have added is only temporary and will not survive a refresh of the page. That's okay, because now you should be able to retrieve individual items in the cart by using the cart item key created by WooCommerce.

    For instance, you could access each cart item like so:

    foreach ($woocommerce->cart->cart_contents as $cart_key => $cart_item_array) {
    	var_dump($cart_item_array);
    }

    WooCommerce documentation is actually pretty terrible. I hope this helps someone.

  3. LeeLawliet
    Member
    Posted 1 year ago #

    I'm trying to work this out but have no luck.
    I'm getting confused on where to hook the code that you provided.
    Any help with it please? :)

  4. johnjazersalazar
    Member
    Posted 1 year ago #

    @LeeLawliet : what version of WooCommerce are you using? is it Version 2.0.3?
    In that case, go to wp-content/plugins/woocommerce/woocommerce-function.php

    and add this to the bottom part

    add_filter('woocommerce_add_cart_item_data','namespace_force_individual_cart_items',10,2);

    function namespace_force_individual_cart_items($cart_item_data, $product_id){
    $unique_cart_item_key = md5(microtime().rand()."Hi Mom!");
    $cart_item_data['unique_key'] = $unique_cart_item_key;

    return $cart_item_data;
    }

  5. johnjazersalazar
    Member
    Posted 1 year ago #

    You can also put it in your theme's function.php. In case you want to upgrade the plugin in the future.

  6. TatumCreative
    Member
    Posted 10 months ago #

    The name of the filter changed. Use this code now.

    add_filter('woocommerce_add_cart_item_data','namespace_force_individual_cart_items',10,2);
    function namespace_force_individual_cart_items($cart_item_data, $product_id)
    {
    	$unique_cart_item_key = md5(microtime().rand()."Hi Mom!");
    	$cart_item_data['unique_key'] = $unique_cart_item_key;
    
    	return $cart_item_data;
    }
  7. drulaniya
    Member
    Posted 8 months ago #

    thanks

  8. skoskie
    Member
    Posted 7 months ago #

    Apparently I don't get notified of responses to my posts. It's a bit late, but sorry for not responding to questions.

    Thanks @TatumCreative for updating with the updated hooks.

  9. narinders
    Member
    Posted 5 months ago #

    Hi @Skoskie, I've been trying to alter the cart item data, have been searching for a solution for 2 days now. I even created a thread, but no one replied - http://wordpress.org/support/topic/woocommerce-add-input-field-to-every-item-in-cart

    I'll continue to try, having seen this methods of yours to add the meta to cart_item_key. I was wondering if that would display the input from user on order page?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic