代码之家  ›  专栏  ›  技术社区  ›  JapeNZ

Woocommerce删除插件过滤器并替换为我自己的

  •  0
  • JapeNZ  · 技术社区  · 1 年前

    我正试图根据自己的需要定制一个插件功能,并设法使我修改后的版本按需显示。 不幸的是,我不知道如何删除原来的函数,所以现在出现了两个实例。

    以下是插件的原始功能:

    function aspwc_split_package_total_rows( $total_rows, $order, $tax_display ) {
        if ( get_option( 'advanced_shipping_packages_display_separately', 'no' ) != 'yes' ) {
            return $total_rows;
        }
    
        /** @var WC_Order $order */
        $shipping_items    = $order->get_items( 'shipping' );
        $key_position      = array_search( 'shipping', array_keys( $total_rows ) );
        $new_shipping_rows = array();
    
        foreach ( $shipping_items as $item ) {
            $package_name = $item->get_meta( 'package_name' ) ?: str_replace( ':', '', __( 'Shipping:', 'woocommerce' ) );
    
            $new_shipping_rows[ 'shipping_' . $item->get_id() ] = array(
                'label' => $package_name . ':', // Package name
                'value' => '<strong>' . $item->get_name() . ':</strong> ' . aspwc_get_shipping_to_display( $item, $order, $tax_display ) . '<br/><small>' . $item->get_meta( 'Items' ) . '</small>',
            );
        }
    
        // Remove original shipping line - Only when new rows are set (which should only happen when package names are stored)
        if ( ! empty( $new_shipping_rows ) ) {
            unset( $total_rows['shipping'] );
        }
    
        // Add package line(s)
        array_splice( $total_rows, $key_position, 0, $new_shipping_rows ); // splice in at position 3
    
        return $total_rows;
    }
    add_filter( 'woocommerce_get_order_item_totals', 'aspwc_split_package_total_rows', 10, 3 );
    

    我正试图使用以下方法删除:

    remove_filter( 'woocommerce_get_order_item_totals', 'aspwc_split_package_total_rows', 10, 3 );
    

    我的自定义函数正在取代它:

    add_filter( 'woocommerce_get_order_item_totals', 'custom_split_package_total_rows', 10, 3 );
    
    function custom_split_package_total_rows( $total_rows, $order, $tax_display ) {
        if ( get_option( 'advanced_shipping_packages_display_separately', 'no' ) != 'yes' ) {
            return $total_rows;
        }
    
        /** @var WC_Order $order */
        $shipping_items    = $order->get_items( 'shipping' );
        $key_position      = array_search( 'shipping', array_keys( $total_rows ) );
        $new_shipping_rows = array();
    
        foreach ( $shipping_items as $item ) {
            $package_name = $item->get_meta( 'package_name' ) ?: str_replace( ':', '', __( 'Shipping:', 'woocommerce' ) );
    
            $new_shipping_rows[ 'shipping_' . $item->get_id() ] = array(
                'label' => $package_name . ':', // Package name
                'value' => aspwc_get_shipping_to_display( $item, $order, $tax_display ) . '<br/><strong>via ' . $item->get_name() . '</strong><br/><small>' . $item->get_meta( 'Items' ) . '</small>',
            );
        }
    
        // Remove original shipping line - Only when new rows are set (which should only happen when package names are stored)
        if ( ! empty( $new_shipping_rows ) ) {
            unset( $total_rows['shipping'] );
        }
    
        // Add package line(s)
        array_splice( $total_rows, $key_position, 0, $new_shipping_rows ); // splice in at position 3
    
        return $total_rows;
    }
    

    一切正常,但我不明白为什么 remove_filter 什么都没做。

    如有任何建议,我们将不胜感激!

    1 回复  |  直到 1 年前
        1
  •  1
  •   GS_    1 年前
    1. remove_filter 应该只有三个属性。因此,请尝试以下remove_filter代码

      remove_filter( 'woocommerce_get_order_item_totals', 'aspwc_split_package_total_rows', 10 );
      
      
    2. 如果它仍然不起作用,请在里面试试 init plugins_loaded 钩子。检查以下代码。

        function gs_plugins_loaded(){
            remove_filter( 'woocommerce_get_order_item_totals', 'aspwc_split_package_total_rows', 10 );
            add_filter( 'woocommerce_get_order_item_totals', 'custom_split_package_total_rows', 10, 3 );
        }
        add_action('plugins_loaded', 'gs_plugins_loaded');