Support » Plugin: WooCommerce » replacing $product->get_files();

  • Resolved Benbodhi

    (@benbodhi)



    Hi,

    I found that $downloads = $product->get_files(); stopped working in WC3.

    I also found notes saying that $downloads = $product->get_downloads(); should work instead, but it doesn’t.

    I’m just trying to get the downloadable files attached to a product.

    Thanks in advance!

Viewing 14 replies - 1 through 14 (of 14 total)
  • get_downloads() would be the correct way in WC 3.0 according to the source code. There was an issue in 3.0.0 that has been fixed in 3.0.1. Since then I did not come across any new issues with get_downloads().

    Have you tried it with wordpress and woocommerce only (no other plugins running) and twenty seventeen as theme? Just to make sure there are no conflicts?

    Thanks @mensmaximus, that’s what I thought.
    Using the latest WooCommerce (3.0.4) and WordPress (4.7.4) with no other plugins, twentyseventeen theme.

    Have you checked whether $product has data? There might be an issue early while you try to get the product itself. With the new CRUD classes this happens to me 10 times a day. It will take some time to get familiar with it – I guess.

    I have indeed, var_dump($product); spits out this:

    
    object(WC_Product_Simple)#986 (12) {
      ["object_type":protected]=>
      string(7) "product"
      ["post_type":protected]=>
      string(7) "product"
      ["cache_group":protected]=>
      string(8) "products"
      ["data":protected]=>
      array(48) {
        ["name"]=>
        string(15) "Awesome Product"
        ["slug"]=>
        string(15) "awesome-product"
        ["date_created"]=>
        object(WC_DateTime)#991 (4) {
          ["utc_offset":protected]=>
          int(0)
          ["date"]=>
          string(26) "2017-01-31 15:57:56.000000"
          ["timezone_type"]=>
          int(1)
          ["timezone"]=>
          string(6) "+00:00"
        }
        ["date_modified"]=>
        object(WC_DateTime)#990 (4) {
          ["utc_offset":protected]=>
          int(0)
          ["date"]=>
          string(26) "2017-04-25 20:51:15.000000"
          ["timezone_type"]=>
          int(1)
          ["timezone"]=>
          string(6) "+00:00"
        }
        ["status"]=>
        string(7) "publish"
        ["featured"]=>
        bool(false)
        ["catalog_visibility"]=>
        string(7) "visible"
        ["description"]=>
        string(48) "The most awesome description for a product ever."
        ["short_description"]=>
        string(47) "The most awesome product ever is one like this."
        ["sku"]=>
        string(0) ""
        ["price"]=>
        string(3) "100"
        ["regular_price"]=>
        string(3) "100"
        ["sale_price"]=>
        string(0) ""
        ["date_on_sale_from"]=>
        NULL
        ["date_on_sale_to"]=>
        NULL
        ["total_sales"]=>
        int(0)
        ["tax_status"]=>
        string(7) "taxable"
        ["tax_class"]=>
        string(0) ""
        ["manage_stock"]=>
        bool(false)
        ["stock_quantity"]=>
        NULL
        ["stock_status"]=>
        string(7) "instock"
        ["backorders"]=>
        string(2) "no"
        ["sold_individually"]=>
        bool(false)
        ["weight"]=>
        string(0) ""
        ["length"]=>
        string(0) ""
        ["width"]=>
        string(0) ""
        ["height"]=>
        string(0) ""
        ["upsell_ids"]=>
        array(0) {
        }
        ["cross_sell_ids"]=>
        array(0) {
        }
        ["parent_id"]=>
        int(0)
        ["reviews_allowed"]=>
        bool(true)
        ["purchase_note"]=>
        string(0) ""
        ["attributes"]=>
        array(0) {
        }
        ["default_attributes"]=>
        array(0) {
        }
        ["menu_order"]=>
        int(0)
        ["virtual"]=>
        bool(true)
        ["downloadable"]=>
        bool(true)
        ["category_ids"]=>
        array(0) {
        }
        ["tag_ids"]=>
        array(0) {
        }
        ["shipping_class_id"]=>
        int(0)
        ["downloads"]=>
        array(0) {
        }
        ["image_id"]=>
        string(1) "9"
        ["gallery_image_ids"]=>
        array(0) {
        }
        ["download_limit"]=>
        int(-1)
        ["download_expiry"]=>
        int(-1)
        ["rating_counts"]=>
        array(0) {
        }
        ["average_rating"]=>
        string(1) "0"
        ["review_count"]=>
        int(0)
      }
      ["supports":protected]=>
      array(1) {
        [0]=>
        string(16) "ajax_add_to_cart"
      }
      ["id":protected]=>
      int(8)
      ["changes":protected]=>
      array(0) {
      }
      ["object_read":protected]=>
      bool(true)
      ["extra_data":protected]=>
      array(0) {
      }
      ["default_data":protected]=>
      array(48) {
        ["name"]=>
        string(0) ""
        ["slug"]=>
        string(0) ""
        ["date_created"]=>
        NULL
        ["date_modified"]=>
        NULL
        ["status"]=>
        bool(false)
        ["featured"]=>
        bool(false)
        ["catalog_visibility"]=>
        string(7) "visible"
        ["description"]=>
        string(0) ""
        ["short_description"]=>
        string(0) ""
        ["sku"]=>
        string(0) ""
        ["price"]=>
        string(0) ""
        ["regular_price"]=>
        string(0) ""
        ["sale_price"]=>
        string(0) ""
        ["date_on_sale_from"]=>
        NULL
        ["date_on_sale_to"]=>
        NULL
        ["total_sales"]=>
        string(1) "0"
        ["tax_status"]=>
        string(7) "taxable"
        ["tax_class"]=>
        string(0) ""
        ["manage_stock"]=>
        bool(false)
        ["stock_quantity"]=>
        NULL
        ["stock_status"]=>
        string(7) "instock"
        ["backorders"]=>
        string(2) "no"
        ["sold_individually"]=>
        bool(false)
        ["weight"]=>
        string(0) ""
        ["length"]=>
        string(0) ""
        ["width"]=>
        string(0) ""
        ["height"]=>
        string(0) ""
        ["upsell_ids"]=>
        array(0) {
        }
        ["cross_sell_ids"]=>
        array(0) {
        }
        ["parent_id"]=>
        int(0)
        ["reviews_allowed"]=>
        bool(true)
        ["purchase_note"]=>
        string(0) ""
        ["attributes"]=>
        array(0) {
        }
        ["default_attributes"]=>
        array(0) {
        }
        ["menu_order"]=>
        int(0)
        ["virtual"]=>
        bool(false)
        ["downloadable"]=>
        bool(false)
        ["category_ids"]=>
        array(0) {
        }
        ["tag_ids"]=>
        array(0) {
        }
        ["shipping_class_id"]=>
        int(0)
        ["downloads"]=>
        array(0) {
        }
        ["image_id"]=>
        string(0) ""
        ["gallery_image_ids"]=>
        array(0) {
        }
        ["download_limit"]=>
        int(-1)
        ["download_expiry"]=>
        int(-1)
        ["rating_counts"]=>
        array(0) {
        }
        ["average_rating"]=>
        int(0)
        ["review_count"]=>
        int(0)
      }
      ["data_store":protected]=>
      object(WC_Data_Store)#982 (4) {
        ["instance":"WC_Data_Store":private]=>
        object(WC_Product_Data_Store_CPT)#989 (5) {
          ["internal_meta_keys":protected]=>
          array(39) {
            [0]=>
            string(11) "_visibility"
            [1]=>
            string(4) "_sku"
            [2]=>
            string(6) "_price"
            [3]=>
            string(14) "_regular_price"
            [4]=>
            string(11) "_sale_price"
            [5]=>
            string(22) "_sale_price_dates_from"
            [6]=>
            string(20) "_sale_price_dates_to"
            [7]=>
            string(11) "total_sales"
            [8]=>
            string(11) "_tax_status"
            [9]=>
            string(10) "_tax_class"
            [10]=>
            string(13) "_manage_stock"
            [11]=>
            string(6) "_stock"
            [12]=>
            string(13) "_stock_status"
            [13]=>
            string(11) "_backorders"
            [14]=>
            string(18) "_sold_individually"
            [15]=>
            string(7) "_weight"
            [16]=>
            string(7) "_length"
            [17]=>
            string(6) "_width"
            [18]=>
            string(7) "_height"
            [19]=>
            string(11) "_upsell_ids"
            [20]=>
            string(14) "_crosssell_ids"
            [21]=>
            string(14) "_purchase_note"
            [22]=>
            string(19) "_default_attributes"
            [23]=>
            string(19) "_product_attributes"
            [24]=>
            string(8) "_virtual"
            [25]=>
            string(13) "_downloadable"
            [26]=>
            string(9) "_featured"
            [27]=>
            string(19) "_downloadable_files"
            [28]=>
            string(16) "_wc_rating_count"
            [29]=>
            string(18) "_wc_average_rating"
            [30]=>
            string(16) "_wc_review_count"
            [31]=>
            string(22) "_variation_description"
            [32]=>
            string(13) "_thumbnail_id"
            [33]=>
            string(11) "_file_paths"
            [34]=>
            string(22) "_product_image_gallery"
            [35]=>
            string(16) "_product_version"
            [36]=>
            string(12) "_wp_old_slug"
            [37]=>
            string(10) "_edit_last"
            [38]=>
            string(10) "_edit_lock"
          }
          ["extra_data_saved":protected]=>
          bool(false)
          ["updated_props":protected]=>
          array(0) {
          }
          ["meta_type":protected]=>
          string(4) "post"
          ["object_id_field_for_meta":protected]=>
          string(0) ""
        }
        ["stores":"WC_Data_Store":private]=>
        array(18) {
          ["coupon"]=>
          string(24) "WC_Coupon_Data_Store_CPT"
          ["customer"]=>
          string(22) "WC_Customer_Data_Store"
          ["customer-download"]=>
          string(31) "WC_Customer_Download_Data_Store"
          ["customer-session"]=>
          string(30) "WC_Customer_Data_Store_Session"
          ["order"]=>
          string(23) "WC_Order_Data_Store_CPT"
          ["order-refund"]=>
          string(30) "WC_Order_Refund_Data_Store_CPT"
          ["order-item"]=>
          string(24) "WC_Order_Item_Data_Store"
          ["order-item-coupon"]=>
          string(31) "WC_Order_Item_Coupon_Data_Store"
          ["order-item-fee"]=>
          string(28) "WC_Order_Item_Fee_Data_Store"
          ["order-item-product"]=>
          string(32) "WC_Order_Item_Product_Data_Store"
          ["order-item-shipping"]=>
          string(33) "WC_Order_Item_Shipping_Data_Store"
          ["order-item-tax"]=>
          string(28) "WC_Order_Item_Tax_Data_Store"
          ["payment-token"]=>
          string(27) "WC_Payment_Token_Data_Store"
          ["product"]=>
          string(25) "WC_Product_Data_Store_CPT"
          ["product-grouped"]=>
          string(33) "WC_Product_Grouped_Data_Store_CPT"
          ["product-variable"]=>
          string(34) "WC_Product_Variable_Data_Store_CPT"
          ["product-variation"]=>
          string(35) "WC_Product_Variation_Data_Store_CPT"
          ["shipping-zone"]=>
          string(27) "WC_Shipping_Zone_Data_Store"
        }
        ["current_class_name":"WC_Data_Store":private]=>
        string(25) "WC_Product_Data_Store_CPT"
        ["object_type":"WC_Data_Store":private]=>
        string(14) "product-simple"
      }
      ["meta_data":protected]=>
      NULL
    }
    

    It looks like the downloads array is simply empty, yet the information is saved in the database and in the post edit screen.

    Ok, so it was to do with how the data is being saved to the database (another part of my own code).
    I have extra fields for downloadable files, which is saving, but somehow it’s saving differently to the standard file data… Not sure why at this point.

    I guess my initial question should have been “How to store extra downloadable file data in WooCommerce 3.x”…

    If you could point me in the right direction for this, I would be greatly appreciative.

    The two versions are so similar…

    Here is the downloadable file data for standard:

    a:2:{s:32:"fa456e6edbf46f62bdd2a04deb0d6b23";a:4:{s:2:"id";s:32:"fa456e6edbf46f62bdd2a04deb0d6b23";s:4:"name";s:13:"STANDARDFILES";s:4:"file";s:68:"http://localhost/woocommerce/wp-content/uploads/2017/03/sun-rain.svg";s:13:"previous_hash";s:32:"fa456e6edbf46f62bdd2a04deb0d6b23";}s:32:"a9228796636a146b19eaba6cdbc51a99";a:4:{s:2:"id";s:32:"a9228796636a146b19eaba6cdbc51a99";s:4:"name";s:9:"Test File";s:4:"file";s:81:"http://localhost/woocommerce/wp-content/uploads/2017/01/mrbill-social-hexagon.svg";s:13:"previous_hash";s:32:"a9228796636a146b19eaba6cdbc51a99";}}
    

    And here is the same table cell but with my extra fields saved:

    s:839:"a:2:{s:32:"a9228796636a146b19eaba6cdbc51a99";a:7:{s:2:"id";s:32:"a9228796636a146b19eaba6cdbc51a99";s:4:"name";s:16:"WITH EXTRAFIELDS";s:4:"file";s:81:"http://localhost/woocommerce/wp-content/uploads/2017/01/mrbill-social-hexagon.svg";s:11:"description";s:23:"EXTRAFIELDS DESCRIPTION";s:4:"size";s:5:"123MB";s:4:"icon";s:70:"http://localhost/woocommerce/wp-content/uploads/2017/03/sun-rain-1.svg";s:13:"previous_hash";s:0:"";}s:32:"caaf05b8aeba292e5e9486f7cc9824fe";a:7:{s:2:"id";s:32:"caaf05b8aeba292e5e9486f7cc9824fe";s:4:"name";s:18:"SECOND EXTRAFIELDS";s:4:"file";s:79:"http://localhost/woocommerce/wp-content/uploads/2017/01/IMG_31012017_210510.png";s:11:"description";s:18:"SECOND DESCRIPTION";s:4:"size";s:3:"123";s:4:"icon";s:70:"http://localhost/woocommerce/wp-content/uploads/2017/03/sun-rain-1.svg";s:13:"previous_hash";s:0:"";}}";
    

    Both are from the _downloadable_files in the database.

    • This reply was modified 1 year, 6 months ago by  Benbodhi.

    Looks like a serialization issue to me. I have seen this with code using serialize() instead of maybe_serialize(). Anyway you should use the new CRUD classes to store the downloadable file information. From my knowledge this would be ‘set_downloads’. But to be honest the last time I had to work with downloads was in WC 2.4.

    If I would have to work with downloads again I would start in class WC_Product reading the source code of set_downloads and in class WC_Product_Data_Store_CPT of read_downloads to get a feeling for the new CRUD classes dealing with downloads. Than I would just create a standard download and try to read from it. The next steps would than hopefully be clear 🙂

    Maybe @mikejolley and/or @claudiosanches can drop a note about the correct handling of downloads now.

    Thanks @mensmaximus,

    I’ve been doing exactly that 🙂

    Would love to hear from the others if there is any other input.

    Plugin Author Mike Jolley

    (@mikejolley)

    If you pass the downloadable file data to the CRUD setters (set_downloads) https://github.com/woocommerce/woocommerce/blob/97958de059b42318390a1248dd5fc30362c131a4/includes/abstracts/abstract-wc-product.php#L1138 serialisation is handled for you.

    This is perfect, thank you guys!
    I’ve gotta say, this new way is soooooo much better.

    Hi @mikejolley,

    I hope you can help me with this!
    I am soooo close I think.

    I am trying to send the extra fields to the set_downloads function, but I can not seem to get it to work.

    I reached out to Woo developers and was told it’s not possible… but I don’t believe that.

    I have everything working except the actual save of the new fields data for each downloadable file. I have a field for icon (URL field), description (textarea) and size (text field).

    I have the following code working, but if I deactivate my plugin, then the standard file name and file URL for simple products is gone when I use get_downloads() to retrieve the data.

    The variable data saves just fine and the file name and URL are still there if my plugin is removed which is ideal.

    I’ve tried so many things that just don’t seem to be working when it comes to passing the new file fields to set_downloads().

    Pleeeeeease help me figure this one out. Here is the code that seems to work ok for variable products with downloadable files, but not simple. Both of these are still using the update_post_meta, and additionally the simple product is using serialize() because I can’t get it to work without.

    Thanks in advance for any help you can provide!

    function bodhi_wedpf_save_meta( $post_id ) {
    
    	if ( $_POST['product-type'] == 'variable' ) {
    
    		foreach ( $_POST['variable_post_id'] as $variation_id ) {
    
    			$data = get_post_meta( $variation_id, '_downloadable_files', true);
    
    			if ( $data ) {
    
    				$final_array = array();
    				$j = 0;
    
    				foreach ( $data as $key => $value ) {
    
    					$final_array[$key] = array(
    						'id' => $key,
    						'name' => $_POST['_wc_variation_file_names'][$variation_id][$j],
    						'file' => $_POST['_wc_variation_file_urls'][$variation_id][$j],
    						'description' => $_POST['_wc_variation_file_descriptions'][$variation_id][$j],
    						'size' => $_POST['_wc_variation_file_size'][$variation_id][$j],
    						'icon' => $_POST['_wc_variation_file_icon_url'][$variation_id][$j],
    						'previous_hash' => ''
    					);
    
    					$j++;
    				}
    
    				update_post_meta( $variation_id, '_downloadable_files', $final_array );
    
    			}
    
    		}
    
    	} else {
    
    		$data = get_post_meta( $post_id, '_downloadable_files', true );
    
    		if ( $data ) {
    
    			$final_array = array();
    			$i = 0;
    
    			foreach ( $data as $key => $value ) {
    
    				$final_array[$key] = array(
    					'id' => $key,
    					'name' => $_POST['_wc_file_names'][$i],
    					'file' => $_POST['_wc_file_urls'][$i],
    					'description' => $_POST['_wc_file_descriptions'][$i],
    					'size' => $_POST['_wc_file_size'][$i],
    					'icon' => $_POST['_wc_file_icon_url'][$i],
    					'previous_hash' => ''
    				);
    
    				$i++;
    			}
    
    			update_post_meta( $post_id, '_downloadable_files', serialize( $final_array ) );
    
    		}
    
    	}
    
    } // End of save meta function. bodhi_wedpf_save_meta( $post_id )
    add_action( 'woocommerce_process_product_meta', 'bodhi_wedpf_save_meta', 1000, 1 );
    
    Plugin Author Mike Jolley

    (@mikejolley)

    Thanks Mike, not to worry.
    I have it working with update_post_meta instead.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘replacing $product->get_files();’ is closed to new replies.