8

This is in relation to my question; how-to-get-a-products-file-attachments

I'd like to make a script (not on the server) that can utilize the Intellimage_Attachs module in my other question.

I am going to use Magento SOAP if possible.

The problem I'm having at the moment is I can't get/put/update/w.e the product "samples/files".

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

Brings back a blank array on a product I have attachments on :(! Obviously using the wrong soap call, which one would be the correct one? (as in product_custom_option.list is incorrect, my function works fine with other calls.)

UPDATE

tried:

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

But won't work as the products are not downloadable products, even though they use samples.

  • issues on the only downloadable products? – Abdul Nov 9 '15 at 10:23
  • Think you are misunderstanding, none of the products are downloadable products. All physical products but can't get the sample downloads for them via soap. – Robert Pounder Nov 9 '15 at 10:43
  • amasty.com/product-attachments.html Will do everything you need, and gives API access. – B00MER Nov 10 '15 at 9:55
  • thanks for info but would prefer to sort it myself rather than buy an extension and the files are already integrated, the reason I'm trying to sort out automation is because it's just a pain to go through magento admin to update files, as previously mentioned I have a solution with the SQL tables so I'd do that before buying an extension – Robert Pounder Nov 10 '15 at 10:17
2
+100

In your case you need to implement custom SOAP API endpoint. Luckily you may heavily reuse Downloadable Product API implementation.

If you create all files listed below, you will have new SOAP V2 API available: catalogProductAttachLinkList. To enable add/remove methods, just port them from app/code/core/Mage/Downloadable/Model/Link/Api.php to app/code/community/Intellimage/Attachs/Model/Link/Api.php.

To test new API run one of the following:

<?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);

Files to be added to your module:

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 (Please note, that wsi.xml should be created if SOAP V2 WS-I compatibility is necessary)

<?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/Model/Link/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);
    }
}

app/code/community/Intellimage/Attachs/Model/Link/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);
        }
    }
  • nice one, will accept and bounty once tested, will probably be tomorrow now though bout to clock off! – Robert Pounder Nov 11 '15 at 15:17
  • Sure, let me know if you have any questions regarding this. Also, please add 'api' and 'soap' tags to the question. – Alex Paliarush Nov 11 '15 at 15:19
0

Ok so I have come up with my own answer, but really hoping their is a neater way of doing this, although this way is surprisingly easily implemented;

The 2 SQL tables used are; downloadable_sample downloadable_sample_title

However I'd prefer not to directly access magento sql and was hoping there would be a built in way of doing in.

0

Please try bellow code for the upload a file attachment using 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);

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.