从简单的SKU或ID获取可配置的sku

cal*_*wan 6 magento skus configurable-product

我正试图从其中一个儿童简单产品的SKU或ID中获取父可配置产品SKU.我的理解是,一个简单的可以属于多个配置,但我希望特定的可配置客户添加到他们的购物车.我读了一个相关的问题,但它获得了所有父配置.

我正在使用Magento EE 1.12

编辑:关于我正在做什么的更多细节.当客户成功结账时,我试图获得SKU的简单和可配置的客户签出.

试图将代码应用于:

app/design/frontend/enterprise/mytheme/template/checkout/success.phtml
Run Code Online (Sandbox Code Playgroud)

Mal*_*chy 4

如果可配置项已在购物车中,我认为您可以询问购物车以查找可配置项及其简单 ID。

$myTargetSimpleProductId = $someIdThatYouKnow;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item){
   if ($option = $item->getOptionByCode('simple_product')) {
     $productIdArray[] = $option->getProduct()->getId(); //for the record
     if ($option->getProduct()->getId()==$myTargetSimpleProductId ){
       $myMatchingConfigurableProductId = $item->getProductId(); //capture the ID of the configurable
     }
   }
 }
//$productIdArray holds a list of all the IDs of simple products that are in the basket due to configurables.
echo("The answer is ".$myMatchingConfigurableProductId);
Run Code Online (Sandbox Code Playgroud)

代码改编自这个问答那个问答 ,但未经测试,所以请让我知道这是否非常糟糕并且您无法找出所有更正。

****在下面的评论之后编辑更多地解释代码***

总的来说,这有助于理解当有人将可配置产品添加到购物车时,Magento 主要存储可配置产品的产品 ID,而不是底层简单产品的产品 ID。但是 Magento 购物车中配置的 Magento 产品是一个复杂的对象,其中一部分是对底层简单产品的引用。

所以:

$item->getProductId(); //Really means [pseudo code] $item->getConfiguredProductId() 
$item->getOptionByCode('simple-product') //Accesses the underlying simple product object, hence
$item->getOptionByCode('simple-product')->getProduct()->getId() //gives accesse to the ID of the underlying simple product - ie the thing you want to test.
Run Code Online (Sandbox Code Playgroud)

现在,如果您位于成功页面,那么面临的挑战是如何访问订单项目。Stack Overflow 上有一些答案,下面是一个示例:

