• When adding an image to the media library programatically during the creation of a woocommerce product cart item, the following function is called.

      
    public function add_attachment_to_folder2( $metadata, $attachment_id ) {
        
        $folder_id = $this->get_default_folder($attachment_id);
        //error_log("add_attachment_to_folder2 folder_id $folder_id");
        if($folder_id !== false) {
          $this->add_new_folder_parent($attachment_id, $folder_id);
          
        }
        return $metadata;
      }  
    
    
    $this->get_default_folder($attachment_id);
    

    returns null and not false, causing a db error on the add function.

    • This topic was modified 5 years, 10 months ago by tazziedave.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author AlanP57

    (@alanp57)

    Since I do not have the code you are using to add an image, I am not able to reproduce this issue.

    Thread Starter tazziedave

    (@tazziedave)

    Sorry,

    It’s in response to this hook:

    add_filter( 'woocommerce_add_cart_item', [ $this, 'create_image_in_cart_item' ], 10, 2 );

    The handling, i’m doing is

    
    public function create_image_in_cart_item( $cart_item, $cart_item_key ): array {
    
    // $cart_item['wcp_image_uri'] is base64 png data
    	$imageUri = $cart_item['wcp_image_uri'] ?? false;
    	if ( ! $imageUri ) {
    		return $cart_item;
    	}
    
    	unset( $cart_item['wcp_image_uri'] );  // Don't need this any more
    
    	// Extract info from the data uri and ensure that it seems valid
    	list ( $image_info, $image_base64 ) = explode( ';base64,', $imageUri );
    	$ext = substr( $image_info, - 3 );
    
    	if ( ! $image_base64 || 'png' !== $ext ) {
    		return $cart_item;
    	}
    
    	// Create path and filenames
    	$upload_dir = wp_upload_dir();
    
    	$session_hash = uniqid();
    	$path         = $upload_dir['path'] . "/wcp_item_images/$session_hash";
    
    	$product      = wc_get_product( $cart_item['product_id'] );
    	$product_name = str_replace( ' ', '-', $product->get_name() );
    
    	$filename = "$product_name-$cart_item_key.$ext";
    
    	// Save it if we can
    	try {
    		if ( wp_mkdir_p( $path ) ) {
    			$filename = $path . '/' . $filename;
    		} else {
    			$filename = $upload_dir['basedir'] . '/' . $filename;
    		}
    		file_put_contents( $filename, base64_decode( $image_base64 ) );
    	} catch ( Exception $e ) {
    		return $cart_item;
    	}
    
    	// Create attachment
    	$wp_filetype = wp_check_filetype( $filename );
    	$attachment  = [
    		'post_mime_type' => $wp_filetype['type'],
    		'post_title'     => sanitize_file_name( $filename ),
    		'post_content'   => '',
    		'post_status'    => 'inherit',
    	];
    
    	$attach_id            = wp_insert_attachment( $attachment, $filename );
    	if (! $attach_id) {
    		return $cart_item;
    	}
    	$this->currentImageId = $attach_id;
    	$cart_item['wcp_image_attachment_id']   = $attach_id;
    	$cart_item['wcp_image_filename']   = $filename;
    
    	// Do all image processing and save image formats metadata
            require_once( get_admin_path() . '/includes/image.php' );
    	$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    	wp_update_attachment_metadata( $attach_id, $attach_data );
    
    	return $cart_item;
    }
    
    • This reply was modified 5 years, 10 months ago by tazziedave. Reason: Replace DS constant shorthand with /
    Plugin Author AlanP57

    (@alanp57)

    It is necessary to first add the new folder to the folder table. Here is some of your code:

    $session_hash = uniqid();
    $path         = $upload_dir['path'] . '/wcp_item_images/$session_hash';
    ...
                    if ( wp_mkdir_p( $path ) ) {

    After creating a folder it is necessary to add it using add_media_folder:

    add_media_folder($folder_name, $parent_folder, $base_path)
    $parent_folder is the ID number of the parent folder.

    Let change add_media_folder() from a private function to a public one. Now you can use the following to call add_media_folder()

    global $maxgalleria_media_library;
     $new_folder_id = $maxgalleria_media_library->add_media_folder($folder_name, $parent_folder, $base_path);

    Once the folder has.been added then add_attachment_to_folder2() will be able to find the ID number of new the folder and return it rather than null.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Folder id null when adding image to woocommerce cart’ is closed to new replies.