2017-03-09 137 views
0

我试图根据WooCommerce小计显示一个特定的附加运输保险产品。我的代码ab-end在第10行。我认为hide/show过程需要一个包装器,但我不确定如何对其进行编码。WooCommerce根据小计显示额外的购物车字段

他们给我一个建议,但我不知道如何实现它?

的建议: 它看起来像你想使用混合PHP /基础上隐藏或显示JavaScript的方法领域。 相反,我建议以大于10的优先级(如20)连接woocommerce_checkout_fields - 这可让您从$checkout_fields['add_ons']阵列中获取所有字段,然后您可以根据购物车小计“取消”其中的一些字段(通过PHP而不是javascript来获取它们更容易)。

任何人都可以帮忙...提前谢谢你吨。 我原来的尝试吧......

<?php 
function wc_shipping_insurance_chooser() { 
// Set variables 
$fifty = 50; 
$one_hundred = 100; 
$two_hundred = 200; 

if (WC()->cart->total >$fifty && WC()->cart->total <$one_hundred) { 
    If(is_cart()) { 
     // Show Insurance cost for $50 - $100 
     $('#wc_checkout_add_ons_10_field').show(); 
     $('#wc_checkout_add_ons_11_field').hide(); 
     $('#wc_checkout_add_ons_12_field').hide(); 
     $('#wc_checkout_add_ons_13_field').hide(); 
     $('#wc_checkout_add_ons_14_field').hide(); 
     $('#wc_checkout_add_ons_15_field').hide(); 
    } else { 
     $('#wc_checkout_add_ons_10_field').hide(); 
     $('#wc_checkout_add_ons_11_field').hide(); 
     $('#wc_checkout_add_ons_12_field').hide(); 
     $('#wc_checkout_add_ons_13_field').hide(); 
     $('#wc_checkout_add_ons_14_field').hide(); 
     $('#wc_checkout_add_ons_15_field').hide(); 
    } 

} elseif (WC()->cart->total >$one_hundred && WC()->cart->total <$two_hundred) { 
    If(is_cart()) { 
     // Show Insurance cost for $100-$199 
     $('#wc_checkout_add_ons_10_field').hide(); 
     $('#wc_checkout_add_ons_11_field').show(); 
     $('#wc_checkout_add_ons_12_field').hide(); 
     $('#wc_checkout_add_ons_13_field').hide(); 
     $('#wc_checkout_add_ons_14_field').hide(); 
     $('#wc_checkout_add_ons_15_field').hide(); 
    } else { 
     $('#wc_checkout_add_ons_10_field').hide(); 
     $('#wc_checkout_add_ons_11_field').hide(); 
     $('#wc_checkout_add_ons_12_field').hide(); 
     $('#wc_checkout_add_ons_13_field').hide(); 
     $('#wc_checkout_add_ons_14_field').hide(); 
     $('#wc_checkout_add_ons_15_field').hide(); 
    } 


    } else { 
     $('#wc_checkout_add_ons_10_field').hide(); 
     $('#wc_checkout_add_ons_11_field').hide(); 
     $('#wc_checkout_add_ons_12_field').hide(); 
     $('#wc_checkout_add_ons_13_field').hide(); 
     $('#wc_checkout_add_ons_14_field').hide(); 
     $('#wc_checkout_add_ons_15_field').hide(); 
    } 
} 

add_action('woocommerce_checkout_process', 'wc_shipping_insurance_chooser'); 
add_action('woocommerce_before_cart' , 'wc_shipping_insurance_chooser'); 

?> 
+0

这是不可能知道你的附加字段是什么,但它似乎是你想混合PHP和jQuery。 – helgatheviking

回答

1

修改我的tutorial on checkout fields了一下,这将显示“一些领域”的时候,一共是50-100和“另一场”之间时,总为100-200之间。

// Add a new checkout field 
function kia_filter_checkout_fields($fields){ 
    $cart_total = WC()->cart->total; 
    if($cart_total > 50 && $cart_total < 100) { 
     $fields['extra_fields'] = array(
       'some_field' => array(
        'type' => 'checkbox', 
        'label' => __('Some field') 
        ), 
       ); 
    } else if ($cart_total > 100 && $cart_total < 200) { 
     $fields['extra_fields'] = array(
      'another_field' => array(
       'type' => 'checkbox', 
       'label' => __('Another field') 
       ) 
      ); 
    } 

    return $fields; 
} 
add_filter('woocommerce_checkout_fields', 'kia_filter_checkout_fields'); 

// display the extra field on the checkout form 
function kia_extra_checkout_fields(){ 

    $checkout = WC()->checkout; 

    if(! empty($checkout->checkout_fields['extra_fields'])){ 

    ?> 

    <div class="extra-fields"> 
    <h3><?php _e('Additional Fields'); ?></h3> 

    <?php 
    // because of this foreach, everything added to the array in the previous function will display automagically 
    foreach ($checkout->checkout_fields['extra_fields'] as $key => $field) : ?> 

      <?php woocommerce_form_field($key, $field, $checkout->get_value($key)); ?> 

     <?php endforeach; ?> 
    </div> 

<?php } 
} 
add_action('woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields'); 
相关问题