$_order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
foreach ($_order->getAllItems() as $item) {
Run Code Online (Sandbox Code Playgroud)

由于这个问答。或者

$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order'); 
$order->load($lastOrderId);
foreach ($order->getItemsCollection() as $item) {
Run Code Online (Sandbox Code Playgroud)

或来自观察者函数:

$order = $observer->getOrder();
/* @var $item Mage_Sales_Model_Order_Item */
foreach ($order->getItemsCollection() as $item) {
Run Code Online (Sandbox Code Playgroud)

都是由于这个问答

但我认为您可能会从精通 Magento 的 Yireo 的本教程中受益最多:

$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$cartItems = $order->getAllItems();
foreach($cartItems as $item) {
Run Code Online (Sandbox Code Playgroud)

感谢 Yireo.com 的 Jisse Reitsma

所以你应该已经准备好了。有一些零散的片段需要串在一起形成最终的代码,但我认为您要么将代码放入template/checkout/success.phtml观察者中,要么将其放入观察者中checkout_type_onepage_save_order_after,上面的片段将指导您。

****进一步编辑***

如果您有一个$productproduct_type = 'configurable'获取其 SKU 的产品,则您应该致电$product->getData('sku');(如果您只调用$product->getSku();该产品,则始终会返回简单的 SKU,因为)

//file: app/code/core/Mage/Core/Catalog/Model/Product.php
//class: Mage_Catalog_Model_Product
public function getSku()
    {
        return $this->getTypeInstance(true)->getSku($this);
    }
Run Code Online (Sandbox Code Playgroud)

如果你遵循代码,你会发现

//file: app/code/core/Mage/Core/Catalog/Model/Product/Type/Configurable.php
//class: Mage_Catalog_Model_Product_Type_Configurable
public function getSku($product = null)
    {
        $sku = $this->getProduct($product)->getData('sku');
        if ($this->getProduct($product)->getCustomOption('option_ids')) {
            $sku = $this->getOptionSku($product,$sku);
        }
        return $sku;
    }
Run Code Online (Sandbox Code Playgroud)

我想补充一点,我已经运行了大量的代码测试。当我在事件上设置观察员checkout_type_onepage_save_order_after

$cartItems = $observer->getEvent()->getOrder()->getAllVisibileItems();
foreach ($cartItems as $item){
   if ($item->getProductType() == 'configurable') {
     $skuConfigurable = $item->getProduct()->getData('sku');
     $skuMatchingSimple = $item->getProduct()->getSku();
     Mage::log("skuConfigurable ".$skuConfigurable." has skuMatchingSimple ".$skuMatchingSimple, null, 'mylogfile.log');
   }else{
     Mage::log("Configurable SKU"." (not configurable product)", null, 'mylogfile.log');
   }
}
Run Code Online (Sandbox Code Playgroud)

效果很好,但是当我从内部访问订单商品时,success.phtml两者getSku()都会getData('sku')返回可配置的 SKU。这让我认为我没有sales/order正确加载或不理解如何从可配置的 .config 文件中确定“可配置选项” $item。但我不明白为什么观察者中的“$item”与 success.phtml 之间存在这种差异。

您可能已经知道这一点,但我想我会添加:如果您捕获该事件,那么您可以访问销售/报价和销售/订单对象,但当您到达 success.phtml 时,销售/报价对象已被重置并且您只能访问销售/订单(如果您需要的只是可配置的 sku,我认为这对您来说没问题)。

这是我在 success.phtml 中运行的代码,对我来说它只返回配置 SKU

<?php //TEST CODE to retrieve SKUs from the order

$_checkoutSession = Mage::getSingleton('checkout/session');
$_customerSession = Mage::getSingleton('customer/session');

$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

echo("<br>Order Id".$orderId);

$myTargetSimpleProductId = 42;//$someIdThatYouKnow;

//$cartItems = $order->getAllVisibleItems(); //returns the configurable items only
$cartItems = $order->getAllItems();
Mage::log(':PHTML--PHTML--PHTML--PHTML--PHTML--PHTML--PHTML--PHTML--', null, 'mylogfile.log'); 
Mage::log(':', null, 'mylogfile.log'); 
Mage::log('cartItems (from order):', null, 'mylogfile.log'); 

foreach ($cartItems as $item){
    Mage::log("product_id".$item->getProductId(), null, 'mylogfile.log');
    Mage::log("product_type".$item->getProductType(), null, 'mylogfile.log');
    Mage::log("product_real_type".$item->getRealProductType(), null, 'mylogfile.log');
   if ($option = $item->getOptionByCode('simple_product')) { //NEVER RETURNS TRUE, why?
      Mage::log("item_opByCode_getProd_getId".$item->getOptionByCode('simple_product')->getProduct()->getId(), null, 'mylogfile.log');
   }else{
     Mage::log("item_opByCode_getProd_getId"." (not simple_product option)", null, 'mylogfile.log');
   }
   if ($item->getProductType() == 'configurable') {
     $dummy = $item->getProduct()->getData('sku');
     Mage::log("Configurable SKU ".$dummy, null, 'mylogfile.log');             $myAnswers[]=array('simpleSku'=>$item->getProduct()->getSku(),'configurableSku'=>$item->getProduct()->getData('sku')); //in success.phtml these two are always the same (the configurable SKU)
   }else{
     Mage::log("Configurable SKU"." (not configurable product)", null, 'mylogfile.log');
   }

    Mage::log("item options".print_r($item->getOptions(),true), null, 'mylogfile.log');
    Mage::log("getProduct()->getId()".$item->getProduct()->getId(), null, 'mylogfile.log');
    Mage::log("getProduct()->getSku".$item->getProduct()->getSku(), null, 'mylogfile.log');
    Mage::log("getProduct()->getName()".$item->getProduct()->getName(), null, 'mylogfile.log');
    Mage::log("SKUs : ".print_r($myAnswers,true), null, 'mylogfile.log');
    Mage::log("<br>********************************", null, 'mylogfile.log');

   if($item->getOptions()){ //NEVER RUNS - how get the configurable product options?
     echo("OPTIONS".print_r($item->getOptions(),true));
   }

 }
echo("SKU's array".print_r($myAnswers,true));
Run Code Online (Sandbox Code Playgroud)

我希望这里的东西对你有用。过去有一次,我编写了一个主题,即使它起源于可配置选项,也将简单的产品放入购物车,所以也许值得检查您的主题是否这样做(通过在默认主题中开发此代码直到它起作用)