Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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 Woo商业订单再次赢得';不复制自定义字段_Php_Wordpress_Woocommerce - Fatal编程技术网

Php Woo商业订单再次赢得';不复制自定义字段

Php Woo商业订单再次赢得';不复制自定义字段,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我已经成功地编写了插件,为woocommerce产品添加了额外的自定义字段。从购物车到完成订单的整个过程都是这样。转到我的帐户并查看过去的订单,自定义字段将正确显示 但是,当我选择在过去的订单上单击“再次订购”时,新购物车不包含自定义字段及其值 以下是我目前必须尝试的: // order again add_filter( 'woocommerce_order_again_cart_item_data', 'woocommerce_order_again_cart_item_data', 10,

我已经成功地编写了插件,为woocommerce产品添加了额外的自定义字段。从购物车到完成订单的整个过程都是这样。转到我的帐户并查看过去的订单,自定义字段将正确显示

但是,当我选择在过去的订单上单击“再次订购”时,新购物车不包含自定义字段及其值

以下是我目前必须尝试的:

// order again
add_filter( 'woocommerce_order_again_cart_item_data', 'woocommerce_order_again_cart_item_data', 10, 3 );

function woocommerce_order_again_cart_item_data($cart_item_meta, $product, $order){
    global $woocommerce;
    // Disable validation
    remove_filter( 'woocommerce_add_to_cart_validation', array( $this, 'validate_add_cart_item' ), 10, 3 );

    if ( ! array_key_exists( 'item_meta', $cart_item_meta ) || ! is_array( $cart_item_meta['item_meta'] ) )
        $cart_item_meta['item_meta'] = array();
    foreach ( array( 'jhpc_toppings', 'jhpc_sauce', 'jhpc_toppings_half', 'jhpc_sauce_half', 'jhpc_garnish' ) as $key )
         $cart_item_meta['item_meta'][$key] = $product['item_meta'][$key];
    return $cart_item_meta;
}
替换

$cart_item_meta['item_meta'][$key] = $product['item_meta'][$key];


否则,为什么要删除验证?

以下是再次为订单添加所有自定义字段数据的代码。在主题的function.php文件中使用给定的代码,并用键替换$customfields数组的自定义字段键

<?php
    add_filter( 'woocommerce_order_again_cart_item_data', 'wpso2523951_order_again_cart_item_data', 10, 3 );

function wpso2523951_order_again_cart_item_data($cart_item_meta, $product, $order){
    //Create an array of all the missing custom field keys that needs to be added in cart item.
    $customfields = [
                    'customfield_key1', 
                    'customfield_key2',
                    'customfield_key3',
                    'customfield_key4',
                    ];
    global $woocommerce;
    remove_all_filters( 'woocommerce_add_to_cart_validation' );
    if ( ! array_key_exists( 'item_meta', $cart_item_meta ) || ! is_array( $cart_item_meta['item_meta'] ) )
    foreach ( $customfields as $key ){
        if(!empty($product[$key])){
            $cart_item_meta[$key] = $product[$key];
        }
    }
    return $cart_item_meta;
}
?>

将数组$customfields的值替换为缺少或未自动添加的自定义字段的键

<?php
    add_filter( 'woocommerce_order_again_cart_item_data', 'wpso2523951_order_again_cart_item_data', 10, 3 );

function wpso2523951_order_again_cart_item_data($cart_item_meta, $product, $order){
    //Create an array of all the missing custom field keys that needs to be added in cart item.
    $customfields = [
                    'customfield_key1', 
                    'customfield_key2',
                    'customfield_key3',
                    'customfield_key4',
                    ];
    global $woocommerce;
    remove_all_filters( 'woocommerce_add_to_cart_validation' );
    if ( ! array_key_exists( 'item_meta', $cart_item_meta ) || ! is_array( $cart_item_meta['item_meta'] ) )
    foreach ( $customfields as $key ){
        if(!empty($product[$key])){
            $cart_item_meta[$key] = $product[$key];
        }
    }
    return $cart_item_meta;
}
?>