2021年由Jeremy進行代碼開發

2023年我更新plant網站重新梳理一下設定方式

第一步、在網站上放php檔案

檔案位置:
D:\LiaoDIn\wordpress 外掛\5外包程式\外包程式- woo會員添加Barcode (Jeremy)\woocommerce 會員添加Barcode_v4.0\代碼\barcode

裡面有兩個檔案:
barcode_newmenu.php 以及 tcpdf_barcodes_1d.php

將此兩個檔案上傳至網站資料夾
[網站目錄底下]/wp-content/plugins/barcode/
barcode資料夾要自行新增,新增完畢後將兩個php檔案放入

barcode_newmenu.php

<?php
	require_once $_SERVER['DOCUMENT_ROOT'] .'/wp-content/plugins/barcode/tcpdf_barcodes_1d.php';
	$user_id = get_current_user_id();
	$useridlen = strlen($user_id);
	$getzero = substr('0000000000000000',0,-$useridlen);
	$tmpnum = $getzero.$user_id;
	$code = new TCPDFBarcode($tmpnum,'C128');
	$barcode = $code->getBarcodeSVGcode(1,30,'black',true,'會員專屬條碼');
	echo "<div onclick='drawInlineSVG(\"".$tmpnum."\")'>".$barcode." </div>";
?>

tcpdf_barcodes_1d.php

<?php
class TCPDFBarcode
{
    protected $barcode_array;
    protected $image;
    public function __construct($code, $type)
    {
        $this->setBarcode($code, $type);

        if ( $this->image instanceof Imagick ) {
            // We don't need the original in memory anymore.
            $this->image->clear();
            $this->image->destroy();
        }
    }
    public function getBarcodeArray()
    {
        return $this->barcode_array;
    }
    public function getBarcodeSVGcode($w = 2, $h = 30, $color = 'black',$needtitle=false,$titlename='',$background='white',$idx=5)
    {
        $repstr = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
        $getcode = strtr($this->barcode_array['code'], $repstr);
        $svgh = $needtitle?$h+40:$h+30;
        $svg = '<' . '?' . 'xml version="1.0" standalone="no"' . '?' . '>' . "\n";
        $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
        $svg .= '<svg id="RAN'.$getcode.'" style="background: '.$background.'" width="' . (round(($this->barcode_array['maxw'] * $w), 3)+28) . '" height="' . ($svgh+10) . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . "\n";
        $svg .= "\t" . '<desc>' . $getcode . '</desc>' . "\n";
        $ty=0;
        if($needtitle){
            $ty+=20;
            $svg .= "\t<text x=\"25\" y=\"{$ty}\"
           style=\"font-family: 'Super Sans', Helvetica, sans-serif;font-weight: bold; font-style: normal;\">
           {$titlename}</text>". "\n";
            $ty+=10;
        }
        $svg .= "\t" . '<g id="bars" fill="' . $color . '" stroke="none">' . "\n";
        $x = 10;
        $tmpnum=10+$ty;
        foreach ($this->barcode_array['bcode'] as $k => $v) {
            $bw = round(($v['w'] * $w), 3);
            $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
            if ($v['t']) {
                $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3)+$ty;
                $svg .= "\t\t" . '<rect x="' . $x . '" y="' . $y . '" width="' . $bw . '" height="' . $bh . '" />' . "\n";
                $tmpnum +=1;
            }
            $x += $bw;
        }
        $svg .= "\t" . '</g>' . "\n";
        $svg .= "<text x=\"{$idx}\" y=\"{$tmpnum}\"style=\" font-style: normal;\">{$getcode}</text>". "\n";
        $svg .= '</svg>' . "\n";

