【问题标题】:get HSN Code in Pdf invoice in woocommerce在woocommerce的PDF发票中获取HSN代码
【发布时间】:2019-08-30 21:03:55
【问题描述】:

我尝试在线搜索并联系插件作者,他说可以使用 WordPress get post meta 检索。

我正在使用一个名为 woo-gst 的插件来添加一个名为“prod_hsn_id”的产品属性,该属性在产品编辑页面添加一个名为 HSN 代码的文件,我还使用一个名为 woocommerce pdf invoice 的 pdf 发票插件来生成 pdf发票。现在我想在发票上显示 HSN 代码。

    <?php foreach ( $this->order->get_items( 'line_item' ) as $item_id => $item ) {
        $product = $this->order->get_product_from_item( $item ); ?>
        <tr class="product-row">
            <td>
                <?php echo esc_html( $item['name'] );
                global $wpdb;

                $hidden_order_itemmeta = apply_filters( 'woocommerce_hidden_order_itemmeta', array(
                    '_qty',
                    '_tax_class',
                    '_product_id',
                    '_variation_id',
                    '_line_subtotal',
                    '_line_subtotal_tax',
                    '_line_total',
                    '_line_tax',
                    '_wc_cog_item_cost',
                    '_wc_cog_item_total_cost',
                    '_reduced_stock',
                ) );

                $hidden_order_itemmeta = apply_filters( 'bewpi_hidden_order_itemmeta', $hidden_order_itemmeta );


                foreach ( $this->order->has_meta( $item_id ) as $meta ) {
                    // Skip hidden core fields.
                    if ( in_array( $meta['meta_key'], $hidden_order_itemmeta, true ) ) {
                        continue;
                    }

                    // Skip serialised meta.
                    if ( is_serialized( $meta['meta_value'] ) ) {
                        continue;
                    }

                    // Get attribute data.
                    if ( taxonomy_exists( wc_sanitize_taxonomy_name( $meta['meta_key'] ) ) ) {
                        $term               = get_term_by( 'slug', $meta['meta_value'], wc_sanitize_taxonomy_name( $meta['meta_key'] ) );
                        $meta['meta_key']   = wc_attribute_label( wc_sanitize_taxonomy_name( $meta['meta_key'] ) );
                        $meta['meta_value'] = isset( $term->name ) ? $term->name : $meta['meta_value'];
                    } else {
                        $meta['meta_key'] = apply_filters( 'woocommerce_attribute_label', wc_attribute_label( $meta['meta_key'], $product ), $meta['meta_key'] );
                    }

                    echo '<div class="item-attribute"><span style="font-weight: bold;">' . wp_kses_post( rawurldecode( $meta['meta_key'] ) ) . ': </span>' . wp_kses_post( rawurldecode( $meta['meta_value'] ) ) . '</div>';
                }

                    $field_name = 'hsn_prod_id';

                    // then loop through items in order and print each custom field
                    foreach ( $this->order->get_items() as $item_id => $item ) {
                        if ( $product = $this->order->get_product_from_item( $item ) ) {
                            $location = $product->get_meta( $field_name );
                            if ( !empty($hsn_prod_id) ) {
                                echo '<div class="product-location">HSN Code: '.$hsn_prod_id.'</div>';
                            }
                        }
                    }

                ?>
            </td>```

above is the code In the Invoice Template file I'm trying to display the HSN Code.

【问题讨论】:

    标签: php wordpress metadata invoice


    【解决方案1】:

    我今天也在做同样的事情,没有找到解决方案,最后这就是我所做的并且效果很好。

    我们需要从postmeta table 中提取数据,并获得post idproduct

    如果您执行$product_id = $product-&gt;get_id(),这将返回id (they call it Variation ID)post idproduct id 不一样。 要获得post idproduct,您需要subtract -1 形成$product_id

    这是您可以用来获取 HSN 代码的行:

    $hsn_prod_id = get_post_meta( $product-&gt;get_id()-1, 'hsn_prod_id', true );

    更新:

    subtract -1 的逻辑取决于 Wordpress,WooCommerce 版本可能是。我在新的设置中尝试了同样的方法,但它不起作用。在那里,我刚刚使用了这个 $product_id = $product-&gt;get_id() 值,它起作用了。

    您可以通过查看产品的 id 来确定它。它在 UI 中显示的 ID 以及您在编辑订单时在 URL 中看到的 ID。如果两者不相同,您需要查看序列差异并接听电话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多