Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用woocommerce\u order\u get\u items过滤器钩子操作项目名称_Php_Arrays_Wordpress_Woocommerce_Orders - Fatal编程技术网

Php 使用woocommerce\u order\u get\u items过滤器钩子操作项目名称

Php 使用woocommerce\u order\u get\u items过滤器钩子操作项目名称,php,arrays,wordpress,woocommerce,orders,Php,Arrays,Wordpress,Woocommerce,Orders,在WooCommerce中,我试图找出如果订单行项目名称包含这些括号,如何删除() 下面是一些代码: $order = wc_get_order($order_id) foreach ( $order->get_items() as $order_item ){ //...enter code here } 但是我想使用过滤器挂钩,因为我无法访问上面的foreach循环。我试图添加到functions.php,因此当get_items()调用时,过滤器将准备数据数组 这是代码: a

在WooCommerce中,我试图找出如果订单行项目名称包含这些括号,如何删除
()

下面是一些代码:

$order = wc_get_order($order_id)
foreach ( $order->get_items() as $order_item ){
   //...enter code here
}
但是我想使用过滤器挂钩,因为我无法访问上面的
foreach
循环。我试图添加到
functions.php
,因此当get_items()调用时,过滤器将准备数据数组

这是代码:

add_filter( 'woocommerce_order_get_items', 'filter_woocommerce_order_get_items', 10, 2 ); 
function filter_woocommerce_order_get_items($items, $instance){
    foreach ($items as $item){
        $search = array('å','ä','ö','(', ')');
        $replace = array('a','a','o', '', '');
        $item['name'] = str_replace($search, $replace, $item['name']);
    }

    return $items;
}
因此,TL;博士:
当调用
$order->get_items()
时,我可以准备数据吗


谢谢

您使用了正确的过滤器挂钩,但实际上,您的自定义挂钩功能不起作用,因为缺少了一些东西

要使其正常工作,您需要在返回前将
$items
数组中的旧值替换为新值。因此,代码中缺少的元素是
$item\u id

我对您的代码做了一些小改动:

add_filter( 'woocommerce_order_get_items', 'filter_woocommerce_order_get_items', 10, 2 );
function filter_woocommerce_order_get_items($items, $instance){
    foreach ($items as $item_id => $item_values){

        $search = array('å','ä','ö','(', ')');
        $replace = array('a','a','o', '', '');

        $items[$item_id]['name'] = str_replace($search, $replace, $item_values['name']);
    }
    return $items;
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中


代码已经过测试并运行正常。

您好,是的,我错过了这一部分。我正在检查/wp content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php第1229行,但没有注意到$items[$item->order\u item\u id]['name']。谢谢