        return $svg;
    }
    public function setBarcode($code, $type)
    {
        switch (strtoupper($type)) {
            case 'C128':{ 
                    $arrcode = $this->barcode_c128($code, '');
                    break;
                }
            default:{
                    $this->barcode_array = false;
                    $arrcode = false;
                    break;
                }
        }
        $this->barcode_array = $arrcode;
    }
    protected function barcode_c128($code, $type = '')
    {
        $chr = array(
            '212222', 
            '222122', 
            '222221', 
            '121223', 
            '121322', 
            '131222', 
            '122213', 
            '122312', 
            '132212', 
            '221213', 
            '221312', 
            '231212', 
            '112232', 
            '122132', 
            '122231', 
            '113222', 
            '123122', 
            '123221', 
            '223211', 
            '221132', 
            '221231', 
            '213212', 
            '223112', 
            '312131', 
            '311222', 
            '321122', 
            '321221', 
            '312212', 
            '322112', 
            '322211', 
            '212123', 
            '212321', 
            '232121', 
            '111323', 
            '131123', 
            '131321', 
            '112313', 
            '132113', 
            '132311', 
            '211313', 
            '231113', 
            '231311', 
            '112133', 
            '112331', 
            '132131', 
            '113123', 
            '113321', 
            '133121', 
            '313121', 
            '211331', 
            '231131', 
            '213113', 
            '213311', 
            '213131', 
            '311123', 
            '311321', 
            '331121', 
            '312113', 
            '312311', 
            '332111', 
            '314111', 
            '221411', 
            '431111', 
            '111224', 
            '111422', 
            '121124', 
            '121421', 
            '141122', 
            '141221', 
            '112214', 
            '112412', 
            '122114', 
            '122411', 
            '142112', 
            '142211', 
            '241211', 
            '221114', 
            '413111', 
            '241112', 
            '134111', 
            '111242', 
            '121142', 
            '121241', 
            '114212', 
            '124112', 
            '124211', 
            '411212', 
            '421112', 
            '421211', 
            '212141', 
            '214121', 
            '412121', 
            '111143', 
            '111341', 
            '131141', 
            '114113', 
            '114311', 
            '411113', 
            '411311', 
            '113141', 
            '114131', 
            '311141', 
            '411131', 
            '211412', 
            '211214', 
            '211232', 
            '233111', 
            '200000', 
        );
        $keys_a = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
        $keys_a .= chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5) . chr(6) . chr(7) . chr(8) . chr(9);
        $keys_a .= chr(10) . chr(11) . chr(12) . chr(13) . chr(14) . chr(15) . chr(16) . chr(17) . chr(18) . chr(19);
        $keys_a .= chr(20) . chr(21) . chr(22) . chr(23) . chr(24) . chr(25) . chr(26) . chr(27) . chr(28) . chr(29);
        $keys_a .= chr(30) . chr(31);
        $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . chr(127);
        $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101);
        $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100);
        $code_data = array();
        $len = strlen($code);

        switch (strtoupper($type)) {
            case 'A':{ 
                    $startid = 103;
                    for ($i = 0; $i < $len; ++$i) {
                        $char = $code[$i]; 
                        $char_id = ord($char);
                        if (($char_id >= 241) and ($char_id <= 244)) {
                            $code_data[] = $fnc_a[$char_id];
                        } elseif (($char_id >= 0) and ($char_id <= 95)) {
                            $code_data[] = strpos($keys_a, $char);
                        } else {
                            return false;
                        }
                    }
                    break;
                }
            case 'B':{ 
                    $startid = 104;
                    for ($i = 0; $i < $len; ++$i) {
                        $char = $code[$i]; 
                        $char_id = ord($char);
                        if (($char_id >= 241) and ($char_id <= 244)) {
                            $code_data[] = $fnc_b[$char_id];
                        } elseif (($char_id >= 32) and ($char_id <= 127)) {
                            $code_data[] = strpos($keys_b, $char);
                        } else {
                            return false;
                        }
                    }
                    break;
                }
            case 'C':{ 
                    $startid = 105;
                    if (ord($code[0]) == 241) {
                        $code_data[] = 102;
                        $code = substr($code, 1);
                        --$len;
                    }
                    if (($len % 2) != 0) {
                        return false;
                    }
                    for ($i = 0; $i < $len; $i += 2) {
                        $chrnum = $code[$i] . $code[$i + 1]; 
                        if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
                            $code_data[] = intval($chrnum);
                        } else {
                            return false;
                        }
                    }
                    break;
                }
            default:{ 
                    $sequence = array();
                    $numseq = array();
                    preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE);
                    if (isset($numseq[1]) and !empty($numseq[1])) {
                        $end_offset = 0;
                        foreach ($numseq[1] as $val) {
                            $offset = $val[1];
                            if ($offset > $end_offset) {
                                $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset, ($offset - $end_offset))));
                            }
                            $slen = strlen($val[0]);
                            if (($slen % 2) != 0) {
                                --$slen;
                            }
                            $sequence[] = array('C', substr($code, $offset, $slen), $slen);
                            $end_offset = $offset + $slen;
                        }
                        if ($end_offset < $len) {
                            $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset)));
                        }
                    } else {
                        $sequence = array_merge($sequence, $this->get128ABsequence($code));
                    }
                    foreach ($sequence as $key => $seq) {
                        switch ($seq[0]) {
                            case 'A':{
                                    if ($key == 0) {
                                        $startid = 103;
                                    } elseif ($sequence[($key - 1)][0] != 'A') {
                                        if (($seq[2] == 1) and ($key > 0) and ($sequence[($key - 1)][0] == 'B') and (!isset($sequence[($key - 1)][3]))) {
                                            $code_data[] = 98;
                                            $sequence[$key][3] = true;
                                        } elseif (!isset($sequence[($key - 1)][3])) {
                                            $code_data[] = 101;
                                        }
                                    }
                                    for ($i = 0; $i < $seq[2]; ++$i) {
                                        $char = $seq[1][$i]; 
                                        $char_id = ord($char);
                                        if (($char_id >= 241) and ($char_id <= 244)) {
                                            $code_data[] = $fnc_a[$char_id];
                                        } else {
                                            $code_data[] = strpos($keys_a, $char);
                                        }
                                    }
                                    break;
                                }
                            case 'B':{
                                    if ($key == 0) {
                                        $tmpchr = ord($seq[1][0]);
                                        if (($seq[2] == 1) and ($tmpchr >= 241) and ($tmpchr <= 244) and isset($sequence[($key + 1)]) and ($sequence[($key + 1)][0] != 'B')) {
                                            switch ($sequence[($key + 1)][0]) {
                                                case 'A':{
                                                        $startid = 103;
                                                        $sequence[$key][0] = 'A';
                                                        $code_data[] = $fnc_a[$tmpchr];
                                                        break;
                                                    }
                                                case 'C':{
                                                        $startid = 105;
                                                        $sequence[$key][0] = 'C';
                                                        $code_data[] = $fnc_a[$tmpchr];
                                                        break;
                                                    }
                                            }
                                            break;
                                        } else {
                                            $startid = 104;
                                        }
                                    } elseif ($sequence[($key - 1)][0] != 'B') {
                                        if (($seq[2] == 1) and ($key > 0) and ($sequence[($key - 1)][0] == 'A') and (!isset($sequence[($key - 1)][3]))) {
                                            $code_data[] = 98;
                                            $sequence[$key][3] = true;
                                        } elseif (!isset($sequence[($key - 1)][3])) {
                                            $code_data[] = 100;
                                        }
                                    }
                                    for ($i = 0; $i < $seq[2]; ++$i) {
                                        $char = $seq[1][$i]; 
                                        $char_id = ord($char);
                                        if (($char_id >= 241) and ($char_id <= 244)) {
                                            $code_data[] = $fnc_b[$char_id];
                                        } else {
                                            $code_data[] = strpos($keys_b, $char);
                                        }
                                    }
                                    break;
                                }
                            case 'C':{
                                    if ($key == 0) {
                                        $startid = 105;
                                    } elseif ($sequence[($key - 1)][0] != 'C') {
                                        $code_data[] = 99;
                                    }
                                    for ($i = 0; $i < $seq[2]; $i += 2) {
                                        $chrnum = $seq[1][$i] . $seq[1][$i + 1]; 
                                        $code_data[] = intval($chrnum);
                                    }
                                    break;
                                }
                        }
                    }
                }
        }
        $sum = $startid;
        foreach ($code_data as $key => $val) {
            $sum += ($val * ($key + 1));
        }
        $code_data[] = ($sum % 103);
        $code_data[] = 106;
        $code_data[] = 107;
        array_unshift($code_data, $startid);
        $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
        foreach ($code_data as $val) {
            $seq = $chr[$val];
            for ($j = 0; $j < 6; ++$j) {
                if (($j % 2) == 0) {
                    $t = true; 
                } else {
                    $t = false; 
                }
                $w = $seq[$j]; 
                $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
                $bararray['maxw'] += $w;
            }
        }
        return $bararray;
    }
    protected function get128ABsequence($code)
    {
        $len = strlen($code);
        $sequence = array();
        $numseq = array();
        preg_match_all('/([\0-\31])/', $code, $numseq, PREG_OFFSET_CAPTURE);
        if (isset($numseq[1]) and !empty($numseq[1])) {
            $end_offset = 0;
            foreach ($numseq[1] as $val) {
                $offset = $val[1];
                if ($offset > $end_offset) {
                    $sequence[] = array('B', substr($code, $end_offset, ($offset - $end_offset)), ($offset - $end_offset));
                }
                $slen = strlen($val[0]);
                $sequence[] = array('A', substr($code, $offset, $slen), $slen);
                $end_offset = $offset + $slen;
            }
            if ($end_offset < $len) {
                $sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset));
            }
        } else {
            $sequence[] = array('B', $code, $len);
        }
        return $sequence;
    }
} 

