将 WooCommerce 中选定的变体自定义字段显示为 pdf 链接文件

如何解决将 WooCommerce 中选定的变体自定义字段显示为 pdf 链接文件

我希望能够为我的产品添加带有文档的外部链接。使用此代码,我为简单的产品添加了它,它可以按我的意愿工作:

// This function gets the value for the the custom fields from the database and adds it to the frontend output function
function wpse_add_custom_link_output() {
    $external_link = get_post_meta(get_the_ID(),'_custom_product_text_field',true);
    if ($external_link !== "")
        {
    $html = '<a href="'.$external_link.'" class="custom-button-class" target="_blank"><img src="https://pdfimage.png" width="100" height="100" alt="pdf_logo.png" title="'.__('External product link','woocommerce').'">Product Datasheet</a>';
    echo $html;
        }
};
add_action( 'woocommerce_after_add_to_cart_button','wpse_add_custom_link_output',10,0 ); 
// This function creates the field in the backend
function wpse_add_custom_link_field(){
    global $woocommerce,$post;
    echo '<div class="product_custom_field">';
    // Custom Product Text Field
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field','placeholder' => __('Paste product link here','woocommerce'),'label' => __('Custom product link','desc_tip' => 'true'
        )
    );
    echo '</div>';
}
add_action('woocommerce_product_options_general_product_data','wpse_add_custom_link_field');
// this function saves the link/text field
function wpse_save_custom_link_field($post_id){
    // Custom Product Text Field
    $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
    if (!empty($woocommerce_custom_product_text_field))
    update_post_meta($post_id,esc_attr($woocommerce_custom_product_text_field));
}
add_action('woocommerce_process_product_meta','wpse_save_custom_link_field');

我必须为要放置此类链接的变体添加相同的内容。此代码创建一个自定义字段,我可以在其中填写链接:

```// -----------------------------------------
// 1. Add custom field input @ Product Data > Variations > Single Variation
 
add_action( 'woocommerce_variation_options_pricing','add_custom_field_to_variations',3 );
 
function add_custom_field_to_variations( $loop,$variation_data,$variation ) {
   woocommerce_wp_text_input( array(
'id' => 'custom_field[' . $loop . ']','class' => 'short','label' => __( 'Custom Field','woocommerce' ),'value' => get_post_meta( $variation->ID,'custom_field',true )
   ) );
}
 
// -----------------------------------------
// 2. Save custom field on product variation save
 
add_action( 'woocommerce_save_product_variation','save_custom_field_variations',2 );
 
function save_custom_field_variations( $variation_id,$i ) {
   $custom_field = $_POST['custom_field'][$i];
   if ( isset( $custom_field ) ) update_post_meta( $variation_id,esc_attr( $custom_field ) );
}
 
// -----------------------------------------
// 3. Store custom field value into variation data
 
add_filter( 'woocommerce_available_variation','add_custom_field_variation_data' );
 
function add_custom_field_variation_data( $variations ) {
   $variations['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ],true ) . '</span></div>';
   return $variations;
}

为了在前端显示它,我编辑:woocommerce/single-product/add-to-cart/variation.php,添加这一行:{{{data.variation.custom_field}}}

我的问题是它以纯文本形式出现 - 自定义字段:https://mylink.com 我正在寻找一种方法来使其成为可点击链接,并将链接本身替换为 PDF 文件或按钮的图像。

解决方法

对于您的产品变体,请将您的最后一个功能替换为以下内容:

add_filter( 'woocommerce_available_variation','add_custom_field_value_to_variation_data',10,3 );
function add_custom_field_value_to_variation_data( $variation_data,$product,$variation ) {
    $variation_data['custom_field'] = $variation->get_meta('custom_field');

    if( ! empty($variation_data['custom_field']) ) {
        $variation_data['custom_field_html'] = '<div class="woocommerce_custom_field"><a href="' . $variation_data['custom_field'] . '" class="custom-button-class" target="_blank"><img src="https://pdfimage.png" width="100" height="100" alt="pdf_logo.png" title="' . __("External product link","woocommerce") . '" />' . __("Product Datasheet","woocommerce") . '</a></div>';
    }
    return $variation_data;
}

然后在 variations.php 模板文件中,将 {{{data.variation.custom_field}}} 替换为:

 {{{data.variation.custom_field_html}}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-