简体   繁体   English

如果Woocommerce上的购物车中有特定产品,则显示自定义结帐字段

[英]Show custom checkout fields if a specific product is in cart on Woocommerce

So I have some custom fields in my billing section on the checkout page, that I only want to display if a product with ID (603) is in the cart. 因此,我在结帐页面的“结算”部分中有一些自定义字段,如果购物车中有ID(603)的产品,我只想显示这些字段。

Currently, my code below works to hide the fields if there is a product in the cart that is not id 603, but one issue is when I have 603 and another product in the cart it unsets the fields, 当前,如果购物车中有一个不是id 603的产品,那么下面的代码可以隐藏字段,但是一个问题是,当我的购物车中有603和另一个产品时,它会取消设置字段,

what is the best way to either hide the fields if 603 is not in the cart or to just show them if it is? 隐藏603(如果不在购物车中)或仅显示(如果显示)这些字段的最佳方法是什么?

this is the current code I was using 这是我正在使用的当前代码

    function conditional_checkout_fields_products( $fields ) {
        $cart = WC()->cart->get_cart();

        foreach ( $cart as $item_key => $values ) {
            $product = $values['data'];

            if ( $product->id != 603 ) {
                unset( $fields['billing']['billing_prev_injuries'] );
                unset( $fields['billing']['billing_dogs_events'] );
                unset( $fields['billing']['billing_dogs_age'] );
                unset( $fields['billing']['billing_dogs_breed'] );
                unset( $fields['billing']['billing_dogs_name'] );
              }
        }

        return $fields;
    }
add_filter( 'woocommerce_checkout_fields', 'conditional_checkout_fields_products' );

The following will do the job: 以下将完成工作:

add_filter( 'woocommerce_checkout_fields', 'conditional_checkout_fields_products' );
function conditional_checkout_fields_products( $fields ) {
    $is_in_cart = false;

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( $cart_item['data']->get_id() == 603 ) {
            $is_in_cart = true;
            break;
        }
    }

    if ( ! $is_in_cart ) {
        unset( $fields['billing']['billing_prev_injuries'] );
        unset( $fields['billing']['billing_dogs_events'] );
        unset( $fields['billing']['billing_dogs_age'] );
        unset( $fields['billing']['billing_dogs_breed'] );
        unset( $fields['billing']['billing_dogs_name'] );
    }

    return $fields;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

暂无
暂无

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

相关问题 在 Woocommerce 的购物车中显示多个产品自定义字段值 - Show multiple product custom fields values in cart on Woocommerce 在购物车和订单项目名称下显示 woocommerce 产品自定义字段 - Show woocommerce product custom fields under the cart and order item name woocommerce如果特定产品在购物车中,请禁用特定的结帐字段,否则将需要 - woocommerce if specific product is in cart disable specific checkout fields that otherwise would be required WooCommerce 根据产品类别显示结帐字段 - WooCommerce Show checkout fields based on product category WooCommerce条件自定义字段,如果购物车中的产品类别 - WooCommerce Conditional Custom Fields if Product Category in Cart 在 Woocommerce 购物车结账和订单中禁用特定产品的项目名称链接 - Disable item name link for specific product in Woocommerce cart checkout and orders 在Woocommerce购物车和结帐中显示特定的父产品属性 - Display specific parent product attribute in Woocommerce cart and checkout 根据产品类别拒绝 Woocommerce 中特定购物车项目的结账 - Deny checkout for specific cart items in Woocommerce based on product categories 在购物车和结帐的 WooCommerce 产品名称中附加自定义字段值 - Append custom field value in WooCommerce product name on cart and checkout 在WooCommerce购物车和结帐表中显示产品自定义字段值 - Display a product custom field value in WooCommerce cart and checkout table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM