透過 SOAP API 匯入產品

edited 六月 2014 in Magento
$proxy = new SoapClient('http://magento.url/index.php/api/v2_soap/?wsdl');
        $sessionId = $proxy->login('id', 'pwd');

// 取得產品屬性集合
        $attributeSets = $proxy->catalogProductAttributeSetList($sessionId);
        $attributeSet = current($attributeSets);

// 取得目標網站的分類
        $result = $proxy->catalogCategoryLevel($sessionId, 1, 1, 3);

        foreach ($products AS $product) {
//檢查分類是否已經存在
            if (!isset($categories[$product['category_name']])) {
                // 3 = Parent category ID
                $result = $proxy->catalogCategoryCreate($sessionId, 3, array(
                    'name' => $product['category_name'],
                    'is_active' => 1,
                    'position' => 1,
                    'include_in_menu' => 1,
                ));
                $categories[$product['category_name']] = new stdClass();
                $categories[$product['category_name']]->category_id = $result;
            }

//建立產品
            $productId = $proxy->catalogProductCreate($sessionId, 'simple', $attributeSet->set_id, $product['model'], array(
                'categories' => array($categories[$product['category_name']]->category_id),
                'websites' => array(1),
                'name' => $product['category_name'] . ' ' . $product['model'],
                'description' => $product['description'],
                'short_description' => $product['feature'],
                'weight' => '10',
                'status' => '1',
                'url_key' => $product['model'],
                'visibility' => '4',
                'price' => $product['price'],
            ));

            $file = array(
                'content' => base64_encode(file_get_contents($product['major_picture'])),
                'mime' => 'image/jpeg'
            );

            $pPosition = 0;

//上傳主要圖片
            $proxy->catalogProductAttributeMediaCreate(
                    $sessionId, $productId, array(
                'file' => $file,
                'label' => $product['model'],
                'position' => ++$pPosition,
                'types' => array('thumbnail', 'small_image', 'image'),
                'exclude' => 0)
            );

//上傳其他產品圖片
            foreach ($product['pictures'] AS $picture) {
                if($picture === $product['major_picture']) continue;
                $file = array(
                    'content' => base64_encode(file_get_contents($picture)),
                    'mime' => 'image/jpeg'
                );

                $proxy->catalogProductAttributeMediaCreate(
                        $sessionId, $productId, array(
                    'file' => $file,
                    'label' => $product['model'],
                    'position' => ++$pPosition,
                    'types' => array(),
                    'exclude' => 0)
                );
            }
        }

基本上就是把手邊資料整理好之後透過程式批次處理,上面只做簡單的備註,有需要就花點時間踹踹吧 ;)

線上文件雖然說明不是很清楚,但是都有附範例,所以還算輕鬆愜意吧

http://www.magentocommerce.com/api/soap/introduction.html
Sign In or Register to comment.