I'm building a custom search results page, and I have the IDs of all products that have to be included in the results. I want to override the default search criteria, and yes, I can override $this->_searchCriterias, but it does not change the results page itself.
This is my custom code of CatalogSearch/Model/Advanced.php
public function getSearchCriterias()
{
$search = $this->_searchCriterias;
var_dump($search);
$search = array();
if(isset($_GET['productid'])) {
$value = $this->getIdsFromSearchUrl($_GET['productid']);
if(is_array($value)){
foreach($value as $v){
if(is_numeric($v)){
$product = Mage::getModel('catalog/product')->load($v);
var_dump($product->getId());
$search[] = array('name'=>'Name','value'=>$product->getName());
}
}
} else {
if(is_numeric($value)){
$product = Mage::getModel('catalog/product')->load($value);
$search[] = array('name'=>'Name','value'=>$product->getName());
}
}
}
var_dump($search);
$this->_searchCriterias = $search;
return $search;
}
Any help appreceated.
The way I understand is that you want the site to always include some "featured products", which are not necessarily related to the search term.
I would suggest an alternative method instead of tampering with the search engine logic:
Create a block and template for displaying featured products.
Add the block to Search engine result page inside the product_list block (under catalogsearch_result_index)
Modify list.phtml and echo out the child block you just added.
Let me know if this helps.
Related
I’m pretty frustrated with the graphql api of Magento. How is it not possible to receive the values of a custom attribute. I mean it’s easy to make a custom select-option attribute and making it available through graphql but you are only getting the integer value.
What’s the best procedure to following
1. Query the complete attribute to get the value
2. Extending the graphql schema (this involves a developer every time the client changes something)
3. Am I missing some functionality inside the graphql endpoints to Fix this?
From Native GraqpQl we cannot get the expected result.But we can write a custom module with the graphql and achieve it.What my suggestion is get the product detail by sku using _productRepository->get($sku);. This will return the entire product details.So you can get the attribute data by using $attributes = $_product->getAttributes(); Here is my data provider file.
/**
* #params string $sku
* this function return all the product data by product sku
**/
public function getProductBySku($sku)
{
return $this->_productRepository->get($sku);
}
/**
* #params int $id
* this function return all the word of the day by id
**/
public function getAttributesBySku( $sku ){
$_product = $this->getProductBySku($sku);
$attributes = $_product->getAttributes();// All Product Attributes
$attributes_data = [];
$x=0;
foreach ($attributes as $attribute) {
if($attribute->getIsUserDefined()){ // Removed the system product attribute by checking the current attribute is user created
$attributeLabel = $attribute->getFrontend()->getLabel();
$attributeValue = $attribute->getFrontend()->getValue($_product);
if($attribute->getAttributeCode()=="language"){
$attributeLabelAndValue = $attributeLabel." - ".$attributeValue;
$attributes_data[$x]['atr_data'] = $attributeLabelAndValue;
}
}
$x++;
}
return $attributes_data;
}
This code will return the need
$attribute->getFrontend()->getLabel(); this will return the label and
$attribute->getFrontend()->getValue($_product); this will return the value.
To check the entire graphQl query and resolver file kindly view How to get product attribute value, label using GraphQl in Magento 2.3?
Hope this will help you.
You can try creating custom graphql attribute with custom resolver.
$product = $this->_productRepository->getById($value['entity_id']);
$data = array();
foreach ($args['fields'] as $fi) {
$att = $product->getResource()->getAttribute($fi);
if (isset($att) && $att) {
if (in_array(
$att->getFrontendInput(),
['multiselect', 'select']
)) {
$data[$fi . '_label'] = $product->getAttributeText($fi);
}
$data[$fi] = $product->getData($fi);
}
}
return json_encode((object) $data);
which should display all provided attributes with their labels.
"sku": "testProduct",
"fabric": 60,
"work": 65,
"dynamicAttributes": "{
"fabric_label":"Pure Silk",
"fabric":"60",
"work_label":"Tanchoi",
"work":"65"
}",
Check out entire module here
I have an observer action setup, that checks if a product page is loaded. If so, it calls a custom help that deals with all of the GET paremeters. It used to work for over 4 years and have suddenly stopped (the only thing that changed on the 3rd party's side is the naming of those parameters).
Here is the observer action:
public function productView($observer) {
/*#var $block Mage_Core_Block_Abstract*/
$block = $observer->getEvent()->getBlock();
if ($block && $this->getProduct()){
if ($block->getModuleName() == 'Mage_Catalog'){
$productId = $this->getProduct()->getEntityId();
//If params exist - save
if ($this->ParamsHelper()->saveParams($productId)){
//code omitted
}
}
}
}
Here is the helper's action:
public function saveParams($productId) {
if (is_numeric($productId)){
$params = Mage::app()->getRequest()->getParams();
if (!empty($params['image']) && !empty($params['config'])){
//never gets here
return true;
}
}
return false;
}
If I try to var_dump the $params, I get the following array which only includes product_id:
array(1) { ["id"]=> string(3) "664" }
Expected result is to be able to access all the GET parameters passed via url in product view.
Any help or guidance is much appreciated.
EDIT
Product URLs are similar to product names, like f.e.:
domain.com/red-jacket
In a perfect case, I would expect to get parameters passed like following:
domain.com/red-jacket?param1=aaa¶m2=bbb
Not sure whats the exact problem is ... but for domain.com/red-jacket?param1=aaa¶m2=bbb you can get parameters in this way:
$params = Mage::app()->getRequest()->getParams();
$param1 = $params['param1']
or
$param1 = Mage::app()->getRequest()->getParam('param1');
//code for get parameters in view page.
$arrParams = Mage::app()->getRequest()->getParams();
echo "Get Parameter"; echo '<pre>', print_r($arrParams);
I'm trying to make something different with my Magento instance;
I want to increase product's stock (in a certain condition) when I'm selling a product. I've added a custom checkbox in the Magento Admin panel and when it's checked I expect to increase product's quantity.
I'm currently investigating the following method:
Mage_Sales_Model_Service_Quote:submitOrder()
Is it a good place to start looking at? Any tip on this problem?
I think I've achieved what I wanted.
I've added the following lines at Mage_Sales_Model_Service_Quote:submitOrder()
...
// $mySpecialCondition = form.checkbox
foreach ($quote->getAllItems() as $item) {
if($mySpecialCondition){
$item->setQty(0 - $item->getQty(), true);
}
...
and also changed the method Mage_Sales_Model_Quote_Item:_prepareQty to accept negative values:
protected function _prepareQty($qty, $ignoreValue = false)
{
$qty = Mage::app()->getLocale()->getNumber($qty);
if(!$ignoreValue){
$qty = ($qty > 0) ? $qty : 1;
}
return $qty;
}
Thanks anyway :)
I'm sorry if this question is trivial but I've been struggling to find what I am doing wrong here. I am trying to change the value of an attribute on a store view level but the default is also changed whereas it shouldn't be. Of course, this attribute is set up to be "store-view-scoped". To keep it simple, I've tried with the product name. No success.
Below are unsuccessful tests I've tried...
Do you see what I am doing wrong here?
Many thanks.
My tries :
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setStore(STORE_CODE)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_CODE)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();
I even tried by adding the line below before the product model load...
Mage::app()->setCurrentStore(STORE_ID);
So here is the complete snippet to change attribute value for a specific product attribute on a specific store view. Example with the product name :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('my_new_product_name')->save();
And as an additional answer, one could be interested in changing the attribute value to the default one. In this case, the argument 'false' must be passed to the setAttribute :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName(false)->save();
You need to set the current store to admin at the top of your code block:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
note when loading product with data for some store view, also default values get loaded. saving such product will save default values as store values (thus unset "Use Default Value" for fields)
i ended up with following function to clean-up product data from default values
public static function _removeDefaults($item) {
static $attributeCodes = null;
if($attributeCodes == null) {
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
$attributeCodes = array();
foreach($attributes as $attribute) {
$ac = $attribute->getAttributeCode();
if(in_array($ac, array('sku','has_options','required_options','created_at','updated_at','category_ids'))) {
continue;
}
$attributeCodes[] = $ac;
}
}
foreach($attributeCodes as $ac) {
if(false == $item->getExistsStoreValueFlag($ac)) {
$item->unsetData($ac);
}
}
}
remember to send only product loaded for some store view
I need to display 3 or so new products automatically on the Magento home-page. This means the administrator should NOT have to mark the product as new using the 'New from Date' and 'New to Date' attributes in the 'General' tab. I know how to create a separate category containing new products automatically (http://www.tridian.com/developer-blog/adding-new-arrivals-to-magento/), but how do I display them on the home page?
You have to set a new model for such system attribute.
First of all edit your *eav_attribute* table in magento mysql database
Go edit "news_to_date" record and set backend model
from "eav/entity_attribute_backend_datetime" to "catalog/product_attribute_backend_newsto"
Now go to Core/Catalog/Model/Product/Attribute/Backend and create a new file "Newsto.php"
write this code in this file than save
class Mage_Catalog_Model_Product_Attribute_Backend_Newsto extends Mage_Eav_Model_Entity_Attribute_Backend_Datetime
{
public function beforeSave($object)
{
$attributeName = $this->getAttribute()->getName();
$startDate = $object->getData('news_from_date');
$toDate = $object->getData($attributeName);
if ($toDate === false) {
return $this;
}
if ($toDate == '' && $startDate != '') {
$newdate = strtotime($startDate);
$toDate = date("d/m/Y",strtotime("+7 days",$newdate));
}
$object->setData($attributeName, $toDate);
parent::beforeSave($object);
return $this;
}
}
I guess you can if you build a custom layout for your homepage and choose to display stuff there not based on new criteria but based on a given category. Never done this so its just a suggestion to research in this direction.