简体   繁体   English

根据 Woocommerce 中的产品类别添加自定义结帐字段

[英]Add custom checkout field based on product category in Woocommerce

I need to add custom checkout field based on product category.我需要根据产品类别添加自定义结帐字段。 I found this code which works with products, but it is very time consuming to add Ids from every new product from particular category.我发现这段代码适用于产品,但是从特定类别的每个新产品中添加 Id 非常耗时。

function is_in_cart() {
    // Add your special product IDs here
    $ids = array( 12468, 9687, 9693);

    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if( in_array( $cart_item['data']->get_id(), $ids ) )
            return true;
    }
    return false;
}

 /**
 * Add the field to the checkout
 */
add_action( 'woocommerce_before_order_notes', 'custom_checkout_field', 20 );

function custom_checkout_field( $checkout ) {
if( ! is_in_cart() ){
    woocommerce_form_field( 'special_field', array(
        'type'          => 'textarea',
        'class'         => array('form-row-wide'),
        'label'         => pll__('Special text'),
        'required'      => false,
        'label_class'   => array('specialfieldclass'),
        'clear'         => false,
        ), $checkout->get_value( 'special_field' ));
}
}

How to modify this code to work with product categories?如何修改此代码以使用产品类别?

Try the following to handle product categories (handling parent terms too) :尝试以下方法来处理产品类别(也处理父术语)

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

// Custom conditional function that check for product categories
function is_in_cart() {
    // HERE your product categories
    $categories = array( 'clothing' );

    foreach( WC()->cart->get_cart() as $cart_item ){
        if( has_product_categories( $cart_item['product_id'], $categories ) )
            return true;
    }
    return false;
}

 // Add a checkout field before order notes
add_action( 'woocommerce_before_order_notes', 'custom_checkout_field', 20 );
function custom_checkout_field( $checkout ) {
    if( ! is_in_cart() ){
        woocommerce_form_field( 'special_field', array(
            'type'          => 'textarea',
            'class'         => array('form-row-wide'),
            'label'         => pll__('Special text'),
            'required'      => false,
            'label_class'   => array('specialfieldclass'),
            'clear'         => false,
            ), $checkout->get_value( 'special_field' ));
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 It should works.它应该有效。

I need to add custom checkout field based on product category.我需要根据产品类别添加自定义结帐字段。 I found this code which works with products, but it is very time consuming to add Ids from every new product from particular category.我找到了适用于产品的代码,但是从特定类别的每个新产品中添加ID都是非常耗时的。

function is_in_cart() {
    // Add your special product IDs here
    $ids = array( 12468, 9687, 9693);

    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if( in_array( $cart_item['data']->get_id(), $ids ) )
            return true;
    }
    return false;
}

 /**
 * Add the field to the checkout
 */
add_action( 'woocommerce_before_order_notes', 'custom_checkout_field', 20 );

function custom_checkout_field( $checkout ) {
if( ! is_in_cart() ){
    woocommerce_form_field( 'special_field', array(
        'type'          => 'textarea',
        'class'         => array('form-row-wide'),
        'label'         => pll__('Special text'),
        'required'      => false,
        'label_class'   => array('specialfieldclass'),
        'clear'         => false,
        ), $checkout->get_value( 'special_field' ));
}
}

How to modify this code to work with product categories?如何修改此代码以使用产品类别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将自定义字段添加到特定类别的 woocommerce 产品 - Add Custom field to woocommerce product for an specific category 根据 WooCommerce 结帐页面上的产品类别申请自定义数量 arguments - Apply custom quantity arguments based on the product category on the WooCommerce checkout page Woocommerce中基于产品类别的条件自定义结帐字段 - Conditional custom checkout fields based on product category in Woocommerce 根据WooCommerce中的产品自定义字段和产品类别隐藏添加到购物车按钮 - Hide Add To Cart Button based on product custom field and product category in WooCommerce 基于特定产品类别的 WooCommerce 结帐消息 - WooCommerce checkout message based on specific product category WooCommerce 根据产品类别显示结帐字段 - WooCommerce Show checkout fields based on product category Woocommerce 根据产品自定义字段在结账后重定向 - Woocommerce redirect after checkout based on product custom field Woocommerce-如果产品ID ==#,则添加过滤器以显示(或隐藏)自定义结帐字段 - Woocommerce - Add filter to display (or hide) custom checkout field if product ID == # 为特定产品类别的 Woocommerce 产品添加自定义字段 - Add a custom field to Woocommerce products for a specific product category 在Woocommerce中根据购物车数量添加自定义结帐字段 - Add a custom checkout field based on cart items quantity in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM