Woocommerce结帐页面取决于产品类别

时间:2015-03-03 16:28:26

标签: wordpress woocommerce

我正在使用woocommerce构建网站。

对于某些产品,客户需要在结帐页面的Child name字段中输入其孩子的姓名。 (该网站销售音乐课程)

然而,对于像礼品卡这样的其他产品,我不需要这个Child name字段。我无法找到任何可以显示不同结帐页面的插件,具体取决于客户购买产品的类别。

任何想法使这成为可能吗?

Thnx提前!

2 个答案:

答案 0 :(得分:0)

我想我找到了一个答案的网站:

https://wordimpress.com/create-conditional-checkout-fields-woocommerce/

我要尝试这个并将结果放在这里。


***几个小时后好吧! 它完成了这项工作,我发布的网站中的代码用于单个产品ID。如果要检查类别ID,可以更改此代码:

 /**
 * Check if Conditional Product is In cart
 *
 * @param $product_id
 *
 * @return bool
 */
function wordimpress_is_conditional_product_in_cart( $product_id ) {
    //Check to see if user has product in cart
    global $woocommerce;

    //flag no book in cart
    $book_in_cart = false;

    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if ( $_product->id === $product_id ) {
            //book is in cart!
            $book_in_cart = true;

        }
    }

    return $book_in_cart;

}

有:

/**
 * Check if Conditional Product is In cart
 *
 * @param $product_id
 *
 * @return bool
 */
function wordimpress_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;

//flag no book in cart
$book_in_cart = false;

foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_cat' );

            foreach ($terms as $term) {
                $_categoryid = $term->term_id;
            }

    if ( $_categoryid === 14 ) {
        //book is in cart!
        $book_in_cart = true;
    }

}

return $book_in_cart;

}

如果您需要检查多个类别ID或产品ID,可以复制此示例:

/**
 * Check if Conditional Product is In cart
 *
 * @param $product_id
 *
 * @return bool
 */
function wordimpress_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;

//flag no book in cart
$book_in_cart = false;

foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_cat' );

            foreach ($terms as $term) {
                $_categoryid = $term->term_id;
            }

    if (( $_categoryid === 14 ) || ( $_categoryid === 16 )) {
        //book is in cart!
        $book_in_cart = true;
    }

}

return $book_in_cart;

}

我希望这篇文章能够帮助人们节省大量的时间来搜索所有松散的信息;)

答案 1 :(得分:0)

这对我来说更好:

  
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );



 function optional_default_address_fields( $address_fields ) {
 $address_fields['postcode']['required'] = false;
 $address_fields['city']['required'] = false;
 $address_fields['state']['required'] = false;
 $address_fields['address_1']['required'] = false;
 $address_fields['country']['required'] = false;
 $address_fields['billing_company']['required'] = false;

 return $address_fields;
 }
 
function custom_override_checkout_fields( $fields ) {
    $categories = array('ajakirjad');

    $foundAjakiri = false;
    $foundOthers = false;
    
    
    foreach ( WC()->cart->get_cart() as $cart_item ){
        
        if(has_term( $categories, 'product_cat', $cart_item['product_id'] )) {
            $foundAjakiri = true;
        } else {
            $foundOthers = true;
        }
    }
    if($foundAjakiri == true && $foundOthers == false) {
//        echo '1';
    } elseif($foundAjakiri == false && $foundOthers == true) {
        
        $fields['billing']['billing_address_1']['required'] = false;
        $fields['billing']['billing_address_2']['required'] = false;
        $fields['billing']['billing_city']['required'] = false;
        $fields['billing']['billing_postcode']['required'] = false;
        $fields['billing']['billing_state']['required'] = false;
        $fields['billing']['billing_country']['required'] = false;
        $fields['billing']['billing_country']['class'][] = 'no-need';
        $fields['billing']['billing_company']['required'] = false;
        unset($fields['billing']['billing_address_1']);
        unset($fields['billing']['billing_address_2']);
        unset($fields['billing']['billing_city']);
        unset($fields['billing']['billing_postcode']);
        
//        unset($fields['billing']['billing_country']);
        unset($fields['billing']['billing_state']);
//        unset($fields['billing']['billing_phone']);
        //unset($fields['order']['order_comments']);
    }
    
    //add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 9999 );
    add_filter( 'woocommerce_checkout_fields' , 'remove_order_notes' );
    
    
    
    return $fields;
}