第二步、新增function.php代碼 (後台顯示條碼)

檔案位置:
D:\LiaoDIn\wordpress 外掛\5外包程式\外包程式- woo會員添加Barcode (Jeremy)\woocommerce 會員添加Barcode_v4.0\代碼

代碼內容參考 functionsadd.txt 內資料

新增位置1:[網站目錄底下 ]/wp-includes/functions.php 裡面新增代碼

新增位置2:使用外掛Code Snippets,並新增程式碼片段

/**
 * 後台->使用者->全部使用者--添加欄位 [title]
 * columns ["cb"=>勾選框,"username"=>使用者名稱,"name"=>顯示名稱,"email"=>電子郵件地址,"role"=>使用者角色,"posts"=>文章]
 */
function contributes( $columns ) {
	foreach($columns as $ck=>$cv){
		if($ck=="cb"){
			$newcolumns[$ck]=$cv;
			$newcolumns['id'] = __( "ID" );
		}else{
			$newcolumns[$ck]=$cv;
		}
	}
	$newcolumns['barcode'] = __( "Barcode");
	return $newcolumns;
}
add_filter( 'manage_users_columns', 'contributes' );

/**
 * 後台->使用者->全部使用者--table 數據
 */
function contributes_columns( $value, $column_name, $user_id ) {
	if ( $column_name == "id" )
		return $user_id;
	if ( $column_name == "barcode"){
		require_once $_SERVER['DOCUMENT_ROOT'] .'/wp-content/plugins/barcode/tcpdf_barcodes_1d.php';
		$useridlen = strlen($user_id);
		$getzero = substr('0000000000000000',0,-$useridlen);
		$code = new TCPDFBarcode($getzero.$user_id,'C128');
		$barcode = $code->getBarcodeSVGcode(1,30,'black',FALSE,'','white',10);
		return "<div>".$barcode." </div>";
	}
	return $value;
}
add_action( 'manage_users_custom_column', 'contributes_columns', 10, 3 );

