WooCommerce根据小计

时间:2017-03-09 22:10:21

标签: php woocommerce

我正在尝试根据WooCommerce SubTotal显示一个特定的附加运输保险产品。我的代码在第10行上结束。我认为隐藏/显示进程需要一个包装器,但我不确定如何编写它。

我得到了一个建议,但我不知道如何实施它......

建议: 看起来你正在尝试使用基于隐藏或显示字段的混合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' );

?>

1 个答案:

答案 0 :(得分:1)

稍微修改我的tutorial on checkout fields,这将显示&#34;某些字段&#34;当总数介于50-100和&#34;另一个字段&#34;当总数在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' );
相关问题