1

Would you have any way to get product SKUs from a category? For example, I have a category with id = 13 and I need to get the SKUs of these products dynamically, so I can do checks that I created in my code.

And I would need to save these SKUs of these products in an array so I can do a check in my code, as I currently "pick up" these SKUs manually by placing them in an array.

Code created in a controller:

public function isCartaoAddAction(){
            $array_de_skus_de_cartoes = array(45,60,80,90,102,103,104,105); //SKUs products
            $isCartaoAdicionado = 0;
            $cart = Mage::getModel('checkout/cart')->getQuote();
            foreach ($cart->getAllItems() as $item) {    
                foreach ($array_de_skus_de_cartoes as $sku) {
                    if($sku == $item->getProduct()->getSku()) {
                        $isCartaoAdicionado = 1;
                        $cartao_id = Mage::getModel("catalog/product")->getIdBySku($sku);
                    }
                }
            }
            if($isCartaoAdicionado == 1) {
                echo json_encode(array('adicionado' => '1','id_produto' => $cartao_id));
            } else {
                echo json_encode(array('adicionado' => '','id_produto' => ''));
            }
        }
3
  • you want all the SKUs that are assigned to category id = 13.. Right? Commented Sep 18, 2017 at 19:05
  • Yes that's right. Commented Sep 18, 2017 at 19:10
  • Please check my answer Commented Sep 18, 2017 at 19:10

1 Answer 1

1

Try Below code to get all SKUs

Update: Instead of Using multiple foreach loop, you can use PHP in_array function.

<?php
public function isCartaoAddAction(){
    $categoryId = 13;
    $collection = Mage::getModel('catalog/category')
                ->load($categoryId)
                ->getProductCollection();
    foreach($collection as $product){
         $array_de_skus_de_cartoes[] = $product->getSku();
    }
    //$array_de_skus_de_cartoes = array(45,60,80,90,102,103,104,105); //SKUs products
    $isCartaoAdicionado = 0;
    $cart = Mage::getModel('checkout/cart')->getQuote();
    foreach ($cart->getAllItems() as $item) {           
        if(in_array($item->getProduct()->getSku(), $array_de_skus_de_cartoes)) {
            $sku = $item->getProduct()->getSku();
            $isCartaoAdicionado = 1;
            $cartao_id = Mage::getModel("catalog/product")->getIdBySku($sku);
        }
    }
    if($isCartaoAdicionado == 1) {
        echo json_encode(array('adicionado' => '1','id_produto' => $cartao_id));
    } else {
        echo json_encode(array('adicionado' => '','id_produto' => ''));
    }
}
?>
8
  • It worked fine, however for my case I'm having a hard time modifying it to my code, since before I took these SKUs manually through an array and I would need to put the SKUs of that category in array to be able to do a check that it contains in my code . Commented Sep 18, 2017 at 19:21
  • basically what is your requirement? what you need to check without adding all the SKUs in array? Commented Sep 18, 2017 at 19:24
  • I updated the question with the code I did, along with the verification. Commented Sep 18, 2017 at 19:28
  • But basically, I'd need to save those SKUs in an array. Commented Sep 18, 2017 at 19:33
  • Check my updated answer. This is what you want or may different requirement. Commented Sep 18, 2017 at 19:36

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.