/**
 * 前台使用下載svg圖檔js
*/
function svgDljs(){
	?>
		<script type="text/javascript">
			function drawInlineSVG(codenum) {
				var fileName = codenum;
				var imgType='jpeg';
				var svgid = '#RAN'+codenum;
				var svgget = document.querySelector(svgid);
				var This = this;
				//1.给svg标签添加属性:version和xmlns
				[
					['version', 1.1],
					['xmlns', "http://www.w3.org/2000/svg"],
				].forEach(function(item){
					svgget.setAttribute(item[0], item[1]);
				});
				// 2.获取到svg标签+标签内的所有元素
				var str = svgget.parentNode.innerHTML;
				//3.创建img
				var img = document.createElement('img');
				// 4.svg格式的base64图像
				img.setAttribute('src', 'data:image/svg+xml;base64,'+ btoa(unescape(encodeURIComponent(str))));
				//base64格式的svg
				//document.getElementById('baseSvg').src='data:image/svg+xml;base64,'+ btoa(unescape(encodeURIComponent(str)));
				// 5.转换成指定图片格式
				img.onload = function(){
					// 1.创建canvas
					var canvas = document.createElement('canvas');
					var context = canvas.getContext("2d");
					canvas.width = img.width;
					canvas.height = img.height;
					// 2.根据base64格式的svg生成canvas
					context.drawImage(img, 0, 0);
					// 3.将canvas转字符串(按指定好的图片格式)
					var canvasData = canvas.toDataURL("image/"+ imgType);
					// 4.创建图片元素
					var img2 = document.createElement('img');
					// 5.生成图片
					img2.setAttribute('src', canvasData);
					// 6.下载该图片
					img2.onload = function() {
						var a = document.createElement("a");
						// 下载
						a.download = fileName + "."+ imgType;
						a.href = img2.getAttribute('src');
						a.click();
					};
				};
			}
		</script>
	<?php
}
add_action('wp_head','svgDljs');

