تابع wc_update_200_line_items بروزرسانی آیتم های ووکامرس به نسخه 2

تابع wc_update_200_line_items بروزرسانی آیتم های ووکامرس به نسخه 2

تابع ووکامرسی wc_update_200_line_items – بروزرسانی آیتم های ووکامرس به نسخه 2

Syntax – سینتکس

wc_update_200_line_items(); 

Usage – نحوه استفاده

if ( !function_exists( 'wc_update_200_line_items' ) ) { 
    require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-update-functions.php'; 
} 
 
  
// NOTICE! Understand what this does before running. 
$result = wc_update_200_line_items(); 
    

Defined – محل تعریف

/includes/wc-update-functions.php

function wc_update_200_line_items() { 
    global $wpdb; 
 
    // Now its time for the massive update to line items - move them to the new DB tables 
    // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_items' WHERE meta_key = '_order_items_old' 
    $order_item_rows = $wpdb->get_results( " 
        SELECT meta_value, post_id FROM {$wpdb->postmeta} 
        WHERE meta_key = '_order_items' 
    " ); 
 
    foreach ( $order_item_rows as $order_item_row ) { 
 
        $order_items = (array) maybe_unserialize( $order_item_row->meta_value ); 
 
        foreach ( $order_items as $order_item ) { 
 
            if ( ! isset( $order_item['line_total'] ) && isset( $order_item['taxrate'] ) && isset( $order_item['cost'] ) ) { 
                $order_item['line_tax'] = number_format( ( $order_item['cost'] * $order_item['qty'] ) * ( $order_item['taxrate'] / 100 ), 2, '.', '' ); 
                $order_item['line_total'] = $order_item['cost'] * $order_item['qty']; 
                $order_item['line_subtotal_tax'] = $order_item['line_tax']; 
                $order_item['line_subtotal'] = $order_item['line_total']; 
            } 
 
            $order_item['line_tax'] = isset( $order_item['line_tax'] ) ? $order_item['line_tax'] : 0; 
            $order_item['line_total'] = isset( $order_item['line_total'] ) ? $order_item['line_total'] : 0; 
            $order_item['line_subtotal_tax'] = isset( $order_item['line_subtotal_tax'] ) ? $order_item['line_subtotal_tax'] : 0; 
            $order_item['line_subtotal'] = isset( $order_item['line_subtotal'] ) ? $order_item['line_subtotal'] : 0; 
 
            $item_id = wc_add_order_item( $order_item_row->post_id, array( 
                 'order_item_name' => $order_item['name'],  
                 'order_item_type' => 'line_item',  
 ) ); 
 
             // Add line item meta 
             if ( $item_id ) { 
                 wc_add_order_item_meta( $item_id, '_qty', absint( $order_item['qty'] ) ); 
                 wc_add_order_item_meta( $item_id, '_tax_class', $order_item['tax_class'] ); 
                 wc_add_order_item_meta( $item_id, '_product_id', $order_item['id'] ); 
                 wc_add_order_item_meta( $item_id, '_variation_id', $order_item['variation_id'] ); 
                 wc_add_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $order_item['line_subtotal'] ) ); 
                 wc_add_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $order_item['line_subtotal_tax'] ) ); 
                 wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $order_item['line_total'] ) ); 
                 wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $order_item['line_tax'] ) ); 
 
                 $meta_rows = array(); 
 
                // Insert meta 
                if ( ! empty( $order_item['item_meta'] ) ) { 
                    foreach ( $order_item['item_meta'] as $key => $meta ) { 
                        // Backwards compatibility 
                        if ( is_array( $meta ) && isset( $meta['meta_name'] ) ) { 
                            $meta_rows[] = '(' . $item_id . ', "' . esc_sql( $meta['meta_name'] ) . '", "' . esc_sql( $meta['meta_value'] ) . '")'; 
                        } else { 
                            $meta_rows[] = '(' . $item_id . ', "' . esc_sql( $key ) . '", "' . esc_sql( $meta ) . '")'; 
                        } 
                    } 
                } 
 
                // Insert meta rows at once 
                if ( sizeof( $meta_rows ) > 0 ) { 
                    $wpdb->query( $wpdb->prepare( " 
                        INSERT INTO {$wpdb->prefix}woocommerce_order_itemmeta ( order_item_id, meta_key, meta_value ) 
                        VALUES " . implode( ', ', $meta_rows ) . "; 
                    ", $order_item_row->post_id ) ); 
                } 
 
                // Delete from DB (rename) 
                $wpdb->query( $wpdb->prepare( " 
                    UPDATE {$wpdb->postmeta} 
                    SET meta_key = '_order_items_old' 
                    WHERE meta_key = '_order_items' 
                    AND post_id = %d 
                ", $order_item_row->post_id ) ); 
             } 
 
            unset( $meta_rows, $item_id, $order_item ); 
        } 
    } 
 
    // Do the same kind of update for order_taxes - move to lines 
    // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_taxes' WHERE meta_key = '_order_taxes_old' 
    $order_tax_rows = $wpdb->get_results( " 
        SELECT meta_value, post_id FROM {$wpdb->postmeta} 
        WHERE meta_key = '_order_taxes' 
    " ); 
 
    foreach ( $order_tax_rows as $order_tax_row ) { 
 
        $order_taxes = (array) maybe_unserialize( $order_tax_row->meta_value ); 
 
        if ( ! empty( $order_taxes ) ) { 
            foreach ( $order_taxes as $order_tax ) { 
 
                if ( ! isset( $order_tax['label'] ) || ! isset( $order_tax['cart_tax'] ) || ! isset( $order_tax['shipping_tax'] ) ) 
                    continue; 
 
                $item_id = wc_add_order_item( $order_tax_row->post_id, array( 
                     'order_item_name' => $order_tax['label'],  
                     'order_item_type' => 'tax',  
 ) ); 
 
                 // Add line item meta 
                 if ( $item_id ) { 
                     wc_add_order_item_meta( $item_id, 'compound', absint( isset( $order_tax['compound'] ) ? $order_tax['compound'] : 0 ) ); 
                     wc_add_order_item_meta( $item_id, 'tax_amount', wc_clean( $order_tax['cart_tax'] ) ); 
                     wc_add_order_item_meta( $item_id, 'shipping_tax_amount', wc_clean( $order_tax['shipping_tax'] ) ); 
                } 
 
                // Delete from DB (rename) 
                $wpdb->query( $wpdb->prepare( " 
                    UPDATE {$wpdb->postmeta} 
                    SET meta_key = '_order_taxes_old' 
                    WHERE meta_key = '_order_taxes' 
                    AND post_id = %d 
                ", $order_tax_row->post_id ) ); 
 
                unset( $tax_amount ); 
            } 
        } 
    } 
} 

versions – نسخه ها

از نسخه : 3.0.2

نسخه فعلی : 3.0.6

دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2

ارسال نظر

جهت استفاده از کد حتما از تگ pre استفاده نمایید .

contact us