donglang7236 2017-03-09 22:10
浏览 31

WooCommerce根据小计显示其他购物车字段

I'm trying to show one specific add-on shipping insurance product based on the WooCommerce SubTotal. My code ab-ends on line 10. I think the hide/show process needs a wrapper but I'm unsure how to code it.

I was given a suggestion but I don't know how to implement it...

The suggestion: It looks like you're trying to use a mixed PHP / javascript approach based on hiding or showing fields. Instead, I'd recommend hooking into the woocommerce_checkout_fields at a priority greater than 10 (like 20) -- this would let you get all fields from the $checkout_fields['add_ons'] array, and then you can "unset" some of them based on the cart subtotal (which is significantly easier to grab via PHP instead of javascript).

Anyone who can help... Thank you tons in advance. My original try at it...

<?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' );

?>
  • 写回答

1条回答 默认 最新

  • dongyishe6689 2017-03-10 03:55
    关注

    Modifying my tutorial on checkout fields a bit, this will show "Some field" when the total is between 50-100 and "Another field" when the total is between 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' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了