在Woocommerce购物车和结帐中显示商品类别名称

时间:2018-03-14 17:13:22

标签: php woocommerce cart checkout custom-taxonomy

我正在使用Woocommerce 3.3.3。和Visual Products Configurator 4.0

My web site中,您可以将某些商品添加到购物车并继续结帐

  1. 我有edited cart/cart.php模板,用于在购物车中显示我的商品类别名称(从第75行到第79行)。
  2. 代码

    <?php
            do_action( 'woocommerce_review_order_before_cart_contents' );
    
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
                $terms = get_the_terms( $product_id, 'product_cat' );
                foreach ($terms as $term) {
                $product_cat = $term->name;
                }
                echo $product_cat ;
    

    这个位置好吗?

    1. 我有edited checkout/review-order.php在结帐时显示我的类别名称(从第36行到第41行):

          foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
              $_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
              $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
              $terms = get_the_terms( $product_id, 'product_cat' );
              foreach ($terms as $term) {
              $product_cat = $term->name;
              }
              echo $product_cat ;
      

      我的类别名称显示两次。我该如何解决这个问题?

    2. 之后,我可以在结帐下看到我的类别名称,但会显示两次

      如何避免这种显示重复?
      安置是否正常?

1 个答案:

答案 0 :(得分:1)

  

要显示产品类别内嵌链接名称,请使用wc_get_product_category_list()

对于第1点:

您应该使用以下专用函数替换代码(更紧凑)

export class Index extends Component {
    state = {
        client: null
    }

    async componentWillMount() {
        const httpLink = new HttpLink({ uri: 'https://v9zqq45l3.lp.gql.zone/graphql' })

        const link = ApolloLink.from([ httpLink ])

        const cache = new InMemoryCache({
            dataIdFromObject: (object) => {
                switch (object.__typename) {
                    // User is whatever type "me" query resolves to
                    case 'User':
                        return object.name
                    default:
                        return object.id || object._id
                }
            }
        })

        await persistCache({
            cache,
            storage: window.localStorage,
            debug: true
        })

        const client = new ApolloClient({
            link,
            cache
    })

    this.setState({ client })
    }

    render() {
        return !this.state.client ? (
            null
        ) : (
            <ApolloProvider client={this.state.client}>
                <App />
            </ApolloProvider>
        )
    }
}

ReactDOM.render(<Index />, document.getElementById('root'))

第2点:

您应该用以下内容替换您的代码以避免重复和格式错误的html (在模板echo wc_get_product_category_list( $cart_item['product_id'] ); 文件上):

checkout/review-order.php

不要忘记你在html表中添加输出......