gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:35:44 24 4
gpt4 key购买 nike

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

// 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', 'woocommerce'),
'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, '_custom_product_text_field',
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', 10, 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', 10, 2 );

function save_custom_field_variations( $variation_id, $i ) {
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', 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' ], 'custom_field', 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}}}

关于php - 在 WooCommerce 中将选定的变体自定义字段显示为 pdf 链接文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66973344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com