代码之家  ›  专栏  ›  技术社区  ›  Innervation

有条件显示Woocommerce签出字段

  •  2
  • Innervation  · 技术社区  · 7 年前

    在Wordpress网站上,我目前正在尝试建立Woocommerce和 Learnpress 插件。我使用 Woocommerce Checkout Add-ons 商业插件,允许创建一些额外的签出字段。

    我希望仅当某些Learnpress课程出现在结帐车中时,才有条件地显示一些结帐字段。

    签出插件中的代码如下:

    <?php if ( $add_on_fields ) : ?>
    
    <div id="wc_checkout_add_ons">
    <?php
        foreach ( $add_on_fields as $key => $field ) :
            woocommerce_form_field( $key, $field, WC()->checkout()->get_value( 
            $key ) );
        endforeach;
    ?>
    </div>
    
    <?php endif; ?>
    

    于是开始走这条路:

    <div id="wc_checkout_add_ons">
    <?php 
        $course_category = get_the_terms( $post->ID, 'course_category' );
        echo $course_category; 
        if ($course_category == 'weekend-intensives') { 
            foreach ( $add_on_fields as $key => $field ) : 
                woocommerce_form_field( $key, $field, WC()->checkout()->get_value( $key ) ); 
            endforeach; 
        } else { 
            echo ('Proceed with checkout'); 
        } 
    ?> 
    </div>
    

    现在我甚至没有收到 $course_category 所以我知道我已经错了。。。

    我需要找出获取 learnpress课程类别 当然在收银台/购物车中。
    我知道还有很多事情要做,我可能还有很长的路要走,但我愿意在一些帮助下完成它。

    任何帮助都将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  0
  •   LoicTheAztec    7 年前

    您可以使用 has_term() 用于自定义分类法和自定义分类法(如“产品类别”)的特定WordPress条件函数 或者你的案子 您可以轻松使用的“课程类别”。

    1) 您需要检查购物车项目,以查看是否有购物车项目保留在“周末强化”课程类别中,这必须在代码开头完成。这样可以改变现有条件 if ( $add_on_fields ) : …

    2) 你需要确保 课程类别 分类法适用于WooCommerce 产品自定义岗位类型 . 如果没有,您将无法检查 课程类别 购物车项目。

    因此,正确的方法是:

    <?php
        ## ---- Custom code start here ---- ##
        $is_in_cart = false;
    
        // Checking cart items for specific "Course category"
        foreach( WC->cart->get_cart() as $cart_item ){
            // Here your Course category
            if( has_term( 'weekend-intensives', 'course_category', $cart_item['product_id'] ){
                $is_in_cart = true; // Found
                break; // Stop the loop
            }
        }
        // adding our condition in existing code if statement
        if ( $add_on_fields && $is_in_cart ) : 
    
        ## ---- Custom code Ends here ---- ##
    
        // if ( $add_on_fields ) :
    ?>
    
    <div id="wc_checkout_add_ons">
    <?php
        foreach ( $add_on_fields as $key => $field ) :
            woocommerce_form_field( $key, $field, WC()->checkout()->get_value( $key ) );
        endforeach;
    ?>
    </div>
    
    <?php endif; ?>
    

    这是未经测试的,但应该有效(如果课程类别适用于WC产品)


    更新

    检查产品ID是否适用于“周末强化”课程类别:

    • 代码进入功能。活动子主题(或主题)的php文件。
    • 在其内部设置正确的产品ID以进行检查和保存。
    • 进入存档页面或产品页面查看结果(保存后)。

    代码:

    // Checking for "weekend-intensives" course category in shop and product pages
    add_action( 'woocommerce_before_main_content', function(){
        // Only for admin user role
        if( ! current_user_can('edit_products')) return;
    
        // ==> BELOW set your product ID to check
        $product_id = 43;
    
        // Output the raw cart object data
        if( has_term( 'weekend-intensives', 'course_category', $product_id ){
            echo '<pre>YES! This product works with "weekend-intensives" course category</pre>';
        } else {
            echo '<pre>This product DOES NOT WORK with "weekend-intensives" course category</pre>';
        }
    }, 987 );