久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
本文介紹了以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

在我的解決方案中,我希望盡可能自動(dòng)化產(chǎn)品創(chuàng)建.在我看來,一種節(jié)省時(shí)間的方法是將可下載文件自動(dòng)添加到產(chǎn)品中.

In my solution I want to automate the product creation as much as possible. One time saver is, in my opinion, to auto add the downloadable file to the product.

我創(chuàng)建了這個(gè)函數(shù):

function fcsp_add_downloadable_file($post_id, $post, $update){
  $post_thumbnail_id = get_post_thumbnail_id( $post_id );
  $url = get_site_url()."/wp-content/uploads/".get_the_date('Y')."/".get_the_date('m')."/".$filename_only = basename( get_attached_file( $post_thumbnail_id ) );

  update_post_meta($post_id, '_downloadable_files' , $url);
}
add_action( 'save_post', 'fcsp_add_downloadable_file', 99, 3 );

當(dāng)我更新產(chǎn)品時(shí),我可以看到文件路徑已保存到 _downloadable_files 元鍵.然而,它只是純文本,而不是 woocommerce 存儲(chǔ)它的方式.查看屏幕截圖(這是使用 Woo Add Product 界面創(chuàng)建的另一個(gè)產(chǎn)品:

I can see when I update the product that the file path is saved to the _downloadable_files meta key. However it is just plain text and not in the way woocommerce stores it. See screenshot (this is from another product created with the Woo Add Product interface:

它也未被 woocommerca 識(shí)別為可下載文件.非常感謝您對(duì)解決此問題的任何幫助.

It is also not recognized by woocommerca as a downloadable file. Any help on fixing this is much appreciated.

第二部分

這是要設(shè)置的產(chǎn)品標(biāo)題:

This is the product title to be set:

我們必須從圖像的 EXIF 元標(biāo)記標(biāo)題"中獲取它,并且必須在保存產(chǎn)品之前或保存產(chǎn)品時(shí)將其設(shè)置為產(chǎn)品名稱.($filemeta['image_meta']['title'];)

We have to get it from the EXIF meta tag "title" from the image and has te be set as the product name before or while saving the product. ($filemeta['image_meta']['title'];)

推薦答案

Update 2 (添加了 if 語句以允許下載生成僅到一個(gè)文件)

以下代碼將自動(dòng)添加由產(chǎn)品圖片制作的可下載文件(下載標(biāo)題來自 EXIF 數(shù)據(jù)標(biāo)題).

The following code will auto add a downloadable file made from the product image (The download title comes from the EXIF data title).

您最好使用專用的 woocommerce_admin_process_product_object 動(dòng)作掛鉤和可用的 CRUD 對(duì)象和 getter/setter 方法 woocommerce 3 以這種方式引入:

You should better use dedicated woocommerce_admin_process_product_object action hook and available CRUD objects and getters / setters methods introduced with woocommerce 3 this way:

add_action( 'woocommerce_admin_process_product_object', 'auto_add_downloadable_file', 50, 1 );
function auto_add_downloadable_file( $product ){
    // Get downloads (if there is any)
    $downloads = (array) $product->get_downloads(); 

    // Only added once (avoiding repetitions
    if( sizeof($downloads) == 0 ){
        // Get post thumbnail data
        $thumb_id = get_post_thumbnail_id( $product->get_id() );
        $src_img  = wp_get_attachment_image_src( $thumb_id, 'full');
        $img_meta = wp_get_attachment_metadata( $thumb_id, false );

        // Prepare download data
        $file_title = $img_meta['image_meta']['title'];
        $file_url   = reset($src_img);
        $file_md5   = md5($file_url);

        $download  = new WC_Product_Download(); // Get an instance of the WC_Product_Download Object

        // Set the download data
        $download->set_name($file_title);
        $download->set_id($file_md5);
        $download->set_file($file_url);


        $downloads[$md5_num] = $download; // Insert the new download to the array of downloads

        $product->set_downloads($downloads); // Set new array of downloads
    }
}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的 function.php 文件中.經(jīng)測(cè)試有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

您還可以使用 is_downloadable 檢查產(chǎn)品是否可下載() 方法在函數(shù)內(nèi)部啟動(dòng)時(shí)的 IF 語句中.

You could also check if product is downloadable using is_downloadable() method in an IF statement on start inside the function.

這篇關(guān)于以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡(jiǎn)單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
Add birthday field to WooCommerce my account and admin user page(將生日字段添加到 WooCommerce 我的帳戶和管理員用戶頁面)
主站蜘蛛池模板: 欧美电影免费网站 | 日韩欧美在线观看 | 日日操日日干 | 北条麻妃一区二区三区在线视频 | 91传媒在线观看 | 午夜激情网 | 欧美中文字幕一区二区三区亚洲 | 91久久精品日日躁夜夜躁欧美 | 亚洲精品久久久久国产 | 午夜精品久久久久久久久久久久久 | 福利一区在线观看 | 一区二区精品在线 | 九九热免费观看 | 日日日操 | 日韩中文在线视频 | 91极品尤物在线播放国产 | 超碰在线人人 | 在线免费观看a级片 | 在线激情视频 | 在线视频成人 | 欧美日韩成人网 | 天天弄| 超碰超碰 | 欧美久久久久久 | 日本手机在线 | 午夜免费视频观看 | 欧美做暖暖视频 | 涩涩鲁亚洲精品一区二区 | 国产一级淫片a直接免费看 免费a网站 | 日日噜噜噜夜夜爽爽狠狠视频97 | 日本久久久一区二区三区 | a在线v| 国产精品久久久久久吹潮 | 在线中文字幕av | 久久久免费 | 九九九久久国产免费 | 亚洲天堂网站 | 中文字幕视频在线观看免费 | 国产日韩欧美中文 | 日韩1区2区 | 激情免费视频 |