我想允许用户按某些属性值进行搜索,例如使用 ajax 的颜色和品牌
I want to allow users to search by certain attribute values, for example color and brand using ajax
我正在尝试使用名称属性以编程方式检索产品列表。
我想允许用户使用 ajax.
按某些属性值进行搜索,例如颜色和品牌
这是我的代码
<?php
require_once 'app/Mage.php';
umask(0);
ini_set('display_errors', 1);
ini_set('max_execution_time', 3600);
Mage::app();
Mage::getSingleton('core/session', array(
'name' => 'frontend'
));
$product_search = $_REQUEST['term'];
if ($product_search) {
$productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name')->addAttributeToFilter(array(
array(
'attribute' => 'name',
'like' => '%' . $product_search . '%'
)
))->addAttributeToSelect('price')->addAttributeToSelect('thumbnail')->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
$imageHelper = Mage::helper('catalog/image');
foreach ($productCollection as $_product) {
$thumbail_src = "";
$url = $_product->getProductUrl();
$data = ' <div class="autocomplete-image"> <img src="' . $imageHelper->init($_product, 'thumbnail')->resize(50, 75) . '"></div> <div class="top">' . $_product->getName() . '</div><li><i class="fa fa-inr" aria-hidden="true"></i><span class="price">' . $_product->getPrice() . '</span></li> ';
$respones[] = array(
'id' => $_product->getId(),
'name' => $_product->getName(),
'data' => $data,
'price' => $_product->getPrice(),
'qty' => $_product->getQty()
);
}
echo json_encode($respones);
}
?>
请尝试以下代码:
<?php
require_once 'app/Mage.php';
umask(0);
ini_set('display_errors', 1);
ini_set('max_execution_time', 3600);
Mage::app();
Mage::getSingleton('core/session', array(
'name' => 'frontend'
));
$product_search = $_REQUEST['term'];
$attrArray = $arrayName = array('color','brand');
if ($product_search) {
$productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name','brand','color')->addAttributeToSelect('price')->addAttributeToSelect('thumbnail')->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
$searchArray = array(
array(
'attribute' => 'name',
'like' => '%' . $product_search . '%'
)
);
foreach ($attrArray as $key => $code) {
$options = getAttrOptions($code);
$optionId=null;
if($options){
foreach ($options as $key => $value) {
if(strtolower($value['label']) == strtolower($product_search)){
$searchArray[] = array(
'attribute' => $code,
'eq' => $value['value']
)
;
}
}
}
}
$productCollection->addAttributeToFilter($searchArray);
$imageHelper = Mage::helper('catalog/image');
foreach ($productCollection as $_product) {
$thumbail_src = "";
$url = $_product->getProductUrl();
$data = ' <div class="autocomplete-image"> <img src="' . $imageHelper->init($_product, 'thumbnail')->resize(50, 75) . '"></div> <div class="top">' . $_product->getName() . '</div><li><i class="fa fa-inr" aria-hidden="true"></i><span class="price">' . $_product->getPrice() . '</span></li> ';
$respones[] = array(
'id' => $_product->getId(),
'name' => $_product->getName(),
'data' => $data,
'color' => $_product->getColor(),
'price' => $_product->getPrice(),
'qty' => $_product->getQty()
);
}
echo json_encode($respones);
}
function getAttrOptions($code){
$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute($code);
if ($attr && $attr->usesSource()) {
return $attr->getSource()->getAllOptions();
}
}
?>
我正在尝试使用名称属性以编程方式检索产品列表。 我想允许用户使用 ajax.
按某些属性值进行搜索,例如颜色和品牌这是我的代码
<?php
require_once 'app/Mage.php';
umask(0);
ini_set('display_errors', 1);
ini_set('max_execution_time', 3600);
Mage::app();
Mage::getSingleton('core/session', array(
'name' => 'frontend'
));
$product_search = $_REQUEST['term'];
if ($product_search) {
$productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name')->addAttributeToFilter(array(
array(
'attribute' => 'name',
'like' => '%' . $product_search . '%'
)
))->addAttributeToSelect('price')->addAttributeToSelect('thumbnail')->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
$imageHelper = Mage::helper('catalog/image');
foreach ($productCollection as $_product) {
$thumbail_src = "";
$url = $_product->getProductUrl();
$data = ' <div class="autocomplete-image"> <img src="' . $imageHelper->init($_product, 'thumbnail')->resize(50, 75) . '"></div> <div class="top">' . $_product->getName() . '</div><li><i class="fa fa-inr" aria-hidden="true"></i><span class="price">' . $_product->getPrice() . '</span></li> ';
$respones[] = array(
'id' => $_product->getId(),
'name' => $_product->getName(),
'data' => $data,
'price' => $_product->getPrice(),
'qty' => $_product->getQty()
);
}
echo json_encode($respones);
}
?>
请尝试以下代码:
<?php
require_once 'app/Mage.php';
umask(0);
ini_set('display_errors', 1);
ini_set('max_execution_time', 3600);
Mage::app();
Mage::getSingleton('core/session', array(
'name' => 'frontend'
));
$product_search = $_REQUEST['term'];
$attrArray = $arrayName = array('color','brand');
if ($product_search) {
$productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name','brand','color')->addAttributeToSelect('price')->addAttributeToSelect('thumbnail')->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
$searchArray = array(
array(
'attribute' => 'name',
'like' => '%' . $product_search . '%'
)
);
foreach ($attrArray as $key => $code) {
$options = getAttrOptions($code);
$optionId=null;
if($options){
foreach ($options as $key => $value) {
if(strtolower($value['label']) == strtolower($product_search)){
$searchArray[] = array(
'attribute' => $code,
'eq' => $value['value']
)
;
}
}
}
}
$productCollection->addAttributeToFilter($searchArray);
$imageHelper = Mage::helper('catalog/image');
foreach ($productCollection as $_product) {
$thumbail_src = "";
$url = $_product->getProductUrl();
$data = ' <div class="autocomplete-image"> <img src="' . $imageHelper->init($_product, 'thumbnail')->resize(50, 75) . '"></div> <div class="top">' . $_product->getName() . '</div><li><i class="fa fa-inr" aria-hidden="true"></i><span class="price">' . $_product->getPrice() . '</span></li> ';
$respones[] = array(
'id' => $_product->getId(),
'name' => $_product->getName(),
'data' => $data,
'color' => $_product->getColor(),
'price' => $_product->getPrice(),
'qty' => $_product->getQty()
);
}
echo json_encode($respones);
}
function getAttrOptions($code){
$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute($code);
if ($attr && $attr->usesSource()) {
return $attr->getSource()->getAllOptions();
}
}
?>