1

I am trying to load a product by its custom attribute outside Magento folder, using the code below but no data is coming.

My attribute dummyproductid is a unique attribute for every product.

require_once('../app/Mage.php'); //Path to Magento
umask(0);
Mage::app('default');

...

$product_id = (string) $item_stock->productid;
$load_product = Mage::getModel('catalog/product')->loadByAttribute('dummyproductid', $product_id);
$product_real_sku = $load_product->getId();

echo $product_real_sku;

Anyone can help?

What I am actually trying to do is I have 2 xml files and trying to merge them by their same node value and using Magmi Data Pump import the products, however the first version of code that I come up is working but extremely slow by using multiple foreach so I am trying to first import the products from products.xml and saving the product id into an attribute of dummyproductid and then using this attribute I am trying to update the already imported products stock and price from the second xml called stock.xml.

The products.xml and stock xml have both the smae node called productid, below is my complete code but I coulnd;t get it working the second part which is updating the stocks and price by the atrribute:

<?php
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');
header('Content-type: text/html; charset=utf-8');
setlocale(LC_ALL, 'tr_TR.utf-8');
setlocale(LC_CTYPE, 'C');

class TestLogger
{
    public function log($data, $type)
    {
        echo "$type:$data\n";
    }
}

set_include_path('../magmi' . PATH_SEPARATOR . '../magmi/inc' . PATH_SEPARATOR . '../magmi/integration/inc' . PATH_SEPARATOR . '../magmi/engines');

require_once("magmi_datapump.php");

$dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport");
$dp->beginImportSession("default", "create");

$products = "http://www.localhost.com/products.xml";
$stock = "http://www.localhost.com/stock.xml";

$products_xml = new SimpleXMLElement($products, null, true);
$stock_xml = new SimpleXMLElement($stock, null, true);

require_once('../app/Mage.php'); //Path to Magento
umask(0);
Mage::app('admin');

foreach ($products_xml as $item) {

    $sku = (string) $item->urunkod;
    $check_product_id = Mage::getModel('catalog/product')->getIdBySku($sku);

    if ($check_product_id){
        echo 'Product '.$sku.' has been updated.<br>';
    } else {

        $is_active = (string) $item->active;

        if ($is_active = 1) {

            $product_is_in_stock = 0;
            $product_status = 2;

            $nameNew = html_entity_decode($item->urunadi, ENT_QUOTES | ENT_HTML5, 'UTF-8');
            $nameNew = ltrim($nameNew);
            $nameNew = rtrim($nameNew);

            $product_url = strtr ($nameNew, array ('ı' => 'i', 'ğ' => 'g', 'ü' => 'u', 'ş' => 's', 'ö' => 'o', 'ç' => 'c', 'İ' => 'I', 'Ğ' => 'G', 'Ü' => 'U', 'Ş' => 'S', 'Ö' => 'O', 'Ç' => 'C', ' ' => '-', '\'' => '-', '(' => '-', ')' => '-', '"' => '-', '.' => '-', '&' => '-', ',' => '-', '/' => '-', '\\' => '-', '!' => '-'));
            $product_url = str_replace("---","-",$product_url);
            $product_url = str_replace("--","-",$product_url);
            $product_url = strtolower($product_url);
            if (substr($product_url, -1) == '-') {
                $product_url = substr($product_url, 0, -1);
            }

            $aciklamaNew = nl2br(html_entity_decode($item->bilgi),false);

            $product_brand = (string) $item->marka;
            $product_brand = strtolower($product_brand);
            $product_brand = ucwords($product_brand);
            $product_brand = ltrim($product_brand);
            $product_brand = rtrim($product_brand);

            $resimURLNew = $item->resim;

            $mediagalleryURLs = $resimURLNew;

            $newProductData = array(
                'sku'               => (string) $item->urunkod,
                'dummyproductid'    => (string) $item->urunid,
                'dummycatid'        => (string) $item->kategoriid,
                'qty'               => 0,
                'name'              => (string) $nameNew,
                'manufacturer'      => (string) $product_brand,
                'description'       => (string) $aciklamaNew,
                'price'             => 0,
                'special_price'     => 0,
                'cost'              => 0,
                'tax_class_id'      => 'None',
                'tedarikci'         => 'Odorvi',
                'manufacturer'      => $product_brand,
                'weight'            => 0.4,
                'url_key'           => $product_url,
                'type'              => 'simple',
                'store'             => 'admin',
                'attribute_set'     => 'Kozmetik',
                'visibility'        => 'Catalog, Search',
                'is_in_stock'       => $product_is_in_stock,
                'manage_stock'      => 1,
                'use_config_manage_stock' => 1,
                'status'            => $product_status,
                'categories'        => '[Ana Kategori]/Temp'
            );

            $newProductData['image']       = (string) $resimURLNew;
            $newProductData['small_image'] = (string) $resimURLNew;
            $newProductData['thumbnail']   = (string) $resimURLNew;
            $newProductData['media_gallery']   = (string) $mediagalleryURLs;

            echo '<pre>';
            print_r($newProductData);
            echo '</pre>';

            $dp->ingest($newProductData);

            echo '' . ' mem:'.memory_get_usage() .  " ... Done! <br />\n";

            unset($newProductData);
            die;
        }
    }
}
unset($products_xml);

