沃梦达 / 编程问答 / php问题 / 正文

根据产品ID更改WooCommerce结账订单备注占位符和标签

Change WooCommerce checkout order notes placeholder and label based on product Ids(根据产品ID更改WooCommerce结账订单备注占位符和标签)

本文介绍了根据产品ID更改WooCommerce结账订单备注占位符和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据产品ID:s(数组)重命名Order Notes标签和占位符。但出于某种原因,这并不管用。无论购物车中有什么产品,文本都保持不变,并且我收到一个参数错误。

以下是我根据现有答案代码定制的代码:

add_filter( 'woocommerce_checkout_fields', 'rename_order_notes_based_on_product', 10, 2 );
function rename_order_notes_based_on_product( $fields, $cart ) {
    $product_ids = array(11, 18);
    $found_ids = array();

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        foreach( $product_ids as $product_id ) {
            if ( in_array( $product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
                $found_ids[$product_id] = $product_id;
                break;
            }
        }
    }
    
    if ( count( $found_ids ) === count( $product_ids ) ) {
        $fields['order']['order_comments']['placeholder'] = 'Placeholder text here';
        $fields['order']['order_comments']['label'] = 'Label text here';
    }
    else {

        $fields['order']['order_comments']['placeholder'] = 'Placeholder text here';
        $fields['order']['order_comments']['label'] = 'Label text here';
    }
    return $fields;
}

推荐答案

您的代码中有一些错误,请尝试执行以下操作:

add_filter( 'woocommerce_checkout_fields', 'rename_order_notes_based_on_product' );
function rename_order_notes_based_on_product( $fields ) {
    $targeted_ids = array(11, 18); // Here set your targeted product Ids
    $found        = false; // Initializing

    foreach ( WC()->cart->get_cart() as $item ) {
        if ( array_intersect( $targeted_ids, array( $item['product_id'], $item['variation_id'] ) ) ) {
            $found = true;
            break;
        }
    }
    
    if ( $found ) {
        $fields['order']['order_comments']['placeholder'] = 'Placeholder text here (found)';
        $fields['order']['order_comments']['label'] = 'Label text here (found)';
    } else {
        $fields['order']['order_comments']['placeholder'] = 'Placeholder text here (Not found)';
        $fields['order']['order_comments']['label'] = 'Label text here (Not found)';
    }
    return $fields;
}

代码放在活动子主题(或活动主题)的函数.php文件中。它应该能更好地工作。


或者,如果您只想在所有目标产品ID都在购物车中时更改订单备注字段占位符和标签,请改用以下选项:

add_filter( 'woocommerce_checkout_fields', 'rename_order_notes_based_on_product' );
function rename_order_notes_based_on_product( $fields ) {
    $targeted_ids = array(11, 18); // Here set your targeted product Ids
    $found_ids    = array(); // Initializing

    foreach ( WC()->cart->get_cart() as $item ) {
        if ( array_intersect( $targeted_ids, array( $item['product_id'], $item['variation_id'] ) ) ) {
            
            $found_ids[$item['data']->get_id()] = $item['data']->get_id();
        }
    }
    
    if ( count($found_ids) === count($targeted_ids) ) {
        $fields['order']['order_comments']['placeholder'] = 'Placeholder text here (match)';
        $fields['order']['order_comments']['label'] = 'Label text here (match)';
    } else {
        $fields['order']['order_comments']['placeholder'] = 'Placeholder text here (Not match)';
        $fields['order']['order_comments']['label'] = 'Label text here (Not match)';
    }
    return $fields;
}

代码放在活动子主题(或活动主题)的函数.php文件中。它也应该可以工作。

这篇关于根据产品ID更改WooCommerce结账订单备注占位符和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:根据产品ID更改WooCommerce结账订单备注占位符和标签

基础教程推荐