我目前正在向我的WooCommerce产品页面成功添加一个字段,该字段显示了以下值:
问题:它没有在管理订单"自定义字段"Metabox中显示为自定义字段,其中包含值,但仅作为订单页面中的文本.
这是我的工作代码:
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
echo '<label>fill in this field</label> <input type="text" name="my_field_name">';
echo '</div>';
}
// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['my_field_name'] ) ) {
$cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( …Run Code Online (Sandbox Code Playgroud) 我已经通过function.php为我的产品页面创建了一个自定义保修字段.
add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );
function test_custom_fields() {
// Print a custom text field
woocommerce_wp_text_input( array(
'id' => '_warranty',
'label' => 'i.e. 15 years',
'description' => '',
'desc_tip' => 'true',
'placeholder' => 'i.e. 15 years'
) );
}
add_action( 'woocommerce_process_product_meta', 'test_save_custom_fields' );
function test_save_custom_fields( $post_id ) {
if ( ! empty( $_POST['_warranty'] ) ) {
update_post_meta( $post_id, '_warranty', esc_attr( $_POST['_warranty'] ) );
}
}
Run Code Online (Sandbox Code Playgroud)
我想在管理订单页面的自生成自定义字段中"复制"带有键和值的自定义字段,具体取决于购物车/订单中的产品(无插件).
因此,通过订单页面上的这个自定义字段,我终于可以使用WooCommerce PDF Invoice插件在我的pdf发票中显示"保修".
另一种解释:
{{_warranty}}WooCommerce PDF Invoice插件显示"保修期:15年"非常感谢您的帮助.
我刚刚测试了以下案例: …