首页 > 解决方案 > 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的方法吗?

提前致谢!

标签: phpmagentoe-commercemagento2

解决方案


如果有多种尺寸或颜色,我使用了这个小调整以获得简单产品的 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

我希望它会帮助你


推荐阅读