根据产品 ID 更改 WooCommerce 结帐订单备注占位符和标签
Change WooCommerce checkout order notes placeholder and label based on product Ids
我正在尝试根据产品 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;
}
您的代码中存在一些错误,请尝试以下操作:
add_filter( 'woocommerce_checkout_fields', 'rename_order_notes_based_on_product' );
function rename_order_notes_based_on_product( $fields ) {
$targeted_ids = array(11, 18); // Here set your targeted product Ids
$found = false; // Initializing
foreach ( WC()->cart->get_cart() as $item ) {
if ( array_intersect( $targeted_ids, array( $item['product_id'], $item['variation_id'] ) ) ) {
$found = true;
break;
}
}
if ( $found ) {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here (found)';
$fields['order']['order_comments']['label'] = 'Label text here (found)';
} else {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here (Not found)';
$fields['order']['order_comments']['label'] = 'Label text here (Not found)';
}
return $fields;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该更好用。
或者,如果您想要更改订单备注字段占位符和标签 仅当所有目标产品 ID 都在购物车中,请改用以下内容:
add_filter( 'woocommerce_checkout_fields', 'rename_order_notes_based_on_product' );
function rename_order_notes_based_on_product( $fields ) {
$targeted_ids = array(11, 18); // Here set your targeted product Ids
$found_ids = array(); // Initializing
foreach ( WC()->cart->get_cart() as $item ) {
if ( array_intersect( $targeted_ids, array( $item['product_id'], $item['variation_id'] ) ) ) {
$found_ids[$item['data']->get_id()] = $item['data']->get_id();
}
}
if ( count($found_ids) === count($targeted_ids) ) {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here (match)';
$fields['order']['order_comments']['label'] = 'Label text here (match)';
} else {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here (Not match)';
$fields['order']['order_comments']['label'] = 'Label text here (Not match)';
}
return $fields;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它也应该有效。
我正在尝试根据产品 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;
}
您的代码中存在一些错误,请尝试以下操作:
add_filter( 'woocommerce_checkout_fields', 'rename_order_notes_based_on_product' );
function rename_order_notes_based_on_product( $fields ) {
$targeted_ids = array(11, 18); // Here set your targeted product Ids
$found = false; // Initializing
foreach ( WC()->cart->get_cart() as $item ) {
if ( array_intersect( $targeted_ids, array( $item['product_id'], $item['variation_id'] ) ) ) {
$found = true;
break;
}
}
if ( $found ) {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here (found)';
$fields['order']['order_comments']['label'] = 'Label text here (found)';
} else {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here (Not found)';
$fields['order']['order_comments']['label'] = 'Label text here (Not found)';
}
return $fields;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该更好用。
或者,如果您想要更改订单备注字段占位符和标签 仅当所有目标产品 ID 都在购物车中,请改用以下内容:
add_filter( 'woocommerce_checkout_fields', 'rename_order_notes_based_on_product' );
function rename_order_notes_based_on_product( $fields ) {
$targeted_ids = array(11, 18); // Here set your targeted product Ids
$found_ids = array(); // Initializing
foreach ( WC()->cart->get_cart() as $item ) {
if ( array_intersect( $targeted_ids, array( $item['product_id'], $item['variation_id'] ) ) ) {
$found_ids[$item['data']->get_id()] = $item['data']->get_id();
}
}
if ( count($found_ids) === count($targeted_ids) ) {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here (match)';
$fields['order']['order_comments']['label'] = 'Label text here (match)';
} else {
$fields['order']['order_comments']['placeholder'] = 'Placeholder text here (Not match)';
$fields['order']['order_comments']['label'] = 'Label text here (Not match)';
}
return $fields;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它也应该有效。