welcart 商品の並び順をドラッグアンドドロップで変える

商品マスタ(商品一覧)のテンプレートファイル
/wp-content/plugins/usc-e-shop/includes/usces_item_master_list.php

詳細

/home/hashimoto/www/yoga-lien.com/wp-content/plugins/usc-e-shop/js にファイルを追加
jquery-1.8.0.min.js
jquery-ui.js

商品一覧のテンプレート(一覧大規模変更)
/home/hashimoto/www/yoga-lien.com/wp-content/plugins/usc-e-shop/includes/usces_item_master_list.php

// 並び替えページ
”usces_item_popup.php”を追加(新設)した。

”itemList.class.php” にてソート順を変更した

手法:$rows[L:410付近]の並び順を変えることで成功!

DB wp_posts.to_ping 型をtext → int に変更

// Heart-Lab Start
//-------------------------------------------------

/*
echo '<pre>';
var_dump($rows);
echo '</pre>';*/

// 商品IDのソート順
$id_array = array();
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASSWORD);
$sql = "SELECT * FROM `wp_posts` WHERE `post_mime_type` = 'item' AND `post_type` = 'post' AND `post_status` = 'publish' ORDER BY `to_ping` ASC";
foreach($pdo->query($sql) as $item) {
// 	echo $item[ID].' -- ';
	array_push($id_array, $item[ID]);
}
// var_dump($id_array);

// $rows の並び順を変える
for($i = 0; $id_array[$i] != ''; $i ++){
	for($a = 0; $rows[$a] != ''; $a ++){
		if($id_array[$i] == $rows[$a][ID]){
			$new_rows[$i][ID] = $rows[$a][ID];
			$new_rows[$i][item_code] = $rows[$a][item_code];
			$new_rows[$i][item_name] = $rows[$a][item_name];
		}
	}
}

// echo '<pre>';
// var_dump($new_rows);
// echo '</pre>';

$rows = $new_rows;

// Heart-Lab END
//-------------------------------------------------

”usces_item_popup.php”

<html lang="ja" class="no-js"><head><meta charset="UTF-8">
<!-- WR -->
<style>
a {
 color: gray;
}
.wr_menu li {
	display: inline-block;
	margin: 0 20px 30px 0;
}
.ns {
	display: block;
	float: left;
	border: 1px solid silver;
	width: 500px;
	height: 60px;
	background: white;
	overflow: hidden;
	margin: 0 0 5px 0;
}
.w100 {
	display: block;
	float: left;
	width: 80px;
}
.w200 {
	display: block;
	float: left;
	width: 300px;
	overflow: hidden;
}
#tablenavi {
	display: none;
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL & ~E_NOTICE);
$act1 = ''; $act2 = ''; $act3 = '';
if($_GET["wr"] == 'lesson'){
	$act1 = ' style="color:red;font-weight: bold"';
}elseif($_GET[wr] == 'shopping'){
	$act2 = ' style="color:red;font-weight: bold"';
}else{
	$act3 = ' style="color:red;font-weight: bold"';
}
echo '<div class="wr_menu">';
echo '<li><a href="?page=usces_itemedit&wr=all"'.$act3.'>All</a></li>';
echo '<li><a href="?page=usces_itemedit&wr=lesson"'.$act1.'>Lesson</a></li>';
echo '<li><a href="?page=usces_itemedit&wr=shopping"'.$act2.'>Shopping</a></li>';
echo '</div>';
?>


<script type="text/javascript">
<!--
function p_reload(){
var pwin=window.opener;
pwin.location.reload();
pwin.focus();
window.close();
}
//-->
</script>
<button type="button" onclick="p_reload()" style="float: right;">閉じる</button>
<br><br>


<form action="" method="post">
      <input type="submit" id="submit" value="並び順を保存する" onClick="alert('並べ替え完了です')"/>
            <ul class="sortable">
<!-- WR -->



<?php

include("../../../../wp-config.php");
$pdo = 
new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASSWORD);


// 並べ替え
if($_POST[result] != ''){
	
	$sort_ids = explode(",", $_POST[result]);
	
	for($i = 0; $sort_ids[$i] != ''; $i ++){
		if($_GET[wr] == 'shopping'){
			$a = $i;
		}else{
			$a = $i + 10000;
		}
		$sql = "UPDATE `wp_posts` SET `to_ping` = '$a' WHERE `ID` = '$sort_ids[$i]'";
		$statement = $pdo->query($sql);
	
	}
}


$sql = "SELECT * FROM `wp_posts` WHERE `post_mime_type` = 'item' AND `post_type` = 'post' AND `post_status` = 'publish' ORDER BY `to_ping` ASC";


