【问题标题】:Override Plugin function.php覆盖插件function.php
【发布时间】:2021-09-11 09:00:29
【问题描述】:

您好,我在插件中做了一些更改,但更新后我必须每次更改代码。现在如何为functions.php 构建一个过滤器。这是我所做的更改。

它们只是一些可变的变化。我需要这些才能正确同步。

我希望有人可以帮助我构建这个:)

这里是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;
}

这里是变量的变化。 模组:

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;
}

【问题讨论】:

    标签: wordpress filter plugins self-modifying


    【解决方案1】:

    也许您可以按照以下代码更新 &data 参数:

    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);
    

    【讨论】:

    • 你的答案,但我已经尝试过这个并且我得到一个错误:(
    • 其实wordpress可以通过过滤器修改值或者通过action添加额外的逻辑。但是如果源代码中没有apply_filters或do_action,则无法修改源代码。
    【解决方案2】:

    现在在路人的帮助下,我迈出了第一步。

    另一个步骤是

    我在同一个文件中有这段代码

           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 是这个代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2015-08-27
      • 1970-01-01
      相关资源
      最近更新 更多