1

I'm building a custom plugin for my client, to add uploaded files to each product in cart. But I need also able to attached "upload id" let say:"456498816" to each product in cart.

So I will be able to use this data and associated it with the uploaded files, but only the products that i sending using this script.

update.php

After files uploaded it transfer the product id and quantity to cart using this code:

$product_id = $imgsizefilterdaddcart;
//$found = true;
$quantity = $spec3;
//  echo '<pre>';
//var_dump( WC() );



//check if product already in cart
if (sizeof(WC()->cart->get_cart()) < 0) {
    foreach(WC()->cart->get_cart() as $cart_item_key = > $values) {
        // var_dump( $cart_item );
        // var_dump( WC()->cart->get_item_data( $cart_item ) );

        $_product = $values['data'];
        if ($_product->id == $product_id)
            $found = true;
    }
    // if product not found, add it
    if (!$found)
        WC()->cart->add_to_cart($product_id);
}
else {
    // if no products in cart, add it
    WC()->cart->add_to_cart($product_id, $quantity);
}

But I can't figure out how to add number of uploading id for example "4564564564" to each product in cart so that way I'm able to retrieve data after order complete, and know what files belong to what order all I need is to able add only id that I created to my uploaded files. I was thinking to do something like this:

WC()->cart->add_to_cart( $product_id,$quantity,$uploadid);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
M.mik
  • 39
  • 1
  • 8
  • You can use [**this way**](http://stackoverflow.com/questions/41971376/add-custom-product-data-dynamically-as-item-meta-data-on-the-order/41971727#41971727) may be as there are many for this kind of things. You can also set a custom field in your product, and update the value after added to cart… How are you planing to generate that unique numbers Ids? … You have to explore a little the web and the ways that people have taken for similar cases... – LoicTheAztec Feb 15 '17 at 17:14
  • the id already generated ids in variable $uploadid and stored in data base where the file names and folder,i need to store only this id that created from this upload to the associated items in cart, so later i can use what files to what items and from what order. – M.mik Feb 15 '17 at 17:44
  • This kind of sounds like a textbook use case for [Product Addons](https://woocommerce.com/products/product-add-ons/). – helgatheviking Feb 15 '17 at 20:04
  • Product Add-Ons- not good for me i building my stand alone plugin without using 3th party or editing function.php and other php files of woocommerce, – M.mik Feb 16 '17 at 11:02
  • after searching google, I found something: [link](https://sarkware.com/adding-custom-product-fields-to-woocommerce-without-using-plugins) really useful, creating an input field for the user to enter name of tshirt, input "name_on_tshirt" before adding item to cart. but in my case I am using stand alone PHP file inside my plugin, and the way I transfer data is with this code: WC () ->cart->add_to_cart ($product_id, $quantity); can I some how instead of let client input text inside product page. and store it as variable "$name_on_tshirt" and use this inside my update.php to pass my value? – M.mik Feb 16 '17 at 17:57

0 Answers0