Jump to content

Find product by sku and update quantity stock


Totti

Recommended Posts

Maybe this snippet could help, it is part of module I built but some variables are changed 

and some code removed.

					if ($products = Product::searchByName((int)$this->context->language->id, $sku))
					{
						foreach ($products as &$product)						
						{
							$productObj = new Product((int)$id_product, false, (int)$this->context->language->id);
							$attributes = $productObj->getProductAttributesIds((int)$id_product, true);

							if (isset($attributes) && $attributes) {
								foreach ($attributes as $attribute){
									$combinations = $productObj->getAttributeCombinationsById($attribute['id_product_attribute'], $this->context->language->id);
									foreach ($combinations as $key => $combination)	
										if($sku == $combination['reference'])
										{
												StockAvailable::setQuantity((int)$id_product, $attribute['id_product_attribute'], (int) $stock_qty);
										}
								}

							}
							else {
								if(isset($productObj) && $productObj){
									StockAvailable::setQuantity((int)$id_product, 0,(int) $stock_qty);				
								}
							}
							

						}
					}
  • Like 1
Link to comment
Share on other sites

  • 1 month later...

 

Maybe this snippet could help, it is part of module I built but some variables are changed 

and some code removed.

					if ($products = Product::searchByName((int)$this->context->language->id, $sku))
					{
						foreach ($products as &$product)						
						{
							$productObj = new Product((int)$id_product, false, (int)$this->context->language->id);
							$attributes = $productObj->getProductAttributesIds((int)$id_product, true);

							if (isset($attributes) && $attributes) {
								foreach ($attributes as $attribute){
									$combinations = $productObj->getAttributeCombinationsById($attribute['id_product_attribute'], $this->context->language->id);
									foreach ($combinations as $key => $combination)	
										if($sku == $combination['reference'])
										{
												StockAvailable::setQuantity((int)$id_product, $attribute['id_product_attribute'], (int) $stock_qty);
										}
								}

							}
							else {
								if(isset($productObj) && $productObj){
									StockAvailable::setQuantity((int)$id_product, 0,(int) $stock_qty);				
								}
							}
							

						}
					}
<?php

error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", '1');
ini_set('display_startup_errors', 1);

require_once($_SERVER['DOCUMENT_ROOT']."/config/config.inc.php");

try{
	
	$sku="xxxxx";
	$products = Product::searchByName(6, $sku);	
	var_dump($products);
		
	
}catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}


?>

Fatal error.

 

if active debug:

Array
(
    [0] => Array
        (
            [file] => /var/www/vhosts/xxxxxx.com/httpdocs/classes/Product.php
            [line] => 2814
            [function] => displayError
            [class] => ToolsCore
            [type] => ::
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /var/www/vhosts/xxxxxx.com/httpdocs/classes/Product.php
            [line] => 3794
            [function] => getPriceStatic
            [class] => ProductCore
            [type] => ::
            [args] => Array
                (
                    [0] => 1685
                    [1] => 1
                    [2] => 
                    [3] => 2
                )

        )

    [2] => Array
        (
            [file] => /var/www/vhosts/xxxxx.com/httpdocs/xxxx/xx_xxx_xx.php
            [line] => 12
            [function] => searchByName
            [class] => ProductCore
            [type] => ::
            [args] => Array
                (
                    [0] => 6
                    [1] => xxxx
                )

        )

)
Link to comment
Share on other sites

Ok, if you do not need prices and you just wanted stock create this override of product class

 

override/classes/Product.php

<?php

class Product extends ProductCore
{
	public static function searchByNameMod($id_lang, $query, Context $context = null)
    {
        if (!$context) {
            $context = Context::getContext();
        }

        $sql = new DbQuery();
        $sql->select('p.`id_product`, pl.`name`, p.`ean13`, p.`upc`, p.`active`, p.`reference`, m.`name` AS manufacturer_name, stock.`quantity`, product_shop.advanced_stock_management, p.`customizable`');
        $sql->from('product', 'p');
        $sql->join(Shop::addSqlAssociation('product', 'p'));
        $sql->leftJoin('product_lang', 'pl', '
			p.`id_product` = pl.`id_product`
			AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl')
        );
        $sql->leftJoin('manufacturer', 'm', 'm.`id_manufacturer` = p.`id_manufacturer`');

        $where = 'pl.`name` LIKE \'%'.pSQL($query).'%\'
		OR p.`ean13` LIKE \'%'.pSQL($query).'%\'
		OR p.`upc` LIKE \'%'.pSQL($query).'%\'
		OR p.`reference` LIKE \'%'.pSQL($query).'%\'
		OR p.`supplier_reference` LIKE \'%'.pSQL($query).'%\'
		OR EXISTS(SELECT * FROM `'._DB_PREFIX_.'product_supplier` sp WHERE sp.`id_product` = p.`id_product` AND `product_supplier_reference` LIKE \'%'.pSQL($query).'%\')';

        $sql->orderBy('pl.`name` ASC');

        if (Combination::isFeatureActive()) {
            $where .= ' OR EXISTS(SELECT * FROM `'._DB_PREFIX_.'product_attribute` `pa` WHERE pa.`id_product` = p.`id_product` AND (pa.`reference` LIKE \'%'.pSQL($query).'%\'
			OR pa.`supplier_reference` LIKE \'%'.pSQL($query).'%\'
			OR pa.`ean13` LIKE \'%'.pSQL($query).'%\'
			OR pa.`upc` LIKE \'%'.pSQL($query).'%\'))';
        }
        $sql->where($where);
        $sql->join(Product::sqlStock('p', 0));

        $result = Db::getInstance()->executeS($sql);

        if (!$result) {
            return false;
        }

        $results_array = array();
        foreach ($result as $row) {
            //$row['price_tax_incl'] = Product::getPriceStatic($row['id_product'], true, null, 2);
            //$row['price_tax_excl'] = Product::getPriceStatic($row['id_product'], false, null, 2);
            $results_array[] = $row;
        }
        return $results_array;
    }
}

Then use it in your code

<?php

error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", '1');
ini_set('display_startup_errors', 1);

require_once($_SERVER['DOCUMENT_ROOT']."/config/config.inc.php");

try{
	
	$sku="xxxxx";
	$products = Product::searchByNameMod(6, $sku);	
	var_dump($products);
		
	
}catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}


?>

Clear cache/delete file cache/class_index.php.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

If I enable a module I do not receive this error only:

 

    [0] => Array
        (
            [file] => /var/www/vhosts/xxxxx.com/httpdocs/classes/Product.php
            [line] => 2814
            [function] => displayError
            [class] => ToolsCore
            [type] => ::
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /var/www/vhosts/xxxxxx.com/httpdocs/classes/Product.php
            [line] => 499
            [function] => getPriceStatic
            [class] => ProductCore
            [type] => ::
            [args] => Array
                (
                    [0] => 1590
                    [1] =>
                    [2] =>
                    [3] => 6
                    [4] =>
                    [5] =>
                    [6] => 1
                    [7] => 1
                    [8] =>
                    [9] =>
                    [10] =>
                    [11] =>
                    [12] => 0
                )

        )

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...