php 从 Magento 产品视图中的可配置产品中获取所有简单产品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12286716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get All simple product from a Configurable Product in Magento Product View
提问by Denis Spalenza
How can I get all the simple products associated with a configurable product? I found how to do the opposite (get a product configurable from a simple product) but that's not what I need.
如何获得与可配置产品相关的所有简单产品?我找到了相反的方法(从简单的产品中获取可配置的产品),但这不是我需要的。
I want to show how many units I have in stock for the selected product (configurable attribute). My initial idea is to print all quantities of stock and control the display with jQuery. Any idea?
我想显示所选产品的库存数量(可配置属性)。我最初的想法是打印所有数量的库存并使用 jQuery 控制显示。任何的想法?
采纳答案by Hardik
use the following script in
使用以下脚本
app/design/frontend/default/[your theme]/template/catalog/product/view/type/options/configurable.phtml
app/design/frontend/default/[your theme]/template/catalog/product/view/type/options/configurable.phtml
Inside the script:
脚本内部:
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)
}
}
Now add onchangeevent to your dropdown in same page :
现在将onchange事件添加到同一页面的下拉列表中:
onchange = "spConfig.getIdOfSelectedProduct()"
回答by Great Indian Brain
Use this below code
使用下面的代码
Code to get the the full product information (where 3 is the configurable product Id)
获取完整产品信息的代码(其中 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
}
Another code to get the Children Product Ids
获取儿童产品 ID 的另一个代码
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getChildrenIds(3);
Hope this helps!!
希望这可以帮助!!
回答by Abid Hussain
A configurable product can have multiple other products associated to it.
一个可配置的产品可以有多个与其相关联的其他产品。
Here is the code to fetch all the children products that are associated with a configurable product.
这是获取与可配置产品关联的所有子产品的代码。
Here goes the code :)
这是代码:)
/**
* 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);
来源:http: //blog.chapagain.com.np/magento-how-to-get-all-related-children-product-of-a-configurable-product/
回答by Denis Spalenza
I got it. Thanks for the replies.
我知道了。感谢您的回复。
<?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; ?>
回答by Kender
For anyone looking to do this, and display the results I will share what I did to finish it off
对于任何想要这样做并显示结果的人,我将分享我为完成它所做的工作
Add to the scriptsegment of: app/design/frontend/default/[your_theme]/template/catalog/product/view/type/options/configurable.phtml
添加到以下script部分:app/design/frontend/default/[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');
}
}
}
in the selectof the same page add:
在select同一页面中添加:
onchange = "spConfig.getIdOfSelectedProduct()"
Feel free to edit what the statement prints, but this should get you there. It also accounts for 0 stock on hand, changing it to Out of stockin css and text
随意编辑语句打印的内容,但这应该会让你到达那里。它还占手头的 0 个库存,将其更改为Out of stockin css 和 text
回答by Yash
write the below simplest code in block and just call it in your template file to get the products associated products :
在块中编写以下最简单的代码,只需在模板文件中调用它即可获取产品关联的产品:
$productId = (int)$this->getRequest()->getParam('id');
$objectManager = \Magento\Framework\App\objectManager::getInstance();
$product = $objectManager->create("\Magento\Catalog\Model\Product")->load($productId);
$collection = $product->getTypeInstance()->getAssociatedProducts($product);
$this->setCollection($collection);
now, in template write the below code to print the names :
现在,在模板中编写以下代码来打印名称:
$collection = $block->getCollection();
foreach ($collection as $key => $value)
{
echo $value->getName();
echo "<br>";
}

