【发布时间】:2018-05-07 21:09:51
【问题描述】:
我目前正在使用 WooCommerce 开发 WordPress 电子商务网站。
我在产品编辑页面常规设置中创建了一个自定义复选框。
我还有一个代码 sn-p,它在单个产品页面中显示自定义文本字段。
现在我想在为产品(在其设置中)选中此自定义复选框时,在单个产品页面中显示此自定义文本字段。
我目前的编码:
要在 WooCommerce 产品仪表板中创建复选框,我已将以下代码输入到 functions.php 文件中:
<?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 个产品类别组成。只有这些类别之一的产品允许自定义刻字。目前,我必须手动将上述编码应用于每个单独的产品,因为我必须将单独的 slug 插入 if (is_single('product-slug-a')) 有没有办法可以更改“product-slug-a”,这样我只需要在functions.php 文件中输入一次代码,然后它会为仅一个产品类别中的每个产品调用?
【问题讨论】:
标签: php wordpress woocommerce product custom-fields