Woocommerce - 如果两个product_ids中的一个在结账时在购物车中,添加表单字段并显示/隐藏div
问题描述:
这就是它。我的php-fu仍然很弱,而且我对woocommerce还很新。Woocommerce - 如果两个product_ids中的一个在结账时在购物车中,添加表单字段并显示/隐藏div
我需要创建一个脚本,用于在结帐页面上检查购物车中的两个特定产品id,如果有,请添加一个“paypal电子邮件地址”文本字段和一个“接收时事通讯?复选框,以及用另一个隐藏的div替换我在“form-shipping.php”模板中创建的div。
令人惊讶的是,到目前为止,我的研究甚至没有在结账页面成功确认购物车中的product_id,这似乎是一个相当普遍的需求。在这一点上,我认为这是一个巨大的胜利。
任何建议,指导或完成此目标的线索将不胜感激。
这里是我一直使用的代码,试图从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等)的功能。其次,我错误地称JQuery为WordPress。第三,我在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;
请, “__write /调试我-code__”, “__recommend /搜索东西换me__”, “__tutorial__” 的要求和 “__low-effort__”, “__unclear__”, “__opinion-based__” ,“**非编程相关**”的问题是[Stack Overflow的主题](http://stackoverflow.com/help/on-topic)。相反,如[如何提问](http://stackoverflow.com/help/how-to-ask)中所述,好的问题有**研究努力**,**对问题的清晰解释**,应该包括[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –
哪个版本的woocommerce? – LoicTheAztec
版本2.6.14,但我们当然必须尽快升级。使用尚未升级到3.0的高级主题。 –