Lý lịch
Chỉ có thể sử dụng API ngoài luồng; tài liệu có vẻ khá rõ ràng về điều này vì nó tài liệu không có cách nào để thiết lập các lựa chọn, các tùy chọn cho một gói
Ngoài ra, việc thiếu tệp api.xml trong Mage_Bundlegói đang nói rằng không có hỗ trợ. Ngược lại với một loại sản phẩm khác xuất hiện sau 1.0, chẳng hạn như Tải xuống và rõ ràng việc thiếu hỗ trợ dường như là cố ý.

Vì vậy, tại sao họ cố tình bỏ qua hỗ trợ ở đây? Nhóm nòng cốt có lẽ có thể kêu vang đến cuối cùng. Tôi đoán là số lượng tùy chọn có sẵn và độ phức tạp xung quanh việc thiết kế một API như thế này chỉ không xuất hiện trong các tính toán chi phí / lợi ích.
Vậy, wut làm gì?
Như Oleksii đã chỉ ra, bạn sẽ phải mở rộng API Magento để cho phép điều này xảy ra.
Một tập lệnh độc lập barebones tạo ra một lựa chọn / tùy chọn gói sẽ trông giống như thế này:
<?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();
Vì vậy, nhiều nhất chúng ta sẽ phải làm là cung cấp giao diện API cho các tùy chọn bắt buộc. Tạo một mô-đun mới với một mô hình mở rộng mô hình trừu tượng API Magento và một phương thức lấy $itemsvà $selectionRawDatalàm tham số:
<?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();
}
}
Hãy nhớ rằng $itemsvà $selectionRawDatalà các mảng (ví dụ được liệt kê ở trên trong khối mã tập lệnh độc lập).
Bây giờ, tạo một tệp api.xml trong etcthư mục mô-đun của bạn với các nội dung sau:
<?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>
Đó là nó.
Và để gọi API mới, chúng tôi sử dụng:
$proxy->call($sessionId, 'bundle_link.createSelectionLink', array($items, $selectionRawData, $productId, $storeid));
Hãy cẩn thận / điều kiện tiên quyết
- Bạn phải tạo một sản phẩm gói vỏ
- Bạn phải có một sản phẩm đơn giản để liên kết vào lựa chọn
- Bạn cần hiểu định dạng của các tham số mảng API nếu không
savecuộc gọi sẽ bị nghẹt, ném ngoại lệ
- Điều này chỉ sử dụng một
radioloại nút - có những người khác: dropdown, checkboxví dụ.
- Các
$itemsmảng tạo ra một mục bó giá cố định. Điều này có thể bị bỏ qua hoặc có thể bị nghẹt cho một gói giá động.
- Đoạn mã trên chỉ là một ví dụ và được rút ngắn vì lợi ích của sự ngắn gọn; nếu bạn đang xây dựng điều này, bạn muốn mã hóa nó trở nên đầy đủ tính năng / khả năng phục hồi hơn để không khiến các nhà phát triển của bạn phát điên khi cố gắng tạo giao diện tạo ra các sản phẩm.
Người giới thiệu:
http://kavinduthundeniya.blogspot.de/2012/11/magento-extention-api-for-bundle.html
/programming/3108775/programmatically-add-bundle-products-in-magento-USE-the-sku-id-of-simple-it