请帮忙!!我需要一个woocommerce签出字段,仅在产品x(产品179,如下所示)不在用户购物车中时显示。实际上,我可以使用默认的woocommerce字段(源于[账单]、[发货]、[帐户]和[订单])使其正常工作……但我需要它来处理我创建的自定义结帐字段,如下所示:
/**
* Add the field to the checkout (the script)
*/
add_action( \'woocommerce_after_order_notes\', \'the_script\' );
function the_script( $checkout ) {
echo \'<div id="the_script"><h2>\' . __(\'\') . \'</h2>\';
woocommerce_form_field( \'user_script\', array(
\'type\' => \'textarea\',
\'class\' => array(\'extracheckoutinfo form-row-wide\'),
\'label\' => __(\'What is the script?\'),
\'placeholder\' => __(\'You can skip this section if you added the "Create the Script" extra to your purchase\'),
\'required\' => true
), $checkout->get_value( \'user_script\' ));
echo \'</div>\';
}
/**
* Process the checkout (the script)
*/
add_action(\'woocommerce_checkout_process\', \'the_script_field_process\');
function the_script_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST[\'user_script\'] )
wc_add_notice( __( \'Provide<strong>the script you want to use.</strong>\' ), \'error\' );
}
/**
* Update the order meta with field value (the script)
*/
add_action( \'woocommerce_checkout_update_order_meta\', \'the_script_field_update_order_meta\' );
function call_script_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST[\'user_script\'] ) ) {
update_post_meta( $order_id, \'The Script\', sanitize_text_field( $_POST[\'user_script\'] ) );
}
}
/**
* Display field value on the order edit page (the script)
*/
add_action( \'woocommerce_admin_order_data_after_billing_address\', \'the_script_field_display_admin_order_meta\', 10, 1 );
function the_script_field_display_admin_order_meta($order){
echo \'<p><strong>\'.__(\'The Script\').\':</strong> \' . get_post_meta( $order->id, \'The Script\', true ) . \'</p>\';
}
/**
* Check if a specific product ID is in the cart
*/
function wc_ninja_product_is_in_the_cart() {
// Add your special product IDs here
$ids = array( \'179\' );;
// Products currently in the cart
$cart_ids = array();
// Find each product in the cart and add it to the $cart_ids array
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values[\'data\'];
$cart_ids[] = $cart_product->id;
}
// If one of the special products are in the cart, return true.
if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) {
return true;
} else {
return false;
}
}
/**
* Conditionally remove a checkout field based on products in the cart
*/
function wc_ninja_remove_checkout_field( $fields ) {
if ( wc_ninja_product_is_in_the_cart() ) {
unset( $fields[\'billing\'][\'billing_first_name\'] );
}
return $fields;
}
add_filter( \'woocommerce_checkout_fields\' , \'wc_ninja_remove_checkout_field\' );
所以基本上,我想我需要改变
unset( $fields[\'billing\'][\'billing_first_name\'] );
大概是
unset( $fields[\'the_script\'][\'user_script\'] );
但当然不是这样的。我不知道如何访问为此创建的自定义字段。已经尝试了好几个小时,但运气不好!!!有人请帮忙
最合适的回答,由SO网友:kosmicbird 整理而成
好吧,我终于明白了
我没有使用上面的代码添加新的自定义字段,而是将其更改为:
add_filter( \'woocommerce_checkout_fields\', \'custom_override_checkout_fields\' );
function custom_override_checkout_fields ( $fields ) {
$fields[\'order\'][\'the_script\'] = array (
woocommerce_form_field ( \'the_script\', array(
\'type\' => \'textarea\',
\'class\' => array(\'extracheckoutinfo form-row-wide\'),
\'label\' => __(\'What is the script?\'),
\'placeholder\' => __(\'You can skin this section if you added the "script" extra to your purchase\'),
\'required\' => true
);
return $fields
}
现在,我可以将自定义字段作为目标,如下所示:
unset ($fields[\'order\'][\'the_script\'] );