【问题标题】:Change WooCommerce checkout order notes placeholder and label based on product Ids根据产品 ID 更改 WooCommerce 结帐订单备注占位符和标签
【发布时间】:2021-07-21 01:42:04
【问题描述】:

我正在尝试根据产品 ID:s(数组)重命名订单备注标签和占位符。但由于某种原因,这不起作用。无论购物车中有什么产品,文本都会保持原样,并且我遇到了参数错误。

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

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;
}

【问题讨论】:

    标签: php wordpress woocommerce field checkout


    【解决方案1】:

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

    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;
    }
    

    代码位于活动子主题(或活动主题)的functions.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;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它也应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-29
      • 2021-04-21
      • 2021-04-08
      • 2018-11-06
      • 2018-06-13
      • 2019-03-02
      • 1970-01-01
      相关资源
      最近更新 更多