产品设置中的自定义复选框,选中时显示自定义字段

2023-12-12

我目前正在使用 WooCommerce 开发 WordPress 电子商务网站。

我在产品编辑页面常规设置中创建了一个自定义复选框。

我还有一个代码片段,用于在单个产品页面中显示自定义文本字段。

现在,我希望当为产品选中此自定义复选框(在其设置中)时,在单个产品页面中显示此自定义文本字段。

到目前为止我的编码:

为了在 WooCommerce 产品仪表板中创建复选框,我已将以下代码输入到functions.php file:

<?php
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');


function woocommerce_product_custom_fields(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    // Custom Product Checkbox Field
    woocommerce_wp_checkbox(
        array(
            'id'          => '_custom_product_checkbox_field',
            'placeholder' => 'Custom Product Checkbox Field',
            'label'       => __('Custom Product Checkbox Field', 'woocommerce'),
            'desc_tip'    => 'true'
        )
    );
echo '</div>';

}

// Saving Values
function woocommerce_product_custom_fields_save($post_id){
    // Custom Product Text Field
    $woocommerce_custom_product_checkbox_field = $_POST['_custom_product_checkbox_field'];
    if (!empty($woocommerce_custom_product_checkbox_field ))
        update_post_meta($post_id, '_custom_product_checkbox_field', esc_attr($woocommerce_custom_product_checkbox_field ));
}
?>

我放置的编码,在functions.php文件中,关于输出相关的自定义文本框,如下:

function add_engrave_text_field() {
    if (is_single('product-url-a')) {
        echo 'Enter your chosen letters:  <span id="character_count"></span>
        <div><table class="variations" cellspacing="0">
            <tbody>
                <tr>
                    <td class="value"><label class="product-custom-text-label" for="custom_text">Custom Text</label></td>
                    <td class="value">
                        <label><input type="text" class="product-counter" name="engrave_text" placeholder="Enter Your Custom Letters ..." maxlength="3" /></label>                        
                    </td>
                </tr>                             
            </tbody>
        </table></div>';
    }
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 ); 

我的下一步是什么,以便仅在选择复选框时才输出自定义文本框编码?

我意识到我可以将自定义文本框的代码放入content-single-product,但是我理想情况下不想这样做,因为它是一大组编码的一部分,该编码计算出自定义刻字等的定价。

附加问题:

该网站由 5 个产品类别组成。只有这些类别之一的产品才允许自定义刻字。目前,我必须手动将上述编码应用于每个单独的产品,因为我必须将单独的块插入到if (is_single('product-slug-a'))有没有办法可以更改“product-slug-a”,这样我只需在其中输入一次代码functions.php文件,并且只为一个产品类别中的每个产品调用它?


Updated:

我重新审视了你的代码,简化了事情,删除了不必要的代码。我添加了必要的代码,以使此“雕刻字段”仅在选中复选框设置选项时才显示在单个产品页面上。

对于你的最后一个问题,请在最后检查(对于没有复选框设置的产品类别).

这是代码:

// Display Fields
add_action('woocommerce_product_options_general_product_data', 'product_custom_fields_add');
function product_custom_fields_add(){
    global $post;

    echo '<div class="product_custom_field">';

    // Custom Product Checkbox Field
    woocommerce_wp_checkbox( array(
        'id'        => '_engrave_text_option',
        'desc'      => __('set custom Engrave text field', 'woocommerce'),
        'label'     => __('Display custom Engrave text field', 'woocommerce'),
        'desc_tip'  => 'true'
    ));

    echo '</div>';
}

// Save Fields
add_action('woocommerce_process_product_meta', 'product_custom_fields_save');
function product_custom_fields_save($post_id){
    // Custom Product Text Field
    $engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : 'no';
        update_post_meta($post_id, '_engrave_text_option', esc_attr( $engrave_text_option ));
}

add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
function add_engrave_text_field() {
    global $post;

    // If is single product page and have the "engrave text option" enabled we display the field
    if ( is_product() && get_post_meta( $post->ID, '_engrave_text_option', true ) == 'yes' ) {

        ?>
        <div>
            <label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
                <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" />
            </label>
        </div><br>
        <?php
    }
}

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

经过测试并有效。

复选框输出并保存:以编程方式添加数据选项选项卡到管理产品页面 Metabox

在产品设置中选中复选框选项后,您将看到类似以下内容:

enter image description here


让它起作用对于产品类别或产品标签 (没有设置复选框):

add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
function add_engrave_text_field() {
    global $post;

    // HERE set the term Ids, slug or name (or an array of values)
    $categories = array( 'clothing', 'music' );

    $taxonomy = 'product_cat'; // (For product tags use 'product_tag' instead)

    // If is single product page and have the "engrave text option" enabled we display the field
    if ( is_product() && has_term( $categories, 'product_cat', $post->ID ) ) {

        ?>
        <div>
            <label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
                <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" />
            </label>
        </div><br>
        <?php
    }
}

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

经过测试并有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

产品设置中的自定义复选框,选中时显示自定义字段 的相关文章

