0

Hello I was using woocommerce_add_order_item_meta to add some additional information for order item. After woocommerce update I got error on ordar pay page. I researched that why it happens. I found this article. and I understood why I'm getting error. I changed action hook name but still get errors. What should I do ?

function addBizDays($item_id, $cart_item ,$start, $add){

    $start =current_time( 'mysql' );
    $add = get_post_meta( $cart_item[ 'product_id' ], 'dp_kargolanma_suresi', true );
    $d = new DateTime($start );
    $t = $d->getTimestamp();


    // loop for X days
    for($i=0; $i<$add; $i++){

        // add 1 day to timestamp
        $addDay = 86400;

        // get what day it is next day
        $nextDay = date('w', ($t+$addDay));

        // if it's Saturday or Sunday get $i-1
        if($nextDay == 0 || $nextDay == 6) {
            $i--;
        }

        // modify timestamp, add 1 day
        $t = $t+$addDay;
    }

    $d->setTimestamp($t);

    $teslim_tarihi = $d->format( 'd-m-Y' ). "\n"; 
    $desiveyakg = ( $cart_item['quantity'] * $cart_item['data']->get_weight());
    $kargosinifi = $cart_item['data']->get_shipping_class();
     wc_update_order_item_meta( $item_id, 'En Gec Kargolanma Tarihi', $teslim_tarihi );
     wc_update_order_item_meta( $item_id, '_desi_kg', $desiveyakg );
     wc_update_order_item_meta( $item_id, '_alici_onay_durumu', 'Onay Bekliyor' );
     if($cart_item['quantity'] >= 100)
     {
     wc_update_order_item_meta( $item_id, '_kargo_sinifi', 'ucretsiz-kargo' );
     }
     else{
     wc_update_order_item_meta( $item_id, '_kargo_sinifi', $kargosinifi );
     }

}
add_action( 'woocommerce_checkout_create_order_line_item', 'addBizDays', 10, 5 );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
serdar
  • 454
  • 1
  • 8
  • 26

1 Answers1

1

You need to change more than just the hook… The hooked function arguments are wrong in your code and the way to updated data has changed too. Try the following:

add_action( 'woocommerce_checkout_create_order_line_item', 'add_business_days', 10, 4 );
function add_business_days( $order_item, $cart_item_key, $cart_item, $order ){

    $now     = current_time( 'mysql' );
    $add     = $cart_item['data']->get_meta( 'dp_kargolanma_suresi' );
    $d       = new DateTime( $now );
    $t       = $d->getTimestamp();
    $oneDay  = 86400;
    $nextDay = date('w', ($t + $oneDay));


    // loop for X days
    for( $i = 0; $i < $add; $i++ ) {
        // if it's Saturday or Sunday get $i-1
        if($nextDay == 0 || $nextDay == 6) {
            $i--;
        }
        // modify timestamp, add 1 day
        $t = $t + $oneDay;
    }
    $d->setTimestamp($t);

    $teslim_tarihi = $d->format( 'd-m-Y' ). "\n"; 
    $desiveyakg    = ( $cart_item['quantity'] * $cart_item['data']->get_weight() );
    $kargosinifi   = $cart_item['data']->get_shipping_class();

    $order_item->update_meta_data( 'En Gec Kargolanma Tarihi', $teslim_tarihi );
    $order_item->update_meta_data( '_desi_kg', $desiveyakg );
    $order_item->update_meta_data( '_alici_onay_durumu', 'Onay Bekliyor' );

    if($cart_item['quantity'] >= 100) {
        $order_item->update_meta_data( '_kargo_sinifi', 'ucretsiz-kargo' );
    } else {
        $order_item->update_meta_data( '_kargo_sinifi', $kargosinifi );
    }
}

It should work…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399