douwei9973 2018-09-20 14:22
浏览 33
已采纳

仅在Woocommerce中编辑产品标签存档页面的痕迹

I have found where in WooCommerce (WC) I can edit directly to get my desired result, which is editing the following function: https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-breadcrumb.php#L222

private function add_crumbs_product_tag() {
        $current_term = $GLOBALS['wp_query']->get_queried_object();
        $this->prepend_shop_page();
        /* translators: %s: product tag */
        $this->add_crumb( sprintf( __( 'My Edit: %s', 'woocommerce' ), $current_term->name ), get_term_link( $current_term, 'product_tag' ) );
    }

Now I know that it is bad practice to directly edit the WC plugin, so I am looking for an alternative, so that when WC updates, the changes aren't overwritten.

Override Attempt

I have tried to add my edited class to my child theme (mytheme-child/woocommerce/includes/class-wc-breadcrumb.php) but this doesn't seem to work.

What can I do?

  • 写回答

1条回答 默认 最新

  • dongmisa4779 2018-09-20 23:41
    关注

    Try the following instead using the specific woocommerce_get_breadcrumb filter hook:

    add_filter( 'woocommerce_get_breadcrumb', 'custom_product_tag_crumb', 20, 2 );
    function custom_product_tag_crumb( $crumbs, $breadcrumb ){
        // Targetting product tags
        $current_taxonomy  = 'product_tag';
        $current_term      = $GLOBALS['wp_query']->get_queried_object();
        $current_key_index = sizeof($crumbs) - 1;
    
        // Only product tags
        if( is_a($current_term, 'WP_Term') && term_exists( $current_term->term_id, $current_taxonomy ) ) {
            // The label term name
            $crumbs[$current_key_index][0] = sprintf( __( 'My Edit: %s', 'woocommerce' ), $current_term->name );
            // The term link (not really necessary as we are already on the page)
            $crumbs[$current_key_index][1] = get_term_link( $current_term, $current_taxonomy );
        }
        return $crumbs;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架