• Resolved atomonelone

    (@atomonelone)


    Hi Saravana!

    First of all, amazing plugin!

    I was hoping you could help me resolve an issue.
    I’m using a plugin to generate a pdf(it’s using mPdf as its engine) of all the items added in the woocommerce cart and I need to display the value of my editable WCFF textfield inside the pdf.
    I’ve been going down a deep rabbit hole trying to figure out how to do this as I first thought the data was stored as post meta when infact it’s stored as cart item data/session data if I’m not wrong? My knowledge about php is very limited so it’s all about trial and error but I just can’t figure this out.

    I found a snippet on your website that I tried to use inside my function, but I just get the result “array” back. Also I’m pretty sure using it this way I’m actually outputing the entire cart not just the value of the WCFF field in the cart.

    So could you please help me or point me in the right direction to just output the value of the field? It has to be just the value not an array and it has to be pulled from the session data/cart item data since you are able to edit the field in the cart.

    I will be forever greatful if you can help me with this! <3
    Thank you in advance.

    Below you can see my function and maybe figure out what I’m trying to do.

    add_filter( 'woocommerce_pdf_catalog_before_product_information', 'show_text_pdf', 10, 2);
     function show_text_pdf ($html, $id) {
     	
     	if( count( WC()->cart->get_cart() ) > 0 ) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if( isset( $cart_item[ "wccaf_fritextutskrift" ] ) ) {
                echo $cart_item[ "wccaf_fritextutskrift" ];
                
            }
        }       
    }
    	$html = 'Position: ' . $cart_item;
    
    	return $html;
     }
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Saravana Kumar K

    (@mycholan)

    Hi,

    Pls try this updated snippet.

    function show_text_pdf ($html, $id) {
     	
     	if( count( WC()->cart->get_cart() ) > 0 ) {
        	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            	if( isset( $cart_item[ "wccaf_fritextutskrift" ] ) ) {
    
    				/**
    				 * 
    				 * This $field has user value as well as some additional meta for other functionalities 
    				 * So we cannot use it as a whole
    				 * 
    				 * */
    				$field = $cart_item[ "wccaf_fritextutskrift" ];
    				/* The user_val property inside $field has the actual user value */
    				echo isset($field["user_val"]) ? $field["user_val"] : "";
                	
            	}
        	}       
    	}
    	$html = 'Position: ' . $cart_item;
    
    	return $html;
    }
    add_filter( 'woocommerce_pdf_catalog_before_product_information', 'show_text_pdf', 10, 2);

    Let me know if it solved or not.

    Thread Starter atomonelone

    (@atomonelone)

    Hey Saravana!

    I thank you for getting back to me this quick, I’m really really grateful!

    When using your updated function I first got an error saying “Data has already been sent to output” after a quick google I found that using the ob_start/ob_clean could possibly help me solve this problem so I tried adding it to the function. This got the Pdf to be outputted again but it still says “Position: Array”

    I tried to do some troubleshooting and maybe this information can help:
    I had two items in the cart, one without any custom field and one with the text:Random but in the output error it said “RandomRandomData has already been sent to output” notice that it repeats 2 times even though I only have one item with the custom field. I then tried adding a third item also without any input in the custom field and the error returned the Random Text three times. This tells me we’re getting the value of the field but that maybe the foreach is not needed and the pdf generator is already running the function for each item in the cart? I might be wrong since like I said my knowledge of php is very limited but I hope this might make some sence to you.

    I’ve been trying to find a solution by myself for a while but I’m stuck and I’m really sorry to be taking up your time. If it’s out of your scope of support I understand but if you would be willing to give it another try with this new information I wouldn’t be able to thank you enough.

    This is the code from you with the ob_start/clean added to it:

    function show_text_pdf ($html, $id) {
    ob_start();	
     	if( count( WC()->cart->get_cart() ) > 0 ) {
        	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            	if( isset( $cart_item[ "wccpf_fritext" ] ) ) {
    				/**
    				 * 
    				 * This $field has user value as well as some additional meta for other functionalities 
    				 * So we cannot use it as a whole
    				 * 
    				 * */
    				$field = $cart_item[ "wccpf_fritext" ];
    				/* The user_val property inside $field has the actual user value */
    				echo isset($field["user_val"]) ? $field["user_val"] : "";
                	
            	}
        	}       
    	}
    	$html = 'Position: ' . $cart_item;
    	return $html;
    ob_clean();
    }
    add_filter( 'woocommerce_pdf_catalog_before_product_information', 'show_text_pdf', 10, 2);

    This is the original example i tried to incorperate your snippet in:

    add_filter( 'woocommerce_pdf_catalog_before_product_information', 'show_case_pack_in_pdf', 10, 2);
     function show_case_pack_in_pdf ($html, $id) {
     	
     	$product = wc_get_product($id);
     	$pid= $product->get_id();
    
     	$case_pack = get_post_meta($pid,'case_pack',true); 
    
    	$html = 'Case Pack: ' . $case_pack;
    
    	return $html;
     }
    Thread Starter atomonelone

    (@atomonelone)

    Ive done some digging and this is how they implement the cart qauntity in the pdf for example:
    It looks very close to what we’re are trying to do so maybe it’s to some help?

    if($this->get_option('showTitleCartQuantity') && $_GET['pdf-catalog'] == "cart") {
    			$cart = WC()->cart->get_cart();
    			if(!empty($cart)) {
    				foreach ( $cart as $cart_item_key => $cart_item ) {
    
    					$cart_product_id = $cart_item['product_id'];
    					if(isset($cart_item['variation_id']) && !empty($cart_item['variation_id'])) {
    						$cart_product_id = $cart_item['variation_id'];
    					}
    					// if(isset())
    					if($cart_product_id == $product->get_id() && isset($cart_item['quantity'])) {
    						$this->data->title = '<span class="quantity">' . $cart_item['quantity'] . ' x </span>' . $this->data->title;
    					}
    				}
    			}
    		}
    Plugin Author Saravana Kumar K

    (@mycholan)

    Hi,

    Pls let me know the actual requirement, so that I can help little more.

    I am guessing, you are using Woocommerce PDF Catalog plugin to create pdf catalog of your products right.?

    In that case you wanted to include the custom field details into the catalog.

    Am I right.? If not pls give me some details.

    I am still not cleared why you need to access Cart Object, do you need to export cart item as pdf.?

    Thread Starter atomonelone

    (@atomonelone)

    Ofcourse, my bad!

    I’m using Woocommerce PDF Catalog to create pdf’s of products in the cart and not to make catalogs of categories. Then I’m using your plugin to make an input field so we can add notes to each object in the cart and be able to edit them from the cart. That’s why I need access to the cart objects and not the post meta. The PDF generator won’t accept arrays and needs to output html.

    Hope this helps you understand, thanks again for taking the time to help me!!

    Plugin Author Saravana Kumar K

    (@mycholan)

    function show_text_pdf ($html, $id) {
     	
    	if (!$html) {
    		$html = "";
    	}
    
    	if( count( WC()->cart->get_cart() ) > 0 ) {
    	   foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    		   if (($cart_item["product_id"] == $id) && isset($cart_item[ "wccaf_fritextutskrift"])) {
    
    			   /**
    				* 
    				* This $field has user value as well as some additional meta for other functionalities 
    				* So we cannot use it as a whole
    				* 
    				* */
    			   $field = $cart_item[ "wccaf_fritextutskrift" ];
    			   /* The user_val property inside $field has the actual user value */			   
    			   $html .= 'Position: ' . isset($field["user_val"]) ? $field["user_val"] : "";
    			   
    		   }
    	   }       
       }   
    
       return $html;
       
    }
    add_filter( 'woocommerce_pdf_catalog_before_product_information', 'show_text_pdf', 10, 2);
    

    Let me know how it goes

    Thread Starter atomonelone

    (@atomonelone)

    Dude, you are amazing! It’s showing the value now!

    I did notice a couple of things but I feel ashamed asking since you pretty muched solved my problem and I’m not sure this is a problem with your code actually.
    But here we go:
    1. the title “Position:” does not show up.
    2. When I add more then one of the same item in the cart all the custom fields text gets combined and added to each of the same product, even if there is no input in the field. I’ve uploaded two images to imgur so you can see what I mean:
    Example of problem – Imgur
    If this is out of your scope I’m totally fine with that and I’m just grateful that you helped me get the values to show. thank you!

    Is there anywhere I could donate to you? I’d also be more than happy to pay for your plugin if possible. once again thank you for taking the time!

    Plugin Author Saravana Kumar K

    (@mycholan)

    Hi, yes the 2nd point is true, we need cart item key to fetch the current custom value. and avoid duplicate.

    but here we receive only $html and $id arguments from the filter.

    I am afraid you will have to edit the Woocommerce PDF Catalog plugin to supply the cart item key as well.

    Hope this will get you in the right direction.

    Thread Starter atomonelone

    (@atomonelone)

    Thank you for all the support, much appreciated! <3

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Show cart item data’ is closed to new replies.