代码之家  ›  专栏  ›  技术社区  ›  Group Of Oceninfo

显示基于产品类别的签出字段

  •  1
  • Group Of Oceninfo  · 技术社区  · 7 年前

    我已经添加了下面的代码,以显示货币切换器下拉菜单上的WooCommerce结帐页,这是工作正常,但我不想显示货币切换器字段,如果有人添加了产品从“游戏”类别,并使用默认的商店货币

    add_action('woocommerce_before_checkout_billing_form', 'wps_add_select_checkout_field');
    function wps_add_select_checkout_field( $checkout ) {
        echo '<label for="payment_option" class="payment_option">'.__('Preferred currency').'</label>';
        echo '<div class="own">', do_shortcode('[woocs]'), '</div>';
        return $checkout;
    }
    
    //* Process the checkout
    add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
    function wps_select_checkout_field_process() {
        global $woocommerce;
        // Check if set, if its not set add an error.
        if ($_POST['payopt'] == "blank")
         wc_add_notice( '<strong>Please select a currency</strong>', 'error' );
    }
    

    基于此回答线索: Checking cart items for a product category in Woocommerce

    add_action('woocommerce_before_cart', 'check_product_category_in_cart');
    function check_product_category_in_cart() {
        // Here set your product categories in the array (can be IDs, slugs or names)
        $categories = array('games');
        $found      = false; // Initializing
    
        // Loop through cart items      
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            // If product categories is found
            if ( !has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
                $found = true; // Set to true
                break; // Stop the loop
            }
        }
    
        // If any defined product category is found, run below code
        if ( $found ) {
            add_action('woocommerce_before_checkout_billing_form', 'wps_add_select_checkout_field');
            function wps_add_select_checkout_field( $checkout ) {
                echo '<label for="payment_option" class="payment_option">'.__('Preferred currency').'</label>';
                echo '<div class="own">', do_shortcode('[woocs]'), '</div>';
                return $checkout;
            }
            //* Process the checkout
            add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
            function wps_select_checkout_field_process() {
                global $woocommerce;
                // Check if set, if its not set add an error.
                if ($_POST['payopt'] == "blank")
                 wc_add_notice( '<strong>Please select a currency</strong>', 'error' );
            }
        }
    }
    

    对于同样的问题,你还有其他建议吗?在那里我可以添加基于购物车产品类别的结帐页货币切换器。 代码1 在结帐页上运行良好,但如果产品类别是游戏,我不想运行该代码。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Group Of Oceninfo    7 年前

    您使用的钩子不正确 woocommerce_before_cart 动作钩子只在购物车页面中触发,但在签出时不触发,它不能以这种方式工作。相反,请尝试使用以下方法:

    // Utility function that checks if at least a cart items remains to a product category
    function has_product_category_in_cart( $product_category ) {
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            // If any product category is found in cart items
            if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) ) {
                return true;
            }
        }
        return false;
    }
    
    // Add a custom select field in checkout
    add_action('woocommerce_before_checkout_billing_form', 'add_custom_checkout_select_field');
    function add_custom_checkout_select_field( $checkout ) {
        // Here set in the function your product category term ID, slugs, names or array
        if ( ! has_product_category_in_cart( 'games' ) && shortcode_exists( 'woocs' ) ) {
            echo '<label for="payment_option" class="payment_option">'.__('Preferred currency').'</label>';
            echo '<div class="own">' . do_shortcode('[woocs]') . '</div>';
        }
    }
    
    // Custom Checkout fields validation
    add_action('woocommerce_checkout_process', 'custom_checkout_select_field_validation');
    function custom_checkout_select_field_validation() {
        if ( isset($_POST['payopt']) && empty($_POST['payopt']) )
            wc_add_notice( '<strong>Please select a currency</strong>', 'error' );
    }
    

    代码放在活动子主题(active theme)的function.php文件中。未经测试,但它应该工作。