【发布时间】:2019-10-13 00:56:00
【问题描述】:
我在 Woocommerce 中有一个免费产品,并使用 Skyverge 的本教程来简化结帐流程:https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/
问题是,对于所有其他产品,我们在“附加信息”选项卡中有 3 个必填字段:
cracha_empresa cracha_primeironome cracha_sobrenome
我们一起删除了“order_notes”。为此,我们使用了 Woocommerce Checkout Field Editor。
当我们尝试完成此免费产品的结帐流程时,过滤器 add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );正在删除附加字段(如预期的那样),但是当您尝试完成购买时,我收到一条错误消息,提示需要填写上述字段,即使它们没有显示。
据我猜测,我需要在数组中过滤这些字段?
/** * 删除 os cupons、notas、e campos que não são necessários parapalestrantes。 * */ 函数 sv_free_checkout_fields() {
// Se carrinho precisa de pagamento, não fazer nada
if ( WC()->cart && WC()->cart->needs_payment() ) {
return;
}
// Continuar somente se estamos no checkout
// is_checkout() foi quebrado em WooCommerce 3.2 no ajax, checkar se is_ajax está ativo
if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) {
// Remove cupons para produtos gratuitos
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
// Remove o campo "Additional Info" nas notas dos pedidos
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Desativa os campos para produtos gratuitos
function unset_unwanted_checkout_fields( $fields ) {
// Adiciona aqui o que deseja remover
// campos: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
$billing_keys = array(
'billing_persontype',
'billing_cpf',
'billing_rg',
'billing_cnpj',
'billing_company',
'billing_phone',
'billing_cellphone',
'billing_address_1',
'billing_address_2',
'billing_neighborhood',
'billing_city',
'billing_postcode',
'billing_country',
'billing_state',
'billing_number',
);
// unset each of those unwanted fields
foreach( $billing_keys as $key ) {
unset( $fields['billing'][ $key ] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'unset_unwanted_checkout_fields' );
}
} add_action('wp', 'sv_free_checkout_fields');
我试过这个没有运气:
/** * 删除 os cupons、notas、e campos que não são necessários parapalestrantes。 * */ 函数 sv_free_checkout_fields() {
// Se carrinho precisa de pagamento, não fazer nada
if ( WC()->cart && WC()->cart->needs_payment() ) {
return;
}
// Continuar somente se estamos no checkout
// is_checkout() foi quebrado em WooCommerce 3.2 no ajax, checkar se is_ajax está ativo
if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) {
// Remove cupons para produtos gratuitos
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
// Remove o campo "Additional Info" nas notas dos pedidos
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Desativa os campos para produtos gratuitos
function unset_unwanted_checkout_fields( $fields ) {
// Adiciona aqui o que deseja remover
// campos: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
$billing_keys = array(
'billing_persontype',
'billing_cpf',
'billing_rg',
'billing_cnpj',
'billing_company',
'billing_phone',
'billing_cellphone',
'billing_address_1',
'billing_address_2',
'billing_neighborhood',
'billing_city',
'billing_postcode',
'billing_country',
'billing_state',
'billing_number',
);
$order_keys = array(
'cracha_empresa',
'cracha_primeironome',
'cracha_sobrenome',
);
// unset each of those unwanted fields
foreach( $billing_keys as $key ) {
unset( $fields['billing'][ $key ] );
}
// unset each of those unwanted fields
foreach( $order_keys as $key ) {
unset( $fields['order'][ $key ] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'unset_unwanted_checkout_fields' );
}
} add_action('wp', 'sv_free_checkout_fields');
【问题讨论】:
标签: php wordpress woocommerce hook-woocommerce