【问题标题】:How to add escpos-php in magento?如何在magento中添加escpos-php?
【发布时间】:2018-09-23 16:15:01
【问题描述】:

我正在使用 magento 1.9。我想使用escpos-php driver 将我们的发票打印到 USB 热敏打印机上。我将 escpos-php 库保存在我的 magento 安装的根目录中。在 magento 的自定义模块中,我覆盖了呈现时为 A4 pdf 的默认发票,并尝试制作热发票 pdf(纸张尺寸 C7)。该文件存在于/local/Receipt/Pos/Model/Invoice.php

<?php
  class Receipt_Pos_Model_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
  {
        public function getPdf($invoices = array())
        {
            // I want to access the libraries from here in this 
            // function like shown  below. where 'vendor' is a directory
            // created by myself.
            require(Mage::getBaseDir('lib') .'/vendor/mike42/escpos-php/autoload.php'); // this is the autoloader that comes with escpos-php driver.
            use Mike42\Escpos\PrintConnectors\FilePrintConnector; // Warning is raised at this line.
            use Mike42\Escpos\Printer;
            $connector = new FilePrintConnector("/dev/usb/lp0");
            $printer = new Printer($connector);

         }
    }
   ?>

我现在正在尝试的是,我想从这个/local/Receipt/Pos/Model/Invoice.php 文件中访问 escpos-php 驱动程序的类文件。所以我将 escpos-php 驱动程序的自动加载器的绝对路径添加到 Invoice.php 中的代码中,但它会导致如下所示的警告

Warning: include(Mike42\Escpos\PrintConnectors\PrintConnector.php): failed to open stream: No such file or directory  in /var/www/html/checkout/Gama_V2/shop/lib/Varien/Autoload.php on line 94

I think the autoloader of Magento is also trying to find the class files of the escpos-php driver and fails to load it. But I don't want the magento autoloader work here because, I have already included the autoloader of escpos-php driver which takes care of loading its files. How can I avoid this warning and proceed to print receipts? Please help me!

【问题讨论】:

    标签: magento-1.9.1 escpos zend-autoloader spl-autoloader


    【解决方案1】:

    要让自动加载识别您的外部库,您必须遵循 magento 文件结构。

    外部库通常位于 /lib/ 下

    在您的模块中,您可以将它们用作

    require_once Mage::getBaseDir('lib') . '/Mike42/Escpos/Whatever.php';
    

    【讨论】:

    • 是的,我试过了,但仍然收到同样的警告!我不知道。顺便说一下,当我在网上搜索时,我发现一个类似的问题没有得到解答,我在这里提出这个问题以便更好地理解这个问题。请参考[stackoverflow.com/questions/14154449/…
    • 公开更多代码以供审查,并告诉您将库的哪个部分放在 magento 中。您应该考虑使用 Varien Autoload 而不是外部自动加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多