Change WooCommerce checkout order notes placeholder and label based on product Ids(根据产品ID更改WooCommerce结账订单备注占位符和标签)
本文介绍了根据产品ID更改WooCommerce结账订单备注占位符和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据产品ID:s(数组)重命名Order Notes标签和占位符。但出于某种原因,这并不管用。无论购物车中有什么产品,文本都保持不变,并且我收到一个参数错误。以下是我根据现有答案代码定制的代码:
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;
}
代码放在活动子主题(或活动主题)的函数.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;
}
代码放在活动子主题(或活动主题)的函数.php文件中。它也应该可以工作。
这篇关于根据产品ID更改WooCommerce结账订单备注占位符和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:根据产品ID更改WooCommerce结账订单备注占位符和标签
基础教程推荐
猜你喜欢
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01