Snippets

Ben Byrne Append line-item meta value to download URL

Updated by Ben Byrne

File e2pdf-woocommerce.php Modified

  • Ignore whitespace
  • Hide word diff
     public function filter_woocommerce_product_file_download_path($file_path, $product, $download_id) {
 
-        // This block of code should run in a separate filter that's run before e2pdf's is run.
+        // This block of code should run in a separate filter that's run before e2pdf's is run so we don't hack e2pdf.
         if ( isset( $_GET['recipient'] ) ) {
             $recipient = base64_decode( urldecode( $_GET['recipient'] ) );
+            $file_path = str_replace( ']', ' extra="' . $recipient . '"]', $file_path ); // I'm 100% sure "extra=[string]" is not the way to modify the shortcode to receive our value, but... what is? HELP!
         }
-        // I'm 100% sure "extra=[string]" is not the way to modify the shortcode to receive our value, but... what is? HELP!
-        $file_path = str_replace( ']', ' extra="' . $recipient . '"]', $file_path );
         
         $file_path = $this->filter_content($file_path, $product->get_id(), true);
         return $file_path;
Updated by Ben Byrne

File e2pdf-woocommerce.php Modified

  • Ignore whitespace
  • Hide word diff
     public function filter_woocommerce_product_file_download_path($file_path, $product, $download_id) {
+
+        // This block of code should run in a separate filter that's run before e2pdf's is run.
         if ( isset( $_GET['recipient'] ) ) {
             $recipient = base64_decode( urldecode( $_GET['recipient'] ) );
         }
+        // I'm 100% sure "extra=[string]" is not the way to modify the shortcode to receive our value, but... what is? HELP!
         $file_path = str_replace( ']', ' extra="' . $recipient . '"]', $file_path );
+        
         $file_path = $this->filter_content($file_path, $product->get_id(), true);
         return $file_path;
     }
Updated by Ben Byrne

File e2pdf-woocommerce.php Added

  • Ignore whitespace
  • Hide word diff
+    public function filter_woocommerce_product_file_download_path($file_path, $product, $download_id) {
+        if ( isset( $_GET['recipient'] ) ) {
+            $recipient = base64_decode( urldecode( $_GET['recipient'] ) );
+        }
+        $file_path = str_replace( ']', ' extra="' . $recipient . '"]', $file_path );
+        $file_path = $this->filter_content($file_path, $product->get_id(), true);
+        return $file_path;
+    }

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+/**
+ * Append a meta from an order's line-items to download URLs for later retrieval
+ */
+function pbi_downloads( $downloads, $order_object ) {
+	
+	$order_items = $order_object->get_items();
+	$downloadable_items = [];
+
+	foreach( $order_items as $index => $item ) {
+		if ( ! is_object( $item ) || ! $item->is_type( 'line_item' ) ) {
+			continue;
+		}
+		if ( $item->get_product() && $item->get_item_downloads() ) {
+			$downloadable_items[] = $item;
+		}
+	}
+
+	foreach( $downloadable_items as $item ) {
+		print_r( $item->get_meta('_wcj_product_input_fields_local_1') );
+	}
+
+	foreach( $downloads as $index => &$download ) {
+		$recipient_name = $downloadable_items[ $index ]->get_meta( '_wcj_product_input_fields_local_1' );
+		if ( $recipient_name ) {
+			$download['download_url'] .= '&recipient=' . urlencode( base64_encode( $recipient_name ) );
+		}
+		
+	}
+
+	return $downloads;
+}
+add_filter( 'woocommerce_order_get_downloadable_items', 'pbi_downloads', 99, 2 );

File snippet.php Deleted

  • Ignore whitespace
  • Hide word diff
-/**
- * Append a meta from an order's line-items to download URLs for later retrieval
- */
-function pbi_downloads( $downloads, $order_object ) {
-	
-	$order_items = $order_object->get_items();
-	$downloadable_items = [];
-
-	foreach( $order_items as $index => $item ) {
-		if ( ! is_object( $item ) || ! $item->is_type( 'line_item' ) ) {
-			continue;
-		}
-		if ( $item->get_product() && $item->get_item_downloads() ) {
-			$downloadable_items[] = $item;
-		}
-	}
-
-	foreach( $downloadable_items as $item ) {
-		print_r( $item->get_meta('_wcj_product_input_fields_local_1') );
-	}
-
-	foreach( $downloads as $index => &$download ) {
-		$recipient_name = $downloadable_items[ $index ]->get_meta( '_wcj_product_input_fields_local_1' );
-		if ( $recipient_name ) {
-			$download['download_url'] .= '&recipient=' . urlencode( base64_encode( $recipient_name ) );
-		}
-		
-	}
-
-	return $downloads;
-}
-add_filter( 'woocommerce_order_get_downloadable_items', 'pbi_downloads', 99, 2 );
Created by Ben Byrne

File snippet.php Added

  • Ignore whitespace
  • Hide word diff
+/**
+ * Append a meta from an order's line-items to download URLs for later retrieval
+ */
+function pbi_downloads( $downloads, $order_object ) {
+	
+	$order_items = $order_object->get_items();
+	$downloadable_items = [];
+
+	foreach( $order_items as $index => $item ) {
+		if ( ! is_object( $item ) || ! $item->is_type( 'line_item' ) ) {
+			continue;
+		}
+		if ( $item->get_product() && $item->get_item_downloads() ) {
+			$downloadable_items[] = $item;
+		}
+	}
+
+	foreach( $downloadable_items as $item ) {
+		print_r( $item->get_meta('_wcj_product_input_fields_local_1') );
+	}
+
+	foreach( $downloads as $index => &$download ) {
+		$recipient_name = $downloadable_items[ $index ]->get_meta( '_wcj_product_input_fields_local_1' );
+		if ( $recipient_name ) {
+			$download['download_url'] .= '&recipient=' . urlencode( base64_encode( $recipient_name ) );
+		}
+		
+	}
+
+	return $downloads;
+}
+add_filter( 'woocommerce_order_get_downloadable_items', 'pbi_downloads', 99, 2 );
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.