skip to Main Content

Hello i did some changes in a Plugin but after Update i have to change every time the code. Now how to build an Filter for functions.php. Here are the changes i did.

they are just some variable changes. and i need these to sync it correctly.

i hope someone can help me build this:)

Here the Code of the ori Plugin
Ori:

    protected function get_order_items($order) {
    $order_items = array();
    $incl_tax = get_option('woocommerce_tax_display_cart') == 'incl';
    foreach ($order->get_items() as $item) {
        if ($incl_tax) {
            $price = ($item['line_subtotal'] + $item['line_subtotal_tax']) / $item['qty'];
        } else {
            $price = $item['line_subtotal'] / $item['qty']; // WC3+ ($item->get_subtotal()/$item->get_quantity()),
        }

        $product = $this->get_product_from_item($item);
        $data = array(
            'sku' => $product ? $product->get_sku() : '',
            'name' => $item['name'], // WC3+ $item->get_name(),
            'quantity' => $item['qty'], // WC3+ $item->get_quantity(),
            'price' => $this->round_price($price),
            'tax_id' =>  Options::get('bf_tax_' . ($item['tax_class'] ?: 'standard')), // WC3+ $item->get_tax_class();
            'account_id' => Options::get('bf_account_products'),
        );

        $data = apply_filters('bf_wc_lq_connector_order_item_data', $data, $item, $product);

        if (isset($item['variation_id'])) {
            $data['text'] = $this->get_item_attribute_meta($item);
        }

        if (Options::get('bf_display_product_excerpt')) {
            $excerpt = get_the_excerpt($item['product_id']);
            if ($excerpt) {
                $data['text'] = isset($data['text']) ? $data['text'] . $excerpt : $excerpt;
            }
        }
        array_push($order_items, $data);
    }
    return $order_items;
}

Here are the changes of the variable.
Mod:

protected function get_order_items($order) {
    $order_items = array();
    $incl_tax = get_option('woocommerce_tax_display_cart') == 'incl';
    foreach ($order->get_items() as $item) {
        if ($incl_tax) {
            $price = ($item['line_subtotal'] + $item['line_subtotal_tax']) / $item['qty'];
        } else {
            $price = $item['line_subtotal'] / $item['qty']; // WC3+ ($item->get_subtotal()/$item->get_quantity()),
        }

        $product = $this->get_product_from_item($item);
        
        $posname = $item->get_meta("posname");
        $pa_groesse = strtoupper($item->get_meta("pa_groesse"));
        $pa_farbe = ucfirst($item->get_meta("pa_farbe")); 
        $nameneu = $posname . ' - ' . $pa_farbe .', ' . $pa_groesse;
        $oriname = $item['name'];
        if ( empty($posname) ) $nameneu = $oriname;
        
        $artnr = strtoupper($item->get_meta("artnr"));
        if ( empty($artnr) ) $artnr = $product ? $product->get_sku() : '';
        
        $data = array(
            'sku' => $artnr,
            'name' => $nameneu, // WC3+ $item->get_name(),
            'quantity' => $item['qty'], // WC3+ $item->get_quantity(),
            'price' => $this->round_price($price),
            'tax_id' =>  Options::get('bf_tax_' . ($item['tax_class'] ?: 'standard')), // WC3+ $item->get_tax_class();
            'account_id' => Options::get('bf_account_products'),
        );

        $data = apply_filters('bf_wc_lq_connector_order_item_data', $data, $item, $product);

        if (array_key_exists('variation_id', $item)) {
            $data['text'] = $this->get_item_attribute_meta($item);
        }

        if (Options::get('bf_display_product_excerpt')) {
            $excerpt = get_the_excerpt($item['product_id']);
            if ($excerpt) {
                $data['text'] = isset($data['text']) ? $data['text'] . $excerpt : $excerpt;
            }
        }
        array_push($order_items, $data);
    }
    return $order_items;
}

2

Answers


  1. Chosen as BEST ANSWER

    Now with help of passerby i did the first step.

    the other step is

    i have in same file this code

           if (isset($item['variation_id'])) {
                $datenalt = $this->get_item_attribute_meta($item);
                $daten1 = preg_replace('/<li><strong class="wc-item-meta-label">Rücken Logo:</strong>.*?</li>/s', '', $datenalt); //Brustlogo
                $daten2 = preg_replace('/<li><strong class="wc-item-meta-label">Brust Logo:</strong>.*?</li>/s', '', $daten1); //Brustlogo
                $daten3 = preg_replace('/<li><strong class="wc-item-meta-label">Artikelnummer:</strong>.*?</li>/s', '', $daten2); //artnr
                $daten4 = preg_replace('/<li><strong class="wc-item-meta-label">Name:</strong>.*?</li>/s', '', $daten3); //artnr
                $datentext = preg_replace('/<li><strong class="wc-item-meta-label">Preis:</strong>.*?</li>/s', '', $daten4);//preis
                $datenneu = '<i>Artikelnummer: '. $artnr . '</i></br>' . $datentext;
                $data['text'] = $datenneu;
            }
    

    ori is this code:

        if (isset($item['variation_id'])) {
            $data['text'] = $this->get_item_attribute_meta($item);
        }
    

  2. Maybe you can update the &data parameter as per the code below:

    add_filter('bf_wc_lq_connector_order_item_data', function( $data, $item, $product ) {
        $posname    = $item->get_meta('posname');
        $pa_groesse = strtoupper($item->get_meta('pa_groesse'));
        $pa_farbe   = ucfirst($item->get_meta('pa_farbe'));
        $nameneu    = $posname . ' - ' . $pa_farbe . ', ' . $pa_groesse;
        $oriname    = $item['name'];
        if ( empty($posname) ) {
            $nameneu = $oriname;
        }
    
        $artnr = strtoupper($item->get_meta('artnr'));
        if ( empty($artnr) ) {
            $artnr = $product ? $product->get_sku() : '';
        }
    
        $data['sku']  = $artnr;
        $data['name'] = $nameneu;
    
        return $data;
    }, 10, 2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search