php - WooCommerce:如果购物车中有特定的产品 ID,则显示自定义结帐字段
问题描述
我正在经营一家卖烤架的在线商店。只有烤架通过需要额外信息的特定承运人运输。
当购物车中有某个 product_ID 时,我设法显示了一个下拉列表。选择特定值时,将显示文本区域。
这应该发生在大约 10 种产品上,而不仅仅是一种。
在阅读了许多主题并在网上搜索后,我不知道如何添加多个产品 ID。
add_action( 'woocommerce_after_order_notes', 'grills_versandauswahl_checkout_field' );
function grills_versandauswahl_checkout_field( $checkout ) {
$grill_in_cart = grills_is_conditional_product_in_cart ( 125 );
if ( $grill_in_cart === true ) {
echo '<div id="my_custom_checkout_field"><h3>' . __( 'Versandoptionen' ) . '</h3><p style="margin: 0 0 8px;">text</p>';
woocommerce_form_field( 'versandoption', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __( 'Versandoptionen' ),
'required' => true,
'options' => array(
'blank' => __( 'Auswählen', 'wps' ),
'fixtermin' => __( 'Fixtermin', 'wps' ),
'avis' => __( 'Telefonisches Avis', 'wps' ),
)
), $checkout->get_value( 'versandoption' ) );
woocommerce_form_field( 'inscription_textbox', array(
'type' => 'textarea',
'class' => array( 'inscription-text form-row-wide' ),
'label' => __( 'Wunschtermin / Ablageort' ),
), $checkout->get_value( 'inscription_textbox' ) );
echo '</div>';
}
}
function grills_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$grill_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
//book is in cart!
$grill_in_cart = true;
}
}
return $grill_in_cart;
}
解决方案
要使您的条件函数适用于许多产品 ID,请使用以下内容:
// Custom conditional function
function grills_in_cart( $product_ids ) {
foreach ( WC()->cart->get_cart() as $item ) {
if ( array_intersect($product_ids, array( $item['product_id'], $item['variation_id']) ) ) {
return true;
}
}
return false;
}
// Conditionally display custom checkout fields for specific product IDS
add_action( 'woocommerce_after_order_notes', 'grills_versandauswahl_checkout_field' );
function grills_versandauswahl_checkout_field( $checkout ) {
// Here define the targeted product IDs in this array
$targeted_ids = array( 125, 132, 154 );
$text_domain = 'woocommerce';
if ( grills_in_cart( $targeted_ids ) ) {
echo '<div id="my_custom_checkout_field">
<h3>' . __( "Versandoptionen", $text_domain ) . '</h3>
<p style="margin: 0 0 8px;">' . __( "text", $text_domain ) . '</p>';
woocommerce_form_field( 'versandoption', array(
'type' => 'select',
'class' => array('wps-drop'),
'label' => __( "Versandoptionen", $text_domain ),
'required' => true,
'options' => array(
'blank' => __( "Auswählen", $text_domain ),
'fixtermin' => __( "Fixtermin", $text_domain ),
'avis' => __( "Telefonisches Avis", $text_domain ),
),
), $checkout->get_value( 'versandoption' ) );
woocommerce_form_field( 'inscription_textbox', array(
'type' => 'textarea',
'class' => array( 'inscription-text form-row-wide' ),
'label' => __( 'Wunschtermin / Ablageort', $text_domain ),
), $checkout->get_value( 'inscription_textbox' ) );
echo '</div>';
}
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。
注意:
$global $woocommerce;
+$woocommerce->cart
已被替换为WC()->cart
推荐阅读
- javascript - 如何在 ReactJS 中获取下拉菜单的值
- google-api - 对 google api 进行身份验证或应用程序配置错误的问题
- python - 线程中的回调函数(PySNMP)阻止其他线程启动
- python-3.x - 为什么 virtualenv 显示所有已安装的包,而不在 virtualenv 中安装模块?
- c# - 将子类方法的代码插入基类方法的中间
- jenkins - Artifactory Jenkins 插件发布 Python 项目
- scala - 如何迭代scala类成员以找到空值
- azure - 使用托管标识在 Azure 中应用服务到应用服务身份验证
- django - “找不到或无法读取要导入的文件:~bootstrap/scss/functions。”,“格式化”:“错误:找不到或无法读取要导入的文件:~
- symfony4 - 如何用多语言翻译网站