WooCommerce是一款流行的WordPress电子商务插件,它提供了丰富的功能和灵活的扩展性。在新产品上设置自定义产品meta可以通过以下步骤实现:
woocommerce_product_options_general_product_data
钩子函数在产品编辑页面添加自定义字段。具体代码如下:// 添加自定义产品meta字段
add_action('woocommerce_product_options_general_product_data', 'add_custom_product_meta_field');
function add_custom_product_meta_field() {
global $woocommerce, $post;
echo '<div class="options_group">';
// 添加自定义字段
woocommerce_wp_text_input(
array(
'id' => '_custom_meta_field',
'label' => __('Custom Meta Field', 'woocommerce'),
'placeholder' => __('Enter custom meta field', 'woocommerce'),
'desc_tip' => 'true',
'description' => __('Enter the custom meta field for the product.', 'woocommerce')
)
);
echo '</div>';
}
// 保存自定义产品meta字段值
add_action('woocommerce_process_product_meta', 'save_custom_product_meta_field');
function save_custom_product_meta_field($post_id) {
$custom_meta_field_value = isset($_POST['_custom_meta_field']) ? sanitize_text_field($_POST['_custom_meta_field']) : '';
update_post_meta($post_id, '_custom_meta_field', $custom_meta_field_value);
}
上述代码将在产品编辑页面添加一个名为"Custom Meta Field"的自定义字段。
// 显示自定义产品meta字段值
add_action('woocommerce_single_product_summary', 'display_custom_product_meta_field', 25);
function display_custom_product_meta_field() {
global $product;
$custom_meta_field_value = get_post_meta($product->get_id(), '_custom_meta_field', true);
if (!empty($custom_meta_field_value)) {
echo '<p><strong>' . __('Custom Meta Field', 'woocommerce') . ':</strong> ' . $custom_meta_field_value . '</p>';
}
}
上述代码将在产品页面的产品摘要部分显示自定义产品meta字段的值。
通过以上步骤,你可以成功在新产品上设置自定义产品meta。请注意,这只是一个示例,你可以根据自己的需求进行修改和扩展。
关于WooCommerce的更多信息和详细文档,请参考腾讯云的WooCommerce产品介绍页面:WooCommerce产品介绍
领取专属 10元无门槛券
手把手带您无忧上云