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 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存