【问题标题】:Woocommerce checkout page depending on product categoryWoocommerce 结帐页面取决于产品类别
【发布时间】:2015-03-03 16:28:25
【问题描述】:

我正在使用 woocommerce 构建网站。

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

但是对于礼品卡等其他产品,我不需要此 Child name 字段。我找不到任何可以根据客户购买的产品类别显示不同结帐页面的插件。

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

提前谢谢!

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

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

    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;
    
    }
    

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

    【讨论】:

      【解决方案2】:

      这对我来说效果更好:

        
      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;
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-24
        • 2015-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-11
        • 1970-01-01
        相关资源
        最近更新 更多