从事Wordpress/Woocommerce架构的工作,我是Woocommerce的新手,在我从事django和laravel框架之前,该项目是一个插件wp,其中一个小表单适合选择标签,以便按sku返回一些产品。我可以获取产品对象,但我不能覆盖ajax响应返回的wp\\U查询,同时也不能用自定义数据覆盖归档循环模板并显示最终结果。
这是我的班级:
if(!defined(\'EXCELREADER_ASSETS_URL\'))
define(\'EXCELREADER_ASSETS_URL\', dirname( FILE ));
class GetAllMenu
{
public $tab;
public $id_product;
public function __construct(){
add_action( \'search-phone\', array( $this, \'render_phone_search\' ) );
add_action( \'wp_enqueue_scripts\', array( $this, \'init_plugin\' ) );
add_action(\'wp_ajax_render_phone_search\', array($this,\'render_phone_search\'));
add_action(\'wp_ajax_nopriv_render_phone_search\', array($this,\'render_phone_search\'));
add_action( \'search-phone\', array( $this, \'display_search_result\' ) );
add_action(\'wp_ajax_display_search_result\', array($this,\'display_search_result\'));
add_action(\'wp_ajax_nopriv_display_search_result\', array($this,\'display_search_result\'));
}
public function init_plugin()
{
wp_enqueue_script(
\'ajax_script\',
plugins_url( \'assets/js/admin.js\', EXCELREADER_ASSETS_URL ),
array(\'jquery\'),
TRUE
);
wp_localize_script(
\'ajax_script\',
\'searchAjax\',
array(
\'url\' => admin_url(\'admin-ajax.php\'),
\'nonce\' => wp_create_nonce( "search-phone" ),
)
);
}
public function render_phone_search(){
$id = urldecode($_POST[\'phone_id\']);
if(isset($id)){
$objPhone = new ER_PhoneName();
$phone = $objPhone->get_phones_by_mark(trim($id));
include(EXCELREADER_TEMPLATE_URL . \'/search-phone-widget.php\');
}
exit;
}
public function render_mark_search(){
$mark = new ER_PhoneMark();
$data = $mark->call_all_mark_in_db();
include(EXCELREADER_TEMPLATE_URL . \'/search-widget.php\');
}
public function display_search_result(){
$sku = urldecode($_POST[\'sku_universel\']);
$sku = explode(\',\',$sku);
$this->tab = array();
foreach($sku as $res){
$product = PhpSpreadReader::select_product_by_sku($res);
if($product){
$this->id_product[] = $product->get_id();
$this->tab[] = $product;
}
}
$args = array(
\'post_type\' => array(\'products\'),
\'post_status\' => array(\'publish\'),
\'post__in\' => $this->id_product
);
// The Query here is my problem =====================================
$ajaxposts = new WP_Query( $args );
$response = \'\';
// The Query i want to use my archive product in my theme not this one
if ( $ajaxposts->have_posts() ) {
while ( $ajaxposts->have_posts() ) {
$ajaxposts->the_post();
$response .= get_template_part(\'woocommerce/content-product\');
}
} else {
$response .= get_template_part(\'none\');
}
echo $response;
//echo get_language_attributes();
//echo $this->display_product_result();
exit;
}
public function changing_query_post($wp_query){
$wp_query->posts;
if ( $wp_query->is_home() && $wp_query->is_main_query() ) {
$wp_query->set( \'post__in\', array($this->tab) );
}
return $wp_query;
}
public function render_search(){
$this->render_mark_search();
}
}
Here is my ajax call in js :
$(document).on("click", "#ex-form-submit", (e) => {
e.preventDefault()
if($("#option-mark").val() === "0"){
return;
}
let str = {
\'action\': \'display_search_result\',
\'sku_universel\': $("#select-phone").val(),
};
// console.log(searchAjax.url);
$.ajax({
type : "post",
dataType : "html",
url : searchAjax.url,
data : str,
success: (response) => {
$(\'.grilleproduits\').html(response).resize();
console.log(response);
},
error : (error) => console.log(error)
});
});
我可以在js中显示所有内容,但这不是一种正确的方式,因为在显示结果时会出现一些问题,例如产品标题翻译不符合要求。
提前感谢您的支持,并对我的nooby技能表示抱歉!我只在一周内回答,因为我没有周末考试的代码和网站。
编辑:我忘了说这是我正在开发的插件