foreach($pdo->query($sql) as $item) {
	
	$print_flg = 0;
	
	if($_GET[wr] == 'shopping'){
		$sql2 = "SELECT * FROM `wp_term_relationships` WHERE `object_id` = '$item[ID]' AND `term_taxonomy_id` = '5'";
		$statement2 = $pdo->query($sql2);
		$item2 = $statement2->fetch(PDO::FETCH_ASSOC);
		
		if($item2[object_id] != ''){
			$print_flg = 1;
		}
	}
	if($_GET[wr] == 'lesson'){
		$sql2 = "SELECT * FROM `wp_term_relationships` WHERE `object_id` = '$item[ID]' AND `term_taxonomy_id` = '3'";
		$statement2 = $pdo->query($sql2);
		$item2 = $statement2->fetch(PDO::FETCH_ASSOC);
		
		if($item2[object_id] != ''){
			$print_flg = 1;
		}
	}
	
	if( ($_GET[wr] == '') || ($_GET[wr] == 'all') ) $print_flg = 1;
	
	if($print_flg == 1){
		
		// 商品情報取得
		$sql2 = "SELECT * FROM `wp_postmeta` WHERE `post_id` = '$item[ID]' AND `meta_key` = '_isku_'";
		$statement2 = $pdo->query($sql2);
		$item2 = $statement2->fetch(PDO::FETCH_ASSOC);
		
		$hoge = explode(";", $item2[meta_value]);
		
		$wr_array = array();
		for($i = 0; $hoge[$i] != ''; $i ++){
			
			$hhh = explode('"', $hoge[$i]);
			
	// 		echo $i.'> '.$hhh[1].'<br>';
			array_push($wr_array, $hhh[1]);

		}
		

		$ID = $item["ID"]; ///////idのようなモノ
		$code = $wr_array[1];
		$price = number_format($wr_array[7]);
		$stock_num = number_format($wr_array[11]);
		
		// 在庫有無
		if($stock_num == 0){
			$stock_text = '<span style="color:red;">在庫切れ</span>';
		}else{
			$stock_text = '.';
		}
		
		// 公開状態
		if($item[post_status] == 'publish'){
			$open_text = '公開';
		}else{
			$open_text = '<span style="color:red;">非公開</span>';
		}
		
		// カテゴリ
		$sql3 = "SELECT * FROM `wp_term_relationships` WHERE `object_id` = '$ID' AND `term_taxonomy_id` != '2'";
		$statement3 = $pdo->query($sql3);
		$item3 = $statement3->fetch(PDO::FETCH_ASSOC);
		
		$sql4 = "SELECT * FROM `wp_terms` WHERE `term_id` = '$item3[term_taxonomy_id]'";
		$statement4 = $pdo->query($sql4);
		$item4 = $statement4->fetch(PDO::FETCH_ASSOC);
		$category = $item4[name];
		
		
		// 画像
		$sql2 = "SELECT * FROM `wp_postmeta` WHERE `post_id` = '$item[ID]' AND `meta_key` = '_thumbnail_id'";
		$statement2 = $pdo->query($sql2);
		$item2 = $statement2->fetch(PDO::FETCH_ASSOC);
		
		$sql2 = "SELECT * FROM `wp_posts` WHERE `ID` = '$item2[meta_value]'";
		$statement2 = $pdo->query($sql2);
		$item2 = $statement2->fetch(PDO::FETCH_ASSOC);
		$img = $item2[guid];
		$post_name = $item2[post_title];
		
		// url
		$sql2 = "SELECT * FROM `wp_options` WHERE `option_name` = 'siteurl'";
		$statement2 = $pdo->query($sql2);
		$item2 = $statement2->fetch(PDO::FETCH_ASSOC);
		$http_url = $item1[option_value];
		
		$item[post_title] = mb_strimwidth( $item[post_title], 0, 85, '…','UTF-8' );
		
		echo '<li class="ns" id="'.$ID.'">
			<div class="w100"><img src="'.$img.'" width="60" alt="
IMG"></div>
			<div class="w100">'.$ID.'</div>
			<div class="w200">'.$code.'<br>'.$item[post_title].'</div>
			<!--
			<div class="w100">'.$price.'</div>
			<div class="w100">'.$stock_num.'</div>
			<div class="w100">'.$stock_text.'</div>
			<div class="w100">'.$category.'</div>
			<div class="w100">'.$open_text.'</div>
			-->
			</li><!-- ----リスト---- -->';
	}

	

}

?>

<?php

		// url
		$sql2 = "SELECT * FROM `wp_options` WHERE `option_name` = 'siteurl'";
		$statement2 = $pdo->query($sql2);
		$item2 = $statement2->fetch(PDO::FETCH_ASSOC);
		$http_url = $item2[option_value];
?>

<!-- WR -->
             </ul>
      <input type="hidden" id="result" name="result" />
</form>




<script src="<?php echo $http_url; ?>/wp-content/plugins/usc-e-shop/js/jquery-1.8.0.min.js"></script>
<script src="<?php echo $http_url; ?>/wp-content/plugins/usc-e-shop/js/jquery-ui.js"></script>
<script>
$(function() {
	$(".sortable").sortable();
	$(".sortable").disableSelection();
	$("#submit").click(function() {
		var result = $(".sortable").sortable("toArray");
		$("#result").val(result);
		$("form").submit();
	});
});
</script>

<div style="clear:both;"></div>
<!--
<pre>
<?php var_dump($_POST); ?>
</pre>
-->
<!-- WR -->