foreach ($stock_xml as $item_stock) {

    $product_id = (string) $item_stock->productid;
    $load_product = Mage::getModel('catalog/product')->loadByAttribute('dummyproductid', $product_id);
    $product_real_id = $load_product->getId();

    $product_sku = Mage::getResourceModel('catalog/product')->getSku($product_real_id);
    $product_qty = $item_stock->stok;
    $price_retail = ((real) $item_stock->skfiyat);
    $price_cost = ((real) $item_stock->byfiyat);

    $price_retail = str_replace(",",".",$price_retail);
    $price_cost = str_replace(",",".",$price_cost);

    if ($product_qty >= 1 && $price_retail > 0) {
        $product_is_in_stock = 1;
        $product_status = 1;

        //Retail Price with VAT
        $price_retail_with_vat = ($price_retail * 0.18) + $price_retail;
        //Retail Price with VAT round
        $price_retail_with_vat_roundup = ceil($price_retail_with_vat) - 0.01;

        //Cost Price with VAT
        $price_cost_with_vat = ($price_cost * 0.18) + $price_cost;
        //Cost Price with VAT round
        $price_cost_with_vat_round = round($price_cost_with_vat,2);

        //Special Price
        $special_price = ($price_cost * 0.25) + $price_cost;
        //Special Price with VAT
        $special_price_with_vat = ($special_price * 0.18) + $special_price;
        //Special Price with VAT round
        $special_price_with_vat_round = ceil($special_price_with_vat) - 0.01;

        if($price_cost != '0.00') {
            $price_cost_with_vat = round((($price_cost * 0.18) + $price_cost),2);

            $special_price = ($price_cost * 0.25) + $price_cost;
            $special_price_with_vat = round((($special_price * 0.18) + $special_price),2);

            $product_special_from_date = date('Y-m-d');
            $product_special_to_date = date('Y-m-d', strtotime("+30 days"));
        } else {
            $price_cost_with_vat = 0;
        }
    } else {
        $product_is_in_stock = 0;
        $product_status = 2;
        $product_special_from_date = '';
        $product_special_to_date = '';
    }

    $newProductDataUpdateStock = array(
        'sku'               => (string) $product_sku,
        'qty'               => $product_qty,
        'price'             => $price_retail_with_vat_roundup,
        'cost'              => $price_cost_with_vat,
        'special_price'     => $special_price_with_vat_round,
        'special_from_date' => $product_special_from_date,
        'special_to_date'   => $product_special_to_date,
        'is_in_stock'       => $product_is_in_stock,
        'status'            => $product_status,
    );

    echo '<pre>';
    print_r($newProductDataUpdateStock);
    echo '</pre>';

    $dp->ingest($newProductDataUpdateStock);

    echo '' . ' mem:'.memory_get_usage() .  " ... Done! <br />\n";

    unset($newProductDataUpdateStock);
}
unset($stock_xml);

$dp->endImportSession();
?>
  • 1
    Make sure your attribute is set as "Used in product listing" and rebuild your indexes – Marius Nov 4 '15 at 13:33
  • The attribute is set as "Used in product listing" and I did rebuild all the indexes screenshot of the attribute @Marius – tufanayd Nov 4 '15 at 13:42
  • I see...I have no other idea. It should work in theory. I see nothing wrong with your code. – Marius Nov 4 '15 at 13:48
  • just check echo $product_id and see if u get any value in this variable and if yes value coming check in the backend is their any product with dummyproductid eq to this $product_id – Pradeep Sanku Nov 4 '15 at 13:50
  • Yes you are right about the code that should be working in theory however in practical it isnot :( I post the whole code if it helps anyone to help or to use) @Marius – tufanayd Nov 4 '15 at 14:00
1

Lets debug,

  • Check the Mage.php path is proper or not. require_once('../app/Mage.php');
  • You have using frontend store (default). Mage::app('default');.So,as Marius says Make sure your attribute is set as "Used in product listing" and rebuild your indexes.

If you have change store= admin then there are no dependency on attribute setting.

SO change:

Mage::app('default');

to

Mage::app('admin');

to Avoid the error: use:

$load_product = Mage::getModel('catalog/product')->loadByAttribute('dummyproductid', $product_id);
if($load_product->getId()):
$product_real_sku = $load_product->getId();
endif;

instead of

$load_product = Mage::getModel('catalog/product')->loadByAttribute('dummyproductid', $product_id);
$product_real_sku = $load_product->getId();
  • I have tried everything and getting this error: Fatal error: Call to a member function getId() on a non-object @Amit Bera – tufanayd Nov 4 '15 at 13:40
  • that mean magento _is fail to load product by dummyproductid and it value $product_id – Amit Bera Nov 4 '15 at 13:49
  • The attribute is all set correctly and the product has unique dummyproductid value but no chance that the product is loading, I paste my entire code if it helps, @Amit Bera – tufanayd Nov 4 '15 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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