第三步、在navigation.php添加代碼 (前台顯示條碼)

檔案位置:
D:\LiaoDIn\wordpress 外掛\5外包程式\外包程式- woo會員添加Barcode (Jeremy)\woocommerce 會員添加Barcode_v4.0\代碼

代碼內容參考 navigationadd.txt 內資料

代碼添加位置:
[網站目錄底下 ]/wp-content/plugins/woocommerce/templates/myaccount/navigation.php
這個文件底下添加代碼
主要貼在 </ul> 這個標籤底下

如果擔心外掛更新會導致該代碼消失,可以將navigation.php更新後,放置到主題的資料夾

主題資料夾位置:
[網站目錄底下 ]/wp-content/themes/主題資料夾/woocommerce/myaccount

將navigation.php放進主題的myaccount資料夾即可

其他調整

前台文字對齊調整 + 條碼背景顏色

如果覺得我的帳號頁面,條碼的文字沒有對齊

在tcpdf_barcodes_1d.php裡面

public function getBarcodeSVGcode($w = 2, $h = 30, $color = 'black',$needtitle=false,$titlename='',$background='white',$idx=5)

可以將$idx的數值改為0,這是文字x軸的起始位置

white就是背景顏色,如果要透明背景可以調整為 #fff0

後台文字對齊調整 + 條碼背景顏色

如果覺得在後台>使用者,條碼的文字沒有對齊

可以修改function.php的代碼

/**
 * 後台->使用者->全部使者--table 數據
 */
function contributes_columns( $value, $column_name, $user_id ) {
	if ( $column_name == "id" )
		return $user_id;
	if ( $column_name == "barcode"){
		require_once $_SERVER['DOCUMENT_ROOT'] .'/wp-content/plugins/barcode/tcpdf_barcodes_1d.php';
		$useridlen = strlen($user_id);
		$getzero = substr('0000000000000000',0,-$useridlen);
		$code = new TCPDFBarcode($getzero.$user_id,'C128');
		$barcode = $code->getBarcodeSVGcode(1,30,'black',FALSE,'','white',10);
		return "<div>".$barcode." </div>";
	}
	return $value;
}

在getBarcodeSVGcode可以調整x軸與條碼的背景顏色

white就是背景顏色,如果要透明背景可以調整為 #fff0

10就是x軸的位置,可以調整為自己覺得可以的數值,plant後台是設定為15

點擊圖片會自動下載 開啟與關閉

預設代碼是點擊圖片後自動下載條碼圖片

如果要關閉這個功能只要調整function.php的代碼

在 img.onload = function() 後面一段

// 1.创建canvas
var canvas = document.createElement('canvas');
var context = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
// 2.根据base64格式的svg生成canvas
context.drawImage(img, 0, 0);
// 3.将canvas转字符串(按指定好的图片格式)
var canvasData = canvas.toDataURL("image/"+ imgType);
// 4.创建图片元素
var img2 = document.createElement('img');
// 5.生成图片
img2.setAttribute('src', canvasData);
// 6.下载该图片
img2.onload = function() {
  var a = document.createElement("a");
  // 下载
  a.download = fileName + "."+ imgType;
  a.href = img2.getAttribute('src');
  a.click();
};

把 // 6.下载该图片 img2.onload =

這整段刪除或是註解掉就不會有點擊下載圖片的功能了

使用YITH WooCommerce Customize My Account Page客製帳號頁面

如果使用這個的外掛來客製我的帳號頁面

navigationadd.txt 裡面的代碼要放到

/wp-content/plugins/yith-woocommerce-customize-myaccount-page/templates

裡面的ywcmap-myaccount-menu.php

最後修改日期: 2023 年 10 月 4 日

作者