pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

以編程方式將可下載文件添加到 Woocommerce 產品

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

問題描述

限時送ChatGPT賬號..

在我的解決方案中,我希望盡可能自動化產品創建.在我看來,一種節省時間的方法是將可下載文件自動添加到產品中.

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.

我創建了這個函數:

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 );

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

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 識別為可下載文件.非常感謝您對解決此問題的任何幫助.

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

第二部分

這是要設置的產品標題:

This is the product title to be set:

我們必須從圖像的 EXIF 元標記標題"中獲取它,并且必須在保存產品之前或保存產品時將其設置為產品名稱.($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 語句以允許下載生成僅到一個文件)

以下代碼將自動添加由產品圖片制作的可下載文件(下載標題來自 EXIF 數據標題).

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 動作掛鉤和可用的 CRUD 對象和 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
    }
}

代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試有效.

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

您還可以使用 is_downloadable 檢查產品是否可下載() 方法在函數內部啟動時的 IF 語句中.

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

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

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

相關文檔推薦

Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
Add birthday field to WooCommerce my account and admin user page(將生日字段添加到 WooCommerce 我的帳戶和管理員用戶頁面)
主站蜘蛛池模板: 杰恒蠕动泵-蠕动泵专业厂家-19年专注蠕动泵 | 哈希PC1R1A,哈希CA9300,哈希SC4500-上海鑫嵩实业有限公司 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 曙光腾达官网-天津脚手架租赁-木板架出租-移动门式脚手架租赁「免费搭设」 | 环氧铁红防锈漆_环氧漆_无溶剂环氧涂料_环氧防腐漆-华川涂料 | 威廉希尔WilliamHill·足球(中国)体育官方网站 | 上海logo设计| 送料机_高速冲床送料机_NC伺服滚轮送料机厂家-东莞市久谐自动化设备有限公司 | 爆炸冲击传感器-无线遥测传感器-航天星百科| 顺辉瓷砖-大国品牌-中国顺辉 | 汽车整车综合环境舱_军标砂尘_盐雾试验室试验箱-无锡苏南试验设备有限公司 | 东莞韩创-专业绝缘骨架|马达塑胶零件|塑胶电机配件|塑封电机骨架厂家 | 寮步纸箱厂_东莞纸箱厂 _东莞纸箱加工厂-东莞市寮步恒辉纸制品厂 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 泰兴市热钻机械有限公司-热熔钻孔机-数控热熔钻-热熔钻孔攻牙一体机 | 福建自考_福建自学考试网 | 欧洲MV日韩MV国产_人妻无码一区二区三区免费_少妇被 到高潮喷出白浆av_精品少妇自慰到喷水AV网站 | 代理记账_免费注册公司_营业执照代办_资质代办-【乐财汇】 | 警用|治安|保安|不锈钢岗亭-售货亭价格-垃圾分类亭-移动厕所厂家-苏州灿宇建材 | 上海恒驭仪器有限公司-实验室平板硫化机-小型平板硫化机-全自动平板硫化机 | 剪刃_纵剪机刀片_分条机刀片-南京雷德机械有限公司 | 轴承振动测量仪电箱-轴承测振动仪器-测试仪厂家-杭州居易电气 | 二手色谱仪器,十万分之一分析天平,蒸发光检测器,电位滴定仪-湖北捷岛科学仪器有限公司 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 周易算网-八字测算网 - 周易算网-宝宝起名取名测名字周易八字测算网 | 岸电电源-60HZ变频电源-大功率变频电源-济南诚雅电子科技有限公司 | 高低温万能试验机_拉力试验机_拉伸试验机-馥勒仪器科技(上海)有限公司 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 英思科GTD-3000EX(美国英思科气体检测仪MX4MX6)百科-北京嘉华众信科技有限公司 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 博博会2021_中国博物馆及相关产品与技术博览会【博博会】 | 【灵硕展览集团】展台展会设计_展览会展台搭建_展览展示设计一站式服务公司 | 传爱自考网_传爱自学考试网 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | 齿轮减速机电机一体机_齿轮减速箱加电机一体化-德国BOSERL蜗轮蜗杆减速机电机生产厂家 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 高硼硅玻璃|水位计玻璃板|光学三棱镜-邯郸奥维玻璃科技有限公司 高温高压釜(氢化反应釜)百科 | 门禁卡_智能IC卡_滴胶卡制作_硅胶腕带-卡立方rfid定制厂家 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 便携式XPDM露点仪-在线式防爆露点仪-增强型烟气分析仪-约克仪器 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 |