How to add a customizable text after WooCommerce add to cart button(如何在WooCommerce添加到购物车按钮后添加可定制文本)
本文介绍了如何在WooCommerce添加到购物车按钮后添加可定制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在WooCommerce中的"添加到购物车"按钮之后添加自定义文本元素。
我尝试使用functions.php
add_action( 'woocommerce_after_add_to_cart_button', 'inserisce_testo_dopobottone' );
function inserisce_testo_dopobottone() {
echo '<div class="second_content">New text</div>';
}
这是可行的,但现在我需要每个页面的文本都是可自定义的。 这可能吗?
推荐答案
您可以通过woocommerce_product_options_general_product_data
操作挂钩添加自定义字段,这会将新字段添加到产品数据元数据的常规选项卡
基于此,您可以根据产品显示不同的消息,或显示默认消息
因此您得到:
// Add to general_product_data tab
function action_woocommerce_product_options_general_product_data() {
// Text field
woocommerce_wp_text_input( array(
'id' => '_my_custom_text',
'label' => __( 'My custom text', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Add your custom text', 'woocommerce' ),
'desc_tip' => true,
));
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
// Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset
if ( isset( $_POST['_my_custom_text'] ) ) {
// Update
$product->update_meta_data( '_my_custom_text', sanitize_text_field( $_POST['_my_custom_text'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
// Display after add to cart button
function action_woocommerce_after_add_to_cart_button() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$message = $product->get_meta( '_my_custom_text' );
// When Empty, use default message
if ( empty ( $message ) ) {
$message = __( 'Default text', 'woocommerce' );
}
echo '<div class="second_content">' . $message . '</div>';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button' );
这篇关于如何在WooCommerce添加到购物车按钮后添加可定制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在WooCommerce添加到购物车按钮后添加可定制文本
基础教程推荐
猜你喜欢
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01