1 回答
TA贡献1830条经验 获得超9个赞
您的代码中有一些错误。请尝试以下操作:
add_action( 'woocommerce_product_options_general_product_data', 'add_admin_product_custom_field' );
function add_admin_product_custom_field() {
woocommerce_wp_text_input( array(
'id' => 'validity_field',
'label' => __( 'Ważność konta', 'waznosc' ),
'class' => 'waznosc-custom-field',
'desc_tip' => true,
'description' => __( 'Wprowadz ilosc dni waznosci konta', 'waznosc' ),
) );
}
add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_field_value' );
function save_product_custom_field_value( $product ) {
$value = isset( $_POST['validity_field'] ) ? sanitize_text_field($_POST['validity_field']) : '';
$product->update_meta_data( 'validity_field', $value );
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_data_to_order_item', 10, 4 );
function add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
$product = wc_get_product( $item->get_product_id() ); // The WC_Product Object (and the parent variable product object for product variations)
$value = $product->get_meta('validity_field');
if( ! empty( $value ) ) {
$item->update_meta_data( __( 'Waznosc konta', 'waznosc' ), $value );
}
}
add_action( 'woocommerce_order_status_completed', 'action_completed_order', 10, 2 );
function action_completed_order( $order_id, $order ) {
$user_id = $order->get_customer_id();
$user = $order->get_user(); // The WP_User Object (if needed)
foreach ( $order->get_items() as $item_id => $item ) {
$validity = $item->get_meta( 'validity_field' );
if ( ! empty($validity) ) {
// Do something with $validity
}
}
}
这次应该可以了。
- 1 回答
- 0 关注
- 80 浏览
添加回答
举报