четверг, 18 марта 2010 г.

Magento. Задать и получить Product QTY

Стоит следующая задача:

Задать и получить Product QTY.

Сначала попробовал сделать это через $product->getQty();

Но как оказалось все данные что связанные с Inventory хранятся в другой модели. Поэтому делаем так:

   $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId); 
   $stock->getQty(); // получаем QTY
   $stock->setQty(1); // задаем QTY
   $stock->save(); // Обязательно сохранись, если были внесены изменения

Собственно вот.

среда, 10 марта 2010 г.

Ebay Api. Отсылаем запросы

Все запросы в eBay Api. Отсылаются в виде XML


Приведу пример:
// Указываем ключи, полученные на developers.ebay.com

        $this->devID = 'DEV ID';
        $this->compatabilityLevel = 681;
        $this->appID = 'APP ID';
        $this->certID = 'CERT ID';
        $this->serverUrl = 'https://api.sandbox.ebay.com/ws/api.dll';
        $this->siteUrl = 'http://cgi.sandbox.ebay.com/ws/eBayISAPI.dll?ViewItem&item=';
        $this->globalUserToken ='YOURTOKEN';
        $this->auth_url="https://signin.sandbox.ebay.com/ws/eBayISAPI.dll?SignIn&";  
        $this->Runame="YOUR RUNAME";

        // Функция которая принимает XML - запрос: $requestBody;
        // $type - это тип запроса. "AddItem, GetCategories .........."
        // $siteID - Id магазина с которым собираемся работать
        public function sendHttpRequest($requestBody, $type = '', $siteID = 0)
        {
         $this->siteID = $siteID;
         $this->verb = $type;
         //build eBay headers using variables passed via constructor
                // создаем Xедеры
         $headers = $this->buildEbayHeaders();

         //initialise a CURL session
         $connection = curl_init();
         //set the server we are using (could be Sandbox or Production server)
         curl_setopt($connection, CURLOPT_URL, $this->serverUrl);

         //stop CURL from verifying the peer's certificate
         curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);

         //set the headers using the array of headers
         curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);

         //set method as POST
         curl_setopt($connection, CURLOPT_POST, 1);

  
         //set the XML body of the request
         curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody);

         //set it to return the transfer as a string from curl_exec
          curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);

         //curl_multi_info_read($connection);
         //Send the Request
         $response = curl_exec($connection);
         //close the connection
         curl_close($connection);

         return $response;
        }

        private function buildEbayHeaders()
        {
          $headers = array (
          //Regulates versioning of the XML interface for the API
          'X-EBAY-API-COMPATIBILITY-LEVEL: ' . $this->compatabilityLevel,
   
          //set the keys
          'X-EBAY-API-DEV-NAME: ' . $this->devID,
          'X-EBAY-API-APP-NAME: ' . $this->appID,
          'X-EBAY-API-CERT-NAME: ' . $this->certID,
   
         //the name of the call we are requesting
          'X-EBAY-API-CALL-NAME: ' . $this->verb,   
   
         //SiteID must also be set in the Request's XML
         //SiteID = 0  (US) - UK = 3, Canada = 2, Australia = 15, ....
         //SiteID Indicates the eBay site to associate the call with
         'X-EBAY-API-SITEID: ' . $this->siteID,
         );

         return $headers;
       }


Это методы класса, которые обрабатывают запрос, отсылают его на Ebay и возвращают ответ.

Продолжение следует.