如何使用Magento SOAP将文件附件上传到自定义产品属性


8

这与我的问题有关;如何获得产品文件附件

我想制作一个可以Intellimage_Attachs在其他问题中使用该模块的脚本(不在服务器上)。

如果可能的话,我将使用Magento SOAP。

我目前遇到的问题是我无法获取/放置/更新/我们的产品“样本/文件”。

return $this->handle->call($this->session,'product_custom_option.list', "productnamehere ");

带回一个产品,我有:(附件的空白阵!显然用错了SOAP调用,其中一个是正确的?(如product_custom_option.list不正确,我的功能正常工作与其他呼叫。)

更新

尝试过:

return $this->handle->call($this->session, 'product_downloadable_link.list', array( $sku . " "));

但是,即使这些产品使用,它们也不是可下载的产品,因此无法使用samples


唯一可下载产品上的问题?
阿卜杜勒

认为您误会了,所有产品都不是可下载的产品。所有物理产品,但无法通过肥皂获得示例下载。
罗伯特·庞德

amasty.com/product-attachments.html将做您需要的一切,并提供API访问权限。
2015年

感谢您提供信息,但希望自己对它进行排序而不是购买扩展名,并且文件已经集成,我要整理自动化的原因是因为如前所述,通过magento管理员更新文件很痛苦我有SQL表的解决方案,因此在购买扩展之前会这样做
Robert Pounder

Answers:


2

在您的情况下,您需要实现自定义SOAP API端点。幸运的是,您可能会大量重用可下载产品API实现。

如果创建下面列出的所有文件,那么将提供新的SOAP V2 API:catalogProductAttachLinkList。要启用添加/删除方法,只需将它们从app / code / core / Mage / Downloadable / Model / Link / Api.php移植app / code / community / Intellimage / Attachs / Model / Link / Api.php即可

要测试新的API,请运行以下任一操作:

<?php
/* SOAP V2 Style */
$client = new SoapClient('http://simple-magento-vagrant.dev/index.php/api/v2_soap/?wsdl');
$sessionId = $client->login('apiUser', 'apiKey');
$productId = 1;
$result = $client->catalogProductAttachLinkList($sessionId, $productId);
print_r($result);

/* SOAP V1 style. If you want to use this style, you may skip creation of custom wsdl.xml and Api/V2.php files proposed below. Adding api.xml and Api.php will be enough */
$client = new SoapClient('http://simple-magento-vagrant.dev/index.php/api/soap/?wsdl');
$sessionId = $client->login('apiUser', 'apiKey');
$productId = 1;
$result = $client->call($sessionId, 'attach_link.list', [$productId]);
print_r($result);

要添加到模块的文件:

app / code / community / Intellimage / Attachs / etc / api.xml

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <catalog_product_attach_link translate="title" module="intellimage_attachs">
                <model>attachs/link_api</model>
                <title>Category API</title>
                <acl>downloadable/link</acl>
                <methods>
                    <list translate="title" module="intellimage_attachs">
                        <title>Retrieve links and samples list from attach product</title>
                        <method>items</method>
                        <acl>downloadable/link/list</acl>
                    </list>
                </methods>
            </catalog_product_attach_link>
        </resources>
        <resources_alias>
            <attach_link>catalog_product_attach_link</attach_link>
        </resources_alias>
        <v2>
            <resources_function_prefix>
                <attach_link>catalogProductAttachLink</attach_link>
            </resources_function_prefix>
        </v2>
    </api>
</config>

app / code / community / Intellimage / Attachs / etc / wsdl.xml (请注意,如果需要与SOAP V2 WS-I兼容,则应创建wsi.xml

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <catalog_product_attach_link translate="title" module="intellimage_attachs">
                <model>attachs/link_api</model>
                <title>Category API</title>
                <acl>downloadable/link</acl>
                <methods>
                    <list translate="title" module="intellimage_attachs">
                        <title>Retrieve links and samples list from attach product</title>
                        <method>items</method>
                        <acl>downloadable/link/list</acl>
                    </list>
                </methods>
            </catalog_product_attach_link>
        </resources>
        <resources_alias>
            <attach_link>catalog_product_attach_link</attach_link>
        </resources_alias>
        <v2>
            <resources_function_prefix>
                <attach_link>catalogProductAttachLink</attach_link>
            </resources_function_prefix>
        </v2>
    </api>
</config>

应用/代码/社区/智能/附件/模型/链接/Api/V2.php

<?php

class Intellimage_Attachs_Model_Link_Api_V2 extends Intellimage_Attachs_Model_Link_Api
{
    protected function _prepareData(&$var)
    {
        if (is_object($var)) {
            $var = get_object_vars($var);
            foreach ($var as $key => &$value) {
                $this->_prepareData($value);
            }
        }
    }

    public function add($productId, $resource, $resourceType, $store = null, $identifierType = null)
    {
        $this->_prepareData($resource);
        return parent::add($productId, $resource, $resourceType, $store, $identifierType);
    }
}

应用/代码/社区/ Intellimage /附件/模型/链接/Api.php

   <?php
    class Intellimage_Attachs_Model_Link_Api extends Mage_Catalog_Model_Api_Resource
    {
        public function items($productId, $store = null, $identifierType = null)
        {
            $product = parent::_getProduct($productId, $store, $identifierType);
            $typeInstance = $product->getTypeInstance(true);
            $product->setTypeInstance(Mage::getModel('attachs/product_type', $typeInstance), true);

            $linkArr = array();
            $links = $product->getTypeInstance(true)->getSamples($product);
            $downloadHelper = Mage::helper('downloadable');
            foreach ($links as $item) {
                $tmpLinkItem = array(
                    'link_id' => $item->getId(),
                    'title' => $item->getTitle(),
                    'price' => $item->getPrice(),
                    'number_of_downloads' => $item->getNumberOfDownloads(),
                    'is_shareable' => $item->getIsShareable(),
                    'link_url' => $item->getLinkUrl(),
                    'link_type' => $item->getLinkType(),
                    'sample_file' => $item->getSampleFile(),
                    'sample_url' => $item->getSampleUrl(),
                    'sample_type' => $item->getSampleType(),
                    'sort_order' => $item->getSortOrder()
                );
                $file = Mage::helper('downloadable/file')->getFilePath(
                    Mage_Downloadable_Model_Link::getBasePath(), $item->getLinkFile()
                );

                if ($item->getLinkFile() && !is_file($file)) {
                    Mage::helper('core/file_storage_database')->saveFileToFilesystem($file);
                }

                if ($item->getLinkFile() && is_file($file)) {
                    $name = Mage::helper('downloadable/file')->getFileFromPathFile($item->getLinkFile());
                    $tmpLinkItem['file_save'] = array(
                        array(
                            'file' => $item->getLinkFile(),
                            'name' => $name,
                            'size' => filesize($file),
                            'status' => 'old'
                        ));
                }
                $sampleFile = Mage::helper('downloadable/file')->getFilePath(
                    Mage_Downloadable_Model_Link::getBaseSamplePath(), $item->getSampleFile()
                );
                if ($item->getSampleFile() && is_file($sampleFile)) {
                    $tmpLinkItem['sample_file_save'] = array(
                        array(
                            'file' => $item->getSampleFile(),
                            'name' => Mage::helper('downloadable/file')->getFileFromPathFile($item->getSampleFile()),
                            'size' => filesize($sampleFile),
                            'status' => 'old'
                        ));
                }
                if ($item->getNumberOfDownloads() == '0') {
                    $tmpLinkItem['is_unlimited'] = 1;
                }
                if ($product->getStoreId() && $item->getStoreTitle()) {
                    $tmpLinkItem['store_title'] = $item->getStoreTitle();
                }
                if ($product->getStoreId() && $downloadHelper->getIsPriceWebsiteScope()) {
                    $tmpLinkItem['website_price'] = $item->getWebsitePrice();
                }
                $linkArr[] = $tmpLinkItem;
            }
            unset($item);
            unset($tmpLinkItem);
            unset($links);

            $samples = $product->getTypeInstance(true)->getSamples($product)->getData();
            return array('links' => $linkArr, 'samples' => $samples);
        }
    }

不错的一个,一旦经过测试,就会接受并有赏金,现在也许明天会开始,尽管轮回开始了!
罗伯特·庞德

当然,如果您对此有任何疑问,请告诉我们。另外,请在问题中添加“ api”和“ soap”标签。
Alex Paliarush 2015年

0

好的,所以我想出了自己的答案,但实际上希望他们是这样做的一种更整洁的方法,尽管这种方法出奇的容易实现。

使用的2个SQL表是: downloadable_sample downloadable_sample_title

但是我不希望直接访问magento sql,希望有一种内置的方法。


0

请尝试使用以下代码使用Magento SOAP API V2上传文件附件

try {
    $client = new SoapClient('http://magentohost/api/v2_soap/?wsdl'); // api url
    $sessionId = $client->login('test123', 'test123'); // API user name & key 
    $resource = array(
        'title' => 'link_2',
        'price' => '11.99',
        'type' => 'file',
        'file' => array(
            'name' => 'file_test',
            'base64_content' => '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAXABcDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDLooor8XP4DCiiigAooooAKKKKAP/Z'
        )
    );
    $resourceType = 'link';
    $productId =  '4607';
    $result = $client->catalogProductDownloadableLinkAdd($sessionId, $productId, $resource, $resourceType);
}
catch (Exception $e) {
   echo $e->getMessage();
}
print_r($result);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.