Woocommerce – 如果结账时购物车中有两个 product_ids 之一,请添加表单字段和 show/hide div
Woocommerce – if one of two product_ids is in cart at checkout, add form fields and show/hide divs
差不多就这些了。我的 php-fu 仍然很弱,而且我对 woocommerce 还是很陌生。
我需要创建一个脚本来检查结帐页面上购物车中两个特定产品 ID 之一,如果有,请添加一个 "paypal email address" 文本字段和一个 "receive-newsletter?"复选框,并将我在 "form-shipping.php" 模板中创建的 div 替换为另一个隐藏的 div.
令人惊讶的是,到目前为止,我的研究在结账页面的购物车中成功确认匹配 product_ids 方面并没有太多结果,这似乎是一个相当普遍的需求。在这一点上,我认为仅此一项就是巨大的胜利。
任何有关实现此目标的建议、指导或线索将不胜感激。
这是我一直在使用的代码,试图改编自 https://wordimpress.com/create-conditional-checkout-fields-woocommerce/:
<?php
add_action( 'woocommerce_before_checkout_billing_form', 'aym_custom_checkout_field' );
function aym_custom_checkout_field( $checkout ) {
//Check if Product in Cart
$prod_in_cart_17563 = aym_is_conditional_product_in_cart_17563( 17563 );
$prod_in_cart_17558 = aym_is_conditional_product_in_cart_17558( 17558 );
if ( $prod_in_cart_17563 === true || $prod_in_cart_17558 === true ) {
//Prod is in cart so hide div
echo '<script type="text/javascript">$(".checkout_promo_content").css("display", "none")</script>';
echo '<script type="text/javascript">$(".checkout_promo_aff_content").css("display", "block")</script>';
//and add additional fields
echo '<div id="email_paypal"><h3>' . __( 'Paypal Email Address' ) . '</h3><p style="margin: 0 0 8px;">Please enter the email address you use for Paypal</p>';
woocommerce_form_field( 'pp_email_textbox', array(
'type' => 'text',
'class' => array( 'paypal-email form-row-wide' ),
'label' => __( 'Paypal Email Address' ),
), $checkout->get_value( 'pp_email_textbox' ) );
echo '<h3>' . __( 'Ambassador Terms and Conditions' ) . '</h3><p style="margin: 0 0 8px;">Please accept the <a href="http://www.acceleratingyoungminds.com/ambassador-terms-conditions/">Ambassaor Terms and Conditions</a></p>';
woocommerce_form_field( 'amb_terms_checkbox', array(
'type' => 'checkbox',
'class' => array( 'amb_terms-checkbox form-row-wide' ),
'label' => __( 'I accept Ambassador terms and Conditions' ),
), $checkout->get_value( 'amb_terms_checkbox' ) );
echo '<h3>' . __( 'Subscribe for Ambassador Mailing List' ) . '</h3><p style="margin: 0 0 8px;">Would you like to subscribe for welcome emails and important information</p>';
woocommerce_form_field( 'amb_sub_checkbox', array(
'type' => 'checkbox',
'class' => array( 'amb_sub_checkbox form-row-wide' ),
'label' => __( 'I would like to subscribe to the Ambassador Newsletter' ),
), $checkout->get_value( 'amb_sub_checkbox' ) );
echo '</div>';
}
}
?>
菜鸟的第一次尝试,我什至没有定义这两个产品的功能(aym_is_conditional_product_in_cart_17563,等等)。其次,我为 Wordpress 错误地调用了 JQuery。第三,我在add_action中使用了错误的钩子。第四,我最初过早地匆忙发布了这整件事。我给我的家人带来了耻辱。以下代码按预期工作。希望它能帮助遇到类似问题的人。
//AMBASSADOR CUSTOM CHECKOUT FIELDS, CONTENT
add_action( 'woocommerce_after_checkout_billing_form', 'aym_custom_checkout_field' );
function aym_custom_checkout_field( $checkout ) {
//Check if Product in Cart
$prod_in_cart_17563 = aym_is_conditional_product_in_cart_17563( 17563 );
$prod_in_cart_17558 = aym_is_conditional_product_in_cart_17558( 17558 );
if ( $prod_in_cart_17563 === true || $prod_in_cart_17558 === true ) {
//Prod is in cart so hide div
echo '<script type="text/javascript">
jQuery(function($) {
$( window ).load(function(){
console.log( "hidden!" );
$(".check-promo-content").css("display", "none");
});
});
</script>';
echo '<script type="text/javascript">
jQuery(function($) {
console.log( "shown!" );
$(".checkout_promo_aff_content").css("display", "block");
});</script>';
//and add additional fields
echo '<div id="email_paypal"><h3>' . __( 'Paypal Email Address' ) . '</h3><p style="margin: 0 0 8px;">Please enter the email address you use for Paypal</p>';
woocommerce_form_field( 'pp_email_textbox', array(
'type' => 'text',
'class' => array( 'paypal-email form-row-wide' ),
'label' => __( 'Paypal Email Address' ),
), $checkout->get_value( 'pp_email_textbox' ) );
echo '<h3>' . __( 'Ambassador Terms and Conditions' ) . '</h3><p style="margin: 0 0 8px;">Please accept the <a href="http://www.acceleratingyoungminds.com/ambassador-terms-conditions/">Ambassador Terms and Conditions</a></p>';
woocommerce_form_field( 'amb_terms_checkbox', array(
'type' => 'checkbox',
'class' => array( 'amb_terms-checkbox form-row-wide' ),
'label' => __( 'I accept Ambassador terms and Conditions' ),
), $checkout->get_value( 'amb_terms_checkbox' ) );
echo '<h3>' . __( 'Subscribe for Ambassador Mailing List' ) . '</h3><p style="margin: 0 0 8px;">Would you like to subscribe for welcome emails and important information?</p>';
woocommerce_form_field( 'amb_sub_checkbox', array(
'type' => 'checkbox',
'class' => array( 'amb_sub_checkbox form-row-wide' ),
'label' => __( 'I would like to subscribe to the Ambassador Newsletter' ),
), $checkout->get_value( 'amb_sub_checkbox' ) );
echo '</div>';
}
}
//TEAM LEADER BUNDLE
function aym_is_conditional_product_in_cart_17558( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no product in cart
$prod_in_cart_17558 = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
//product is in cart!
$prod_in_cart_17558 = true;
}
}
return $prod_in_cart_17558;
}
//AMBSSADOR BUNDLE
function aym_is_conditional_product_in_cart_17563( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no product in cart
$prod_in_cart_17563 = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
//product is in cart!
$prod_in_cart_17563 = true;
}
}
return $prod_in_cart_17563;
差不多就这些了。我的 php-fu 仍然很弱,而且我对 woocommerce 还是很陌生。
我需要创建一个脚本来检查结帐页面上购物车中两个特定产品 ID 之一,如果有,请添加一个 "paypal email address" 文本字段和一个 "receive-newsletter?"复选框,并将我在 "form-shipping.php" 模板中创建的 div 替换为另一个隐藏的 div.
令人惊讶的是,到目前为止,我的研究在结账页面的购物车中成功确认匹配 product_ids 方面并没有太多结果,这似乎是一个相当普遍的需求。在这一点上,我认为仅此一项就是巨大的胜利。
任何有关实现此目标的建议、指导或线索将不胜感激。
这是我一直在使用的代码,试图改编自 https://wordimpress.com/create-conditional-checkout-fields-woocommerce/:
<?php
add_action( 'woocommerce_before_checkout_billing_form', 'aym_custom_checkout_field' );
function aym_custom_checkout_field( $checkout ) {
//Check if Product in Cart
$prod_in_cart_17563 = aym_is_conditional_product_in_cart_17563( 17563 );
$prod_in_cart_17558 = aym_is_conditional_product_in_cart_17558( 17558 );
if ( $prod_in_cart_17563 === true || $prod_in_cart_17558 === true ) {
//Prod is in cart so hide div
echo '<script type="text/javascript">$(".checkout_promo_content").css("display", "none")</script>';
echo '<script type="text/javascript">$(".checkout_promo_aff_content").css("display", "block")</script>';
//and add additional fields
echo '<div id="email_paypal"><h3>' . __( 'Paypal Email Address' ) . '</h3><p style="margin: 0 0 8px;">Please enter the email address you use for Paypal</p>';
woocommerce_form_field( 'pp_email_textbox', array(
'type' => 'text',
'class' => array( 'paypal-email form-row-wide' ),
'label' => __( 'Paypal Email Address' ),
), $checkout->get_value( 'pp_email_textbox' ) );
echo '<h3>' . __( 'Ambassador Terms and Conditions' ) . '</h3><p style="margin: 0 0 8px;">Please accept the <a href="http://www.acceleratingyoungminds.com/ambassador-terms-conditions/">Ambassaor Terms and Conditions</a></p>';
woocommerce_form_field( 'amb_terms_checkbox', array(
'type' => 'checkbox',
'class' => array( 'amb_terms-checkbox form-row-wide' ),
'label' => __( 'I accept Ambassador terms and Conditions' ),
), $checkout->get_value( 'amb_terms_checkbox' ) );
echo '<h3>' . __( 'Subscribe for Ambassador Mailing List' ) . '</h3><p style="margin: 0 0 8px;">Would you like to subscribe for welcome emails and important information</p>';
woocommerce_form_field( 'amb_sub_checkbox', array(
'type' => 'checkbox',
'class' => array( 'amb_sub_checkbox form-row-wide' ),
'label' => __( 'I would like to subscribe to the Ambassador Newsletter' ),
), $checkout->get_value( 'amb_sub_checkbox' ) );
echo '</div>';
}
}
?>
菜鸟的第一次尝试,我什至没有定义这两个产品的功能(aym_is_conditional_product_in_cart_17563,等等)。其次,我为 Wordpress 错误地调用了 JQuery。第三,我在add_action中使用了错误的钩子。第四,我最初过早地匆忙发布了这整件事。我给我的家人带来了耻辱。以下代码按预期工作。希望它能帮助遇到类似问题的人。
//AMBASSADOR CUSTOM CHECKOUT FIELDS, CONTENT
add_action( 'woocommerce_after_checkout_billing_form', 'aym_custom_checkout_field' );
function aym_custom_checkout_field( $checkout ) {
//Check if Product in Cart
$prod_in_cart_17563 = aym_is_conditional_product_in_cart_17563( 17563 );
$prod_in_cart_17558 = aym_is_conditional_product_in_cart_17558( 17558 );
if ( $prod_in_cart_17563 === true || $prod_in_cart_17558 === true ) {
//Prod is in cart so hide div
echo '<script type="text/javascript">
jQuery(function($) {
$( window ).load(function(){
console.log( "hidden!" );
$(".check-promo-content").css("display", "none");
});
});
</script>';
echo '<script type="text/javascript">
jQuery(function($) {
console.log( "shown!" );
$(".checkout_promo_aff_content").css("display", "block");
});</script>';
//and add additional fields
echo '<div id="email_paypal"><h3>' . __( 'Paypal Email Address' ) . '</h3><p style="margin: 0 0 8px;">Please enter the email address you use for Paypal</p>';
woocommerce_form_field( 'pp_email_textbox', array(
'type' => 'text',
'class' => array( 'paypal-email form-row-wide' ),
'label' => __( 'Paypal Email Address' ),
), $checkout->get_value( 'pp_email_textbox' ) );
echo '<h3>' . __( 'Ambassador Terms and Conditions' ) . '</h3><p style="margin: 0 0 8px;">Please accept the <a href="http://www.acceleratingyoungminds.com/ambassador-terms-conditions/">Ambassador Terms and Conditions</a></p>';
woocommerce_form_field( 'amb_terms_checkbox', array(
'type' => 'checkbox',
'class' => array( 'amb_terms-checkbox form-row-wide' ),
'label' => __( 'I accept Ambassador terms and Conditions' ),
), $checkout->get_value( 'amb_terms_checkbox' ) );
echo '<h3>' . __( 'Subscribe for Ambassador Mailing List' ) . '</h3><p style="margin: 0 0 8px;">Would you like to subscribe for welcome emails and important information?</p>';
woocommerce_form_field( 'amb_sub_checkbox', array(
'type' => 'checkbox',
'class' => array( 'amb_sub_checkbox form-row-wide' ),
'label' => __( 'I would like to subscribe to the Ambassador Newsletter' ),
), $checkout->get_value( 'amb_sub_checkbox' ) );
echo '</div>';
}
}
//TEAM LEADER BUNDLE
function aym_is_conditional_product_in_cart_17558( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no product in cart
$prod_in_cart_17558 = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
//product is in cart!
$prod_in_cart_17558 = true;
}
}
return $prod_in_cart_17558;
}
//AMBSSADOR BUNDLE
function aym_is_conditional_product_in_cart_17563( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no product in cart
$prod_in_cart_17563 = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
//product is in cart!
$prod_in_cart_17563 = true;
}
}
return $prod_in_cart_17563;