1

I'm a Magento newbee so please forgive me if there is ... a crap in the code ... Sending Ajax request with Brand ID and current state "status = 1 or 0" and would like to update state in table:

public function BrandsAction()
{
    $brand_id = $this->getRequest()->getPost("brand_id");
    $status = $this->getRequest()->getPost("status");
    $_category = Mage::getResourceModel('catalog/category_collection')
            ->addFieldToFilter('name', 'Brands')
            ->getFirstItem();

    $categoryId = $_category->getId();
    $brands = Mage::getModel('catalog/category')->load($categoryId);
    $brand = $brands->load($brand_id);
    foreach($brand as $element){
        echo $element;
        if($element->getId() == $brand_id){
            $element->getData();
            $element->getAttribute('show_brand');
            $element->setData('show_brand',$status);
            $element->save();
        }
    }

I expected it will update value in column 'show_brand' but it actually does nothing just returning data back so I can see in console what values were sent. How to update value for specified "category -> Brand -> Somebrand" ?? Did I miss something ?

//just checking category id
$categoryId = $_category->getId();
//loading selected category
$brands = Mage::getModel('catalog/category')->load($categoryId);
//Loading Brand Data
$brand = $brands->load($brand_id);

isn't it ? Or I misunderstood something ?

Current code:

    public function BrandsAction()
    {
    $brandNameId = $this->getRequest()->getPost("brand_id");
    $status = $this->getRequest()->getPost("status");
    $brandsNameCat = Mage::getModel('catalog/category')->load($brandNameId);
    $brandsNameCat->setShowBrand($status);
    $brandsNameCat->save();
    }

not updating values in table ... what I should check to make sure it willl update values ?

4
  • Can you please tell us what these 3 lines of code does $categoryId = $_category->getId(); $brands = Mage::getModel('catalog/category')->load($categoryId); $brand = $brands->load($brand_id); Apr 28, 2017 at 10:04
  • $brand_id is this your category id ? Apr 28, 2017 at 10:09
  • When you say "Brand ID", is it your category id or id of any other data? or simply do you want to set status of your category called "Brands"? Apr 28, 2017 at 10:19
  • Let me clarify a bit. What I wanted to achieve is: in categories I have Brands which contains all the brands so Brands/BrandName. My $brand_id is a BrandName id that's why first I'm getting Parent category ID by name to load it's content then from "parent" picking a BrandName by id ($brand_id). then I want update status for BrandName
    – Rob D. A.
    Apr 28, 2017 at 10:23

2 Answers 2

1

In that case below code should work

<?php
function BrandsAction()
{
    $brand_id = $this->getRequest()->getPost("brand_id");
    // I assume $brand_id is your brand name category id which is subcategory or brands (Parent category)
    $status = $this->getRequest()->getPost("status");
    $brandsNameCat = Mage::getModel('catalog/category')->load($brand_id);
    $brandsNameCat->setShowBrand($status);
    $brandsNameCat->save();
}
1
  • Not updating values ;(
    – Rob D. A.
    Apr 28, 2017 at 11:14
3

To avoid saving the entire model and improve the performance, you can replace the following code:

$brandsNameCat->save();

With:

$brandsNameCat->saveAttribute($brandsNameCat, 'show_brand');
3
  • Getting error 503 after this: replace the code: $brandsNameCat->save();With: $brandsNameCat->saveAttribute($brandsNameCat, 'show_brand');
    – Rob D. A.
    Apr 28, 2017 at 11:04
  • @RobertA.what's the error message ? Apr 28, 2017 at 11:05
  • "POST http://...../brands/ajax/brands/ 503 (Service unavailable)"
    – Rob D. A.
    Apr 28, 2017 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.