当前位置: 首页 > 知识库问答 >
问题:

WooCommerce显示基于产品类别的结帐字段

越学博
2023-03-14
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' );
}
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个答案

卢阳泽
2023-03-14
// 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' );
}
 类似资料:
  • 我目前正在使用以下代码获取WooCommerce产品类别: 当前显示所有类别,但我希望仅显示子类别。 例如,如果您在“类别1”页面上,则该页面应仅显示该类别中的所有子级。 我在这里看过很多例子,但都找不到适合我需要的东西。

  • 我在一家Woocommerce商店工作,该商店的产品按父类别(如品牌、颜色、大小等)组织。这些顶级类别中的每一个都有许多子类别:例如,颜色的子类别包括红色、蓝色、粉色等。 客户希望在首页和归档页的网格/循环视图中,在每个产品缩略图下方显示每个产品的品牌和尺寸(产品是鞋)。 每个产品只有一个品牌,但可能有多种尺寸。 因此,本质上,我需要知道是否有一种方法可以只显示单个产品的特定子类别的标题,如下所示

  • 我这里有一个场景。 在我设置的WooCommerce商店中,产品类别是父类别,而是它的子类别。这里的父类别名称布朗克斯是销售布朗克斯婴儿用品的城市。 另一个产品类别是父类别,是它的子类别。在这里,父类别名称Newark是销售“Newark婴儿用品”下产品的城市。 布朗克斯的一名顾客正试图购买纽瓦克婴儿用品类别的产品。 我正在尝试在WooCommerce中设置一个自定义运费,它可以根据客户的位置计算

  • 在WooCommerce中,我试图显示一个基于定义的运输类更新的运输徽章。 我遇到的问题是,当我有一个变量产品,变量有不同的类。我需要根据选择更新徽章。 例如,当一个可变产品有一个单一和纸箱选项,而单一有一个10类,纸箱有一个大块1类时,我需要在纸箱从选项中选择时更新徽章。 我不知道如何获得变体装运类别,而不是单一产品装运类别。 以下是我取得的成绩:

  • 我正在尝试创建一个页面与木商产品类别在标签。 我的选项卡菜单正在工作,但我需要在每个选项卡内容区域中运行一个查询到相应的类别。 但当我单击每个选项卡时,选项卡内容显示了类别中不属于当前选项卡的所有帖子。我没有得到正在出现的问题,请帮助我解决问题 下面是我的代码: 问题是它显示每个类别的所有post。我粘在上面了..请帮帮我

  • 我需要一些关于Woocommerce类别显示选项的帮助。在Wordpress仪表板中,我按照以下链接全局设置类别显示选项<外观- 有三种显示选项可用。 展示产品 目前,选择了第三个选项,它显示子类别以及父类别和子类别下的所有产品。但是,我想排除子类别下的所有产品。换句话说,我只想在父类别下显示子类别和产品。 我在几个教程网站上找到了以下片段。代码完全按照我想要的方式工作,但它也禁用了管理产品搜索和