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

2022-07-02 00:00:00 field php wordpress woocommerce checkout
我正在尝试根据产品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文件中。它也应该可以工作。

相关文章