如何獲得與可配置產品相關的所有簡單產品?我發現如何做相反的事情(從簡單的產品獲得產品配置),但這不是我需要的。從Magento中的可配置產品中獲取所有簡單產品產品視圖
我想顯示我有多少單位的股票對於選定的產品(可配置屬性)。我最初的想法是打印所有數量的庫存並用jQuery控制顯示。任何想法?
如何獲得與可配置產品相關的所有簡單產品?我發現如何做相反的事情(從簡單的產品獲得產品配置),但這不是我需要的。從Magento中的可配置產品中獲取所有簡單產品產品視圖
我想顯示我有多少單位的股票對於選定的產品(可配置屬性)。我最初的想法是打印所有數量的庫存並用jQuery控制顯示。任何想法?
使用下面的腳本在
app/design/frontend/default/[your theme]/template/catalog/product/view/type/options/configurable.phtml
裏面的腳本:
spConfig.getIdOfSelectedProduct = function() {
var existingProducts = new Object();
for (var i = this.settings.length - 1; i >= 0; i--) {
var selected = this.settings[i].options[this.settings[i].selectedIndex];
if (selected.config) {
for (var iproducts = 0; iproducts < selected.config.products.length; iproducts++) {
var usedAsKey = selected.config.products[iproducts] + "";
if (existingProducts[usedAsKey] == undefined) {
existingProducts[usedAsKey] = 1;
} else {
existingProducts[usedAsKey] = existingProducts[usedAsKey] + 1;
}
}
}
}
for (var keyValue in existingProducts) {
for (var keyValueInner in existingProducts) {
if (Number(existingProducts[keyValueInner]) < Number(existingProducts[keyValue])) {
delete existingProducts[keyValueInner];
}
}
}
var sizeOfExistingProducts = 0;
var currentSimpleProductId = "";
for (var keyValue in existingProducts) {
currentSimpleProductId = keyValue;
sizeOfExistingProducts = sizeOfExistingProducts + 1
}
if (sizeOfExistingProducts == 1) {
alert("Selected product is: " + currentSimpleProductId)
}
}
現在增加onchange
事件在同一頁面的下拉菜單:
onchange = "spConfig.getIdOfSelectedProduct()"
使用此代碼如下
代碼來獲取完整的產品信息(這裏的3是可配置的產品ID)
$product = Mage::getModel('catalog/product')->load(3);
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$product);
foreach($childProducts as $child) {
print_r($child->getName()); // You can use any of the magic get functions on this object to get the value
}
另一個代碼來獲取兒童產品ID
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getChildrenIds(3);
希望這有助於!
致命錯誤:調用一個非對象的成員函數getChildrenIds() –
'$ childProducts = Mage :: getModel('catalog/product_type_configurable ') - > getChildrenIds(3);'爲我完全工作(減去額外的分號。)謝謝! –
第一種方法給我所有的兒童產品,但它說所有的兒童產品都啓用了,儘管大多數都是實際的爲什麼是這樣? –
我明白了。感謝您的回覆。
<?php if($_product->getTypeId() == "configurable"): ?>
<?php $_configurable = $_product->getTypeInstance()->getUsedProductIds(); ?>
<?php foreach ($_configurable as $_config): ?>
<?php $_simpleproduct = Mage::getModel('catalog/product')->load($_config); ?>
<?php //Magic php with a $_simpleproduct. ?>
<?php endforeach; ?>
<?php endif; ?>
一個可配置產品可以有多個其他產品與之相關聯。
以下是獲取與可配置產品關聯的所有兒童產品的代碼。
這裏去的代碼:)
/**
* Load product by product id
*/
$product = Mage::getModel('catalog/product')->load(YOUR_PRODUCT_ID);
/**
* Get child products id and such (only ids)
*/
$childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
/**
* Get children products (all associated children products data)
*/
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
請注意,您可以簡單地將d而不是手動加載'product_type_configurable'模型:'$ product-> getTypeInstance() - > getChildrenIds()'或'$ product-> getTypeInstance() - > getUsedProducts()'。 – ScottSB
@ScottSB不錯,代碼越短越好。 –
對於任何人要做到這一點,並顯示結果我將分享我做了什麼來完成它
添加到script
的段:應用程序/設計/前端/默認/ [your_theme] /template/catalog/product/view/type/options/configurable.phtml
id = {};
<?php
foreach ($_product->getTypeInstance(true)->getUsedProducts (null, $_product) as $simple) {
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
echo " id[" . $simple->getId() . "] = $stock;\n\r";
}
?>
spConfig.getIdOfSelectedProduct = function() {
var existingProducts = new Object();
for (var i = this.settings.length - 1; i >= 0; i--) {
var selected = this.settings[i].options[this.settings[i].selectedIndex];
if (selected.config) {
for (var iproducts = 0; iproducts < selected.config.products.length; iproducts++) {
var usedAsKey = selected.config.products[iproducts] + "";
if (existingProducts[usedAsKey] == undefined) {
existingProducts[usedAsKey] = 1;
} else {
existingProducts[usedAsKey] = existingProducts[usedAsKey] + 1;
}
}
}
}
for (var keyValue in existingProducts) {
for (var keyValueInner in existingProducts) {
if (Number(existingProducts[keyValueInner]) < Number(existingProducts[keyValue])) {
delete existingProducts[keyValueInner];
}
}
}
var sizeOfExistingProducts = 0;
var currentSimpleProductId = "";
for (var keyValue in existingProducts) {
currentSimpleProductId = keyValue;
sizeOfExistingProducts = sizeOfExistingProducts + 1
}
if (sizeOfExistingProducts == 1) {
var qtyLeft = id[currentSimpleProductId];
if(qtyLeft >= 1) {
jQuery('.availability-only').html('Only ' + qtyLeft + ' available.');
jQuery('p.out-of-stock').removeClass('out-of-stock').addClass('in-stock');
jQuery('p.in-stock > span').html('In stock');
} else {
jQuery('.availability-only').html('Sorry, there are none available in this size.');
jQuery('p.in-stock').removeClass('in-stock').addClass('out-of-stock');
jQuery('p.out-of-stock > span').html('Out of stock');
}
}
}
在同一頁面中添加的select
:
onchange = "spConfig.getIdOfSelectedProduct()"
隨意編輯什麼語句打印,但這應該讓你那裏。它也佔庫存0手,將其更改爲Out of stock
在css和文本
正常工作....謝謝你Hardik ... – Lawrance
真棒解決方案Hardik! –
雖然這不是PHP。 – Qix