php - Magento2 在 AddToCart 时获取简单的 SKU
问题描述
添加到购物车时,我试图获取简单产品的 SKU。我有一个可配置的产品,它有 3 个尺寸选项 S、M、L。每个都有自己的 SKU,当我选择尺寸并单击添加到购物车时,我想获取简单产品的 sku。
我查看了 js 文件 catalog-add-to-cart.js 它不返回简单的 sku,只返回可配置的 sku。
但是在控制台中有来自http://mywebsite.com/customer/section/load/?sections=cart%2Cmessages&update_section_id=true的响应
其中包含简单的 sku。
有人可以告诉我一种快速捕获简单sku的方法吗?
提前致谢!
解决方案
如果有多种尺寸或颜色,我使用了这个小调整以获得简单产品的 SKU,而不是可配置的产品
//By default, use the default product image
$path = Mage::helper('catalog/image')->init($product, 'small_image');
//BUT
//If there is multiple colors/sizes
if (is_array($product["_cache_instance_products"])){
//For each product in the inventory
foreach ($product["_cache_instance_products"] as $_prod ) {
//check if the SKU match with the one of the article in the Cart
if($_prod["sku"]==$product->getSku()){
//check if this product have various color
if (isset($_prod["small_image"])){
//if so, take the specific image
$path = Mage::helper('catalog/image')->init($_prod, 'small_image');
}
}
}
}
这段代码放在:shop/app/code/community/Mdl/Ajaxcheckout/controllers/IndexController.php
我希望它会帮助你
推荐阅读
- python-3.x - Selenium 无法获取 iframe > iframe > img 内的图像链接
- ios - 具有通用(可选)用户默认值的属性包装器
- javascript - 样式化的组件最终样式直接注入到 DOM 中
标签,使用样式标签注入会影响性能吗? - python - 转换一系列日期格式熊猫不起作用
- php - 自动提交后重置选择值
- python - 如何将 python 3.7 设为我的默认 python3?
- php - Symfony Messenger 使用 Apache Kafka 作为队列传输
- asp.net-mvc - asp.net 相同的解决方案名称但在不同的目录下相互同步
- python - 运行 python 命令时在 ES 版本 7.3.2 上使用 elastalert 时出现问题
- c# - 泛型和 Nullable(类与结构)