随机推荐

  • 无法找到管道“ ” Angular2 自定义管道

    我似乎无法修复这个错误 我有一个搜索栏和一个 ngFor 我正在尝试使用这样的自定义管道来过滤数组 import Pipe PipeTransform from angular core import User from user user
  • Java:运行JAR文件时如何获取文件的路径

    当我使用相对路径时 我可以从 Eclipse 运行我的 Java 程序 但是当我将它作为 JAR 文件运行时 该路径不再起作用 在我的 src components SettingsWindow java 中 我有 ObjectInputS
  • 如何在 pyqt 中使用 Qtableview /QAbstractTableModel 存储和检索自定义数据(使用 QtCore.Qt.UserRole?)?

    我对模型 视图框架的使用还很陌生 并且遇到了一些麻烦 我在小部件上使用 Qtableview 和 QAbstractTableModel 我试图使用 QAbstractTableModel 中的 data 方法在 QModelIndex 中
  • 实数、浮点数、货币

    为什么当我在 SQL Server 中将 40 54 的值保存到 Real 类型的列时 它返回给我的值更像是 40 53999878999 而不是 40 54 我已经见过几次这种情况 但一直不明白为什么会发生这种情况 有其他人遇到过这个问题
  • OpenCV。匹配时绘制矩形

    我使用 OpenCv 来查找与参考图像上的模板匹配的区域 当代码找到与模板匹配的区域时 在该区域周围绘制一个矩形 但我想要的是当代码找不到该区域时 代码不会绘制任何矩形 code IplImage res CvPoint minloc ma
  • R-markdown 自包含

    我正在使用 R markdown 来编写一些报告 我用独立的图形来做 因为我通过电子邮件发送它 这一直有效直到最近 但现在图像不再显示 方框中出现十字错误 可能是我更新了RStudio的版本 我在脚本的顶部使用了这个 title blabl
  • Fluent NHibernate Composite ID表问题

    我对 nhibernate 有点陌生 遇到了一个问题 我有以下表格 Table 1 我在使用复合 id 做事时遇到了很多问题 例如this 我建议做我所做的事情 即创建一个新类型 它只包含复合 id 用于 id 的内容 然后像这样映射它 C
  • Objective-C中如何将字节值转换为int

    请告诉我如何在 iPhone 编程中将 Objective C 中的字节转换为 NSInteger int 字节 是什么意思 如果要将表示整数值的单字节转换为 int 或 NSInteger 类型 只需使用 Byte b 123 NSInt
  • 全套组合组合 3 套

    我需要生成组合三个不同子集所获得的完整组合集 Set 1 从 13 个元素的向量中选择任意 4 个数字 Set 2 从 3 个元素的向量中选择任意 2 个数字 Set 3 从 9 个元素的向量中选择任意 2 个数字 示例 A 组的向量 4
  • 将 DataContractSurrogate 与 WCF REST 结合使用

    如何将 DataContractSurrogate 用于我的 WCF REST 服务 使用 WebServiceHostFactory 托管 我没有看到添加一个的方法 即使我添加自定义 IOperationBehavior WebServi
  • 如何用C语言为AVR-Studio编写自定义reset()函数?

    所以我被分配了为 AVR 编译编写自定义 Reset 函数的任务 我得到了这个信息 Atmega128 和 Pic24e 在程序地址 0x0 处有复位中断 编写一个函数 Reset 来复位程序 我还听说强制系统重新启动的一个简单方法是发送它
  • 从多个源文件构建内核模块,其中一个源文件与模块同名

    是否可以从多个源文件构建一个内核模块 其中一个源文件与该模块同名 例如 我想使用以下源文件构建 mymodule ko mymodule cmymodule func c 这个 makefile 不起作用 Makefile obj m my
  • UILabel + IRR、KRW 和 KHR 货币符号错误

    我在将韩元 柬埔寨瑞尔和伊朗里亚尔的十进制转换为货币并将结果显示到 UILabel 文本时遇到问题 转换本身顺利进行 我可以在调试器中看到正确的货币符号 甚至 NSLog 也能很好地打印该符号 如果我将此 NSString 实例分配给 UI
  • ListView项目背景地狱

    因为复选框不是我的项目的选项 所以我希望可选项在选中时有背景 从 2 3 开始支持 我还没有设法解决这个问题 选择是正确的 但我在屏幕上看到的不是 随机行的随机颜色 拳头我有这个
  • React Router 无法与 Github Pages 一起使用

    我以前的网站仅在单击主页选项卡时显示主页 然后如果您单击我的导航栏品牌 它会显示 404 该网站在带有 npm start 的 create react app 上运行 但在这里不起作用 也不起作用在构建上 我不知道该应用程序出了什么问题
  • 使用 Apache poi 在堆叠条上方显示 SUM 值

    我目前正在研究功能 应该在 pptx 文件内生成堆叠图表 为此 我使用这里的代码 java 使用 APACHE POI 在 powerpoint 中创建图表 我做了一些修改 主要是我将分组设置为 堆叠 并将重叠设置为 100 因此子栏看起来
  • 测试 jQuery 可选择 capybara 或 selenium (ctrl + click)

    我正在使用 jQuery Selectable 来管理日历 这个功能很好用 只需进入测试自动化即可 我需要从日历网格中选择多个不连续的日期 我尝试了一些方法 但并不真正期望它们能起作用 date 2013 05 02 page execut
  • Verilog HDL ?操作员

    什么是 用 Verilog 做什么 例如 以下命令是什么意思 input first din input 7 0 din output 127 0 parity reg 127 0 parity wire 7 0 feedback assi
  • 排除 $lookup 聚合中的字段

    我正在查询 3 个要排除的集合 id输出中随处可见 我的输出是 id ObjectId 5b6aed5f9bcdb5d4ae64aef5 userID 1 skills id ObjectId 5b766b5f1365a4940bb6050
  • 产品设置中的自定义复选框,选中时显示自定义字段

    我目前正在使用 WooCommerce 开发 WordPress 电子商务网站 我在产品编辑页面常规设置中创建了一个自定义复选框 我还有一个代码片段 用于在单个产品页面中显示自定义文本字段 现在 我希望当为产品选中此自定义复选框 在其设置中