WooCommerce结帐消息基于特定的运输类

时间:2016-11-30 18:13:50

标签: php wordpress woocommerce

我发现了一个类似的问题,并希望将这些代码与某些运输类一起实施。这是代码:

add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
    // set your special category name, slug or ID here:
    $special_cat = 'special_category';
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( has_term( $special_cat, 'product_cat', $item->id ) )
            $bool = true;
    }
    // If the special cat is detected in one items of the cart
    // It displays the message
    if ($bool)
        echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';

1 个答案:

答案 0 :(得分:0)

 add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
    // set your special category name, slug or ID here:
    $shippingclass =  array( 'cut' );
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
	$shipping_class = get_the_terms( $values['variation_id'], 'product_shipping_class' );

        if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass ) ) {
            $bool = true;
    }
    // If the special cat is detected in one items of the cart
    // It displays the message
    if ($bool)
        echo '<div class="example1"><h3>Items in your cart can be cut to save on shipping. List which items you want cut in your order notes.</h3></div>';
}
}