2017-04-26 28 views
-1

這就是它。我的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>'; 
    } 

} 


?> 
+2

請, 「__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)。 –

+1

哪個版本的woocommerce? – LoicTheAztec

+0

版本2.6.14,但我們當然必須儘快升級。使用尚未升級到3.0的高級主題。 –

回答

0

首先新秀的舉動,我失敗了,甚至定義了兩種產品(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; 
相關問題