我是magento 2的新手
我想使用Ajax获取选定的产品库存数量。
我可以通过使用以下脚本来获取选定的产品ID
app/design/frontend/Sm/market/Magento_Catalog/templates/product/view/addtocart.phtml
个文件
var $ = jQuery.noConflict();
$(window).load(function() {
requirejs(['jquery'], function(jQuery) {
var selected_options = {};
jQuery('.swatch-attribute').each(function(k, v) {
var attribute_id = jQuery(v).attr('attribute-id');
var option_selected = jQuery(v).attr('option-selected');
if (!attribute_id || !option_selected) {
return;
}
selected_options[attribute_id] = option_selected;
});
var product_id_index = jQuery('[data-role=swatch-options]').data('mageSwatchRenderer').options.jsonConfig.index;
var found_ids = 0;
jQuery.each(product_id_index, function(product_id, attributes) {
var productIsSelected = function(attributes, selected_options) {
return _.isEqual(attributes, selected_options);
}
if (productIsSelected(attributes, selected_options)) {
found_ids += parseInt(product_id);
}
});
console.log(found_ids);
alert(found_ids);
});
});
并且还可以通过使用此代码来计算产品数量
$product_Id = 1748;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$stockInfo = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($product_Id);
$stockqty = (int)$stockInfo->getQty();
echo $stockqty;
但是如何将product_id动态传递给php代码?