admin管理员组文章数量:1302293
I googled a long time to find this snippet.
Is there a way to convert the snippet that shows a button in the front end instead of the full link? For example, "BUY NOW". Clicking on this would open the external page in a new browser window. The css for the buttons would come from the basic theme.
// 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);
$html = '<a href="'.$external_link.'" class="custom-button-class" target="_blank" title="'.__('External product link','woocommerce').'">'.$external_link.'</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');
I googled a long time to find this snippet.
Is there a way to convert the snippet that shows a button in the front end instead of the full link? For example, "BUY NOW". Clicking on this would open the external page in a new browser window. The css for the buttons would come from the basic theme.
// 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);
$html = '<a href="'.$external_link.'" class="custom-button-class" target="_blank" title="'.__('External product link','woocommerce').'">'.$external_link.'</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');
Share
Improve this question
edited Mar 3, 2020 at 23:15
7uc1f3r
2292 silver badges7 bronze badges
asked Mar 3, 2020 at 14:00
Peter LangPeter Lang
11 bronze badge
1 Answer
Reset to default 0As you can see in the wpse_add_custom_link_output
callback function this echo the $html
variable.
So you can simply replace the following line
$html = '<a href="'.$external_link.'" class="custom-button-class" target="_blank" title="'.__('External product link','woocommerce').'">'.$external_link.'</a>';
With something like
$html = '<button type="button">BUY NOW</button>';
本文标签: woocommerce ad custom button with custom link after add to cart for every product
版权声明:本文标题:woocommerce ad custom button with custom link after add to cart for every product 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741681581a2392203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论