【发布时间】:2021-07-21 01:42:04
【问题描述】:
我正在尝试根据产品 ID:s(数组)重命名订单备注标签和占位符。但由于某种原因,这不起作用。无论购物车中有什么产品,文本都会保持原样,并且我遇到了参数错误。
这是我根据现有答案代码定制的代码:
add_filter( 'woocommerce_checkout_fields', 'rename_order_notes_based_on_product', 10, 2 );
function rename_order_notes_based_on_product( $fields, $cart ) {
$product_ids = array(11, 18);
$found_ids = array();
foreach ( WC()->cart->get_cart() as $cart_item ) {
foreach( $product_ids as $product_id ) {
if ( in_array( $product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found_ids[$product_id] = $product_id;
break;
}
}
}
if ( count( $found_ids ) === count( $product_ids ) ) {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here';
$fields['order']['order_comments']['label'] = 'Label text here';
}
else {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here';
$fields['order']['order_comments']['label'] = 'Label text here';
}
return $fields;
}
【问题讨论】:
标签: php wordpress woocommerce field checkout