【问题标题】:Display barcode of each product on the invoice or delivery note, woocommerce在发票或送货单上显示每个产品的条形码,woocommerce
【发布时间】:2020-11-12 12:46:06
【问题描述】:

我需要在发票或交货单上显示产品的条形码,我尝试使用此代码但它没有显示条形码图像:

<?php
    echo get_post_meta( $product_id, '_ywbc_barcode_image', true );
    ?>

我正在使用 YITH WooCommerce Barcodes and QR Codes 插件,它为每个产品添加了一个条形码,在下图中您可以看到该插件添加的元数据(产品发布元数据)。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    假设您可以访问$order 对象或订单ID,您可以在任何您希望显示订单条码的位置添加do_action,如下所示:

    do_action ( 'yith_wc_barcodes_and_qr_filter', $order-&gt;get_id() );

    然后您可以使用它来调用一个操作,该操作将在您添加do_action的所有位置上显示条形码:

    if ( ! function_exists( 'yith_wc_barcodes_and_qr_filter_call_back' ) ) {
        add_action( 'yith_wc_barcodes_and_qr_filter', 'yith_wc_barcodes_and_qr_filter_call_back', 10, 1 );
        function yith_wc_barcodes_and_qr_filter_call_back( $id ) {
            ob_start();
            $css = ob_get_clean();
            YITH_YWBC()->show_barcode( $id, true, $css );
        }
    }
    

    最后一部分放在你的(子)主题的 functions.php 中,或者应该添加一个像代码片段这样的插件。

    如果您需要产品条形码,则必须使用产品 ID 而不是订单 ID,假设您可以访问 $product 对象。

    do_action ( 'yith_wc_barcodes_and_qr_filter', $product-&gt;get_id() );

    【讨论】:

    • 感谢您的帮助,您的代码可以很好地显示Order的条形码,但必须显示每个Product的条形码> 在发票或交货单模板上。你能帮帮我吗?
    • 如果我使用此代码,它只会显示每个产品的条形码,而不是图像&lt;?php echo get_post_meta( $product_id, '_ywbc_barcode_value', true ); ?&gt;
    • THX。我可以用你的代码解决它,我可以显示订单的条形码,对于我做的产品,我只需要更改:$product &lt;?php do_action ( 'yith_wc_barcodes_and_qr_filter_product', $product-&gt;get_id() ); ?&gt; if ( ! function_exists( 'yith_wc_barcodes_and_qr_filter_call_back_product' ) ) { add_action( 'yith_wc_barcodes_and_qr_filter_product', 'yith_wc_barcodes_and_qr_filter_call_back_product', 10, 1 ); function yith_wc_barcodes_and_qr_filter_call_back_product( $product_id ) { ob_start(); $css = ob_get_clean(); YITH_YWBC()-&gt;show_barcode( $product_id, true, $css ); } }
    • 抱歉,回复晚了。很高兴看到您已经解决了这个问题。您确实可以使用产品 ID 来获取产品的条形码。我会更新我的答案。
    【解决方案2】:

    我对这个插件不太熟悉;但是,从您的屏幕截图来看,_ywbc_barcode_image 似乎没有值。

    这可能被认为有点 hacky,但为什么不自己构建文件名呢?示例:

    $barcodeFile = sprintf( "Image%s_QRcode_%s.png", array(
        get_post_meta( $product_id, '_ywbc_barcode_value', true ),
        get_post_meta( $product_id, '_ywbc_barcode_value', true ) ) );
    

    这将为您提供Image5582_QRcode_5582.png 的值。您可以使用它在需要的地方插入;比如在图片标签内。

    <img src="<?php echo site_url(); ?>/wp-content/uploads/yith-barcodes/<?php echo $barcodeFile; ?>">
    

    【讨论】:

    • 感谢您的回答,您提到的代码如下:语法错误意外 T_STRING,期待 ´)´ &lt;?php $barcodeFile = sprintf( "Image%s_QRcode_%s.png", array( get_post_meta( $product_id, '_ywbc_barcode_value', true ), get_post_meta( $product_id, '_ywbc_barcode_value', true ) ); ?&gt; &lt;img src="&lt;?php echo site_url() ?&gt;/wp-content/uploads/yith-barcodes/&lt;?php echo $barcodeFile ?&gt;"&gt;
    • @Williams 对此感到抱歉。我已经更新了我的答案以包括缺少的 ')'。
    • 现在它显示一个无效的图像,该图像的 url 是:mi-domain/wp-content/uploads/yith-barcodes 显然你没有加载图像的完整地址来显示它。
    • 您必须在系统上找到图像,并且可能需要根据您的特定需要调整 IMG 标签。这不是一劳永逸的解决方案。
    • 我可以将它们永久定位到的图像:mi-domain/wp-content/uploads/yith-barcodes/Image5582_QRcode_5582.png 我可以像这样直接可视化它们而不会出现问题。
    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2018-02-17
    • 2018-01-17
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多