以编程方式将可下载文件添加到Woocommerce产品 [英] Add programmatically a downloadable file to Woocommerce products

查看:114
本文介绍了以编程方式将可下载文件添加到Woocommerce产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的解决方案中,我想尽可能地使产品创建自动化. 我认为,一种省时的方法是将可下载文件自动添加到产品中.

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元标记"title"中获取它,并且在保存产品之前或保存时将其设置为产品名称. ($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'];)

推荐答案

更新2 (添加了if语句,以允许仅生成一个下载文件 )

以下代码将自动添加由产品图片制成的可下载文件(下载标题来自EXIF数据标题).

您最好使用专用的woocommerce_admin_process_product_object操作挂钩和可用的 CRUD woocommerce 3引入的对象和getters/setter方法是这样的:

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语句中.

这篇关于以编程方式将可下载文件添加到Woocommerce产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