【问题标题】:Get configurable sku from simple SKU or ID从简单的 SKU 或 ID 获取可配置的 sku
【发布时间】:2014-03-18 22:29:54
【问题描述】:

我正在尝试从其中一个子简单产品的 SKU 或 ID 中获取父级可配置产品 SKU。我的理解是一个简单的可以属于多个可配置项,但我希望客户添加到他们的购物车中的特定可配置项。我读了related question,但它得到了 all 父配置。

我正在使用 Magento EE 1.12

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

尝试将代码应用于:

app/design/frontend/enterprise/mytheme/template/checkout/success.phtml

【问题讨论】:

    标签: magento configurable-product skus


    【解决方案1】:

    如果可配置项已经在购物车中,我认为您可以查询购物车以找到可配置项及其简单 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);
    

    改编自 this Q&Athat Q&A 的代码未经测试所以请告诉我这个炸弹是否可怕并且你无法弄清楚所有的更正。

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

    总体而言,有助于理解当有人将可配置产品添加到购物车时,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.
    

    现在,如果您在成功页面上,挑战是如何访问订单商品。 Stack Overflow 上有一些答案,这里有一个示例:

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

    due to this Q&A。或者

    $_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) {
    

    或来自观察者函数:

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

    both due to this Q&A.

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

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

    due to Jisse Reitsma of Yireo.com

    所以你应该准备好了。有一些零碎的东西可以串成你的最终代码,但我认为你要么将代码放在 template/checkout/success.phtml 中,要么放在 checkout_type_onepage_save_order_after 上的观察者中,上面的 sn-ps 将指导你。

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

    如果您有一个$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);
        }
    

    如果你按照代码,你会发现

    //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;
        }
    

    我要补充一点,我一直在运行大量代码测试。当我在事件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');
       }
    }
    

    很好用,但是当我从 success.phtml 访问订单项目时,getSku()getData('sku') 都返回可配置的 SKU。这让我认为我没有正确加载sales/order,或者不了解如何从可配置的$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));
    

    我希望这里有一些适合你的东西。过去有一次我写了一个主题,它将简单的产品放入购物车,即使它起源于一个可配置的选项,所以也许值得检查你的主题没有这样做(通过在默认主题,直到它工作)

    【讨论】:

    • $item-&gt;getProductId() 如何获取可配置 ID? $option 来自哪里?我更新了我的问题,以提供有关我正在尝试做的事情的更多信息(即在成功结帐后)。感谢您的回答,我想这已经接近了!
    • 我在我的答案中添加了更多代码供您尝试,具体取决于您的情况。对象 $option 被初始化并设置 inside if($option = $item-&gt;getOptionByCode('simple_product')) 条件。我并不总是这样编码,因为它要么看起来像一个打字错误(我们的意思是'=='),要么它会抛出可能更习惯于 JavaScript 的编码器,其中此类代码被评估为 true(Javascript 返回 true,因为赋值成功,但是 PHP 返回右侧的值所以在if() 子句之后$option is $item-&gt;getOptionByCode('simple_product')).
    • 这太完美了!感谢您提供如此出色的解释。我非常感谢外部示例和教程。
    • 我回到了开始的地方。我使用了您提供的最后一个 sn-p,但我不知道如何获取 Configurable SKU。在 foreach 中,我尝试了 $item->getSku(),但它没有返回任何内容。当我记录 $item 时,我发现 Mage_Sales_Model_Order_Item 之外的一个 $data 数组包含 product_type => 可配置的,但它的 SKU 是简单的,而不是管理面板中显示的可配置!
    【解决方案2】:

    我只需要 AJAX 购物车功能的 SKU。这对我有用:

    $cart = Mage::helper('checkout/cart')->getCart();
    $items = $cart->getItems();
    foreach ($items as $item) {
      if ($item->getProductType() == 'configurable') {
           $sku = $item->getProduct()->getData('sku');
      } else {
          $sku = $item->getSku();  // simple product
      }
    }
    

    答案主要来自马拉奇的论文......

    【讨论】:

      【解决方案3】:

      有任何直接的功能来获取父产品sku

       <?php
           $parentIds  = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($childproid); 
          /* $parentIds is array of ids */ 
          foreach( $parentIds as $eachProID)
          $child=Mage::getModel('catalog/product')->load($eachProID);
          }
          ?>
      

      【讨论】:

      • 我相信这个解决方案的问题是一个简单的可以有多个可配置的父级,它会得到所有个可配置的父级ID。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 2014-02-03
      • 2013-03-17
      • 1970-01-01
      相关资源
      最近更新 更多