APIを使用して、バンドルに製品を追加しますか?
-
22-10-2019 - |
質問
既存のDjangoアプリケーションをMagentoストアと統合するシステムを開発しています。私は管理しました、使用しています これ バンドルタイプの製品を作成するライブラリ、および予想どおりの属性とカテゴリ。問題は、APIを使用して方法を見つけることができないことです(前述のライブラリを介して、または電話をかける xmlrpc
メソッド直接)、以前に作成されたバンドルに製品を追加します。電話をかける catalog_product.info
メソッドはバンドルされたオブジェクトについて役立つものを返しません。
オンラインで調査すると、次のいずれかを含むソリューションのみが見つかりました。
- Magentoコードベースを使用するPHPスクリプト
- まっすぐな節から挿入
現在、これらのどちらも私にとってはオプションではありません。API(RESTまたはSOAP)だけを使用してこれを行うことができないと思います。
解決
バックグラウンド
そうです ありえない ボックス外のAPIのみを使用します。ドキュメントは、選択を確立する方法、バンドルのオプションを確立する方法をドキュメントしているため、これについてはかなり明確に思えます。
それとは別に、anの欠如 api.xml のファイル Mage_Bundle
パッケージは、サポートがないことを示しています。それは、ダウンロード可能など、1.0以降に到着した別の製品タイプとは対照的であり、サポートの欠如が意図的であるように見えることは明らかです。
だから、なぜそうなるのでしょう 意図的に ここでサポートを省略しますか?コアチームはおそらくそのためにチャイムを鳴らすことができます。私の推測では、このようなAPIを設計することで利用可能なオプションと複雑さの数は、コスト/利益計算では潜りませんでした。
それで、wut?
Oleksiiがすでに指摘したように、これが起こることを許可するためにMagento APIを拡張する必要があります。
バンドル選択/オプションを作成するベアボーンスタンドアロンスクリプトは、次のようになります。
<?php
require('app/Mage.php');
Mage::app();
$items[] = array(
'title' => 'test title',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => 1,
'position' => 0
);
$selections = array();
$selectionRawData[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '159',
'delete' => '',
'selection_price_value' => '10',
'selection_price_type' => 0,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0
);
$selections[] = $selectionRawData;
$product = Mage::getModel('catalog/product')->setStoreId(0);
$product->load(182);
if (!$product) {
//bail
throw new Exception('Product loaded does not exist');
}
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
$product->setBundleOptionsData($items);
$product->setBundleSelectionsData($selections);
$product->setCanSaveCustomOptions(true);
$product->setCanSaveBundleSelections(true);
$product->save();
したがって、せいぜい、私たちがしなければならないことは、必要なオプションにAPIインターフェイスを提供することです。 Magento API抽象モデルを拡張するモデルと、 $items
と $selectionRawData
パラメーターとして:
<?php
class YourCompany_YourModule_Model_Api extends Mage_Api_Model_Resource_Abstract
{
public function createSelectionLink($items, $selectionRawData, $productId, $storeid)
{
$selections = array();
//check if product id in selection data is valid
$optionProduct = Mage::getModel('catalog/product')->load($selectionRawData['product_id']);
if(!$optionProduct->getId()){
throw new Exception('Selection product provided does not reference a valid product');
}
$selections[] = $selectionRawData;
$product = Mage::getModel('catalog/product')->setStoreId($storeid);
$product->load($productId);
if (!$product->getId()) {
//bail
throw new Exception('Product loaded does not exist');
}
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
$product->setBundleOptionsData($items);
$product->setBundleSelectionsData($selections);
$product->setCanSaveCustomOptions(true);
$product->setCanSaveBundleSelections(true);
$product->save();
}
}
それを覚えておいてください $items
と $selectionRawData
アレイです(例は、上記のスタンドアロンスクリプトコードブロックに記載されています)。
今、作成します api.xml あなたのファイル etc
次の内容を持つモジュールのディレクトリ:
<?xml version="1.0"?>
<config>
<api>
<resources>
<bundle_link translate="title" module="yourcompany_bundleapi">
<title>Bundle creation extension</title>
<model>bundleapi/api</model>
<methods>
<createSelectionLink translate="title" module="yourcompany_bundleapi">
<title>Create a selection link</title>
</createSelectionLink>
</methods>
</bundle_link>
</resources>
</api>
</config>
それでおしまい。
そして、使用する新しいAPIを呼び出すには:
$proxy->call($sessionId, 'bundle_link.createSelectionLink', array($items, $selectionRawData, $productId, $storeid));
警告/前提条件
- あなたはすでにシェルバンドル製品を作成している必要があります
- 選択にリンクするには、すでにシンプルな製品が必要です
- API配列パラメーターの形式または
save
コールはチョーク、例外を投げます - これはaのみを使用します
radio
ボタンタイプ - 他にあります:dropdown
,checkbox
例えば。 -
$items
配列は、固定価格のバンドルアイテムを作成します。これは無視されるか、動的な価格バンドルを求めて窒息する場合があります。 - 上記のコードは例のみであり、Brevityのために短縮されました。これを構築している場合は、製品を作成するインターフェイスを作成しようとするときに開発者を狂わせないように、より完全に機能/弾力性のあるものにするようにコーディングしたいと思うでしょう。
参考文献:
http://kavindutundeniya.blogspot.de/2012/11/magento-extention-api-for-bundle.html