2014-03-18 43 views
6

我試圖從其中一個子簡單產品的SKU或ID獲取父配置產品SKU。我的理解是,一個簡單的可以屬於多個可配置,但我想要特定的可配置的客戶添加到他們的購物車。我讀了一個related question,但它得到所有父配置。從簡單的SKU獲取可配置的sku或ID

我使用的Magento EE 1.12

編輯:更多細節上我在做什麼。當客戶成功簽出時,我試圖獲得SKU的簡單功能,並且可以配置客戶簽出。

嘗試應用代碼:

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

回答

3

如果配置在車已經我想你可以詢問車到找到可配置的和簡單的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. 

現在,如果你是成功的頁面上面臨的挑戰是如何進入訂單項目。有堆棧溢出的答案爲灑,這裏是一個採樣:

$_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的觀察者處,上面的代碼片段會引導你。

****進一步修改***

如果你有一個$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; 
    } 

我想補充一點,我一直在天邊g很多代碼測試。當我建立了一個觀察者的事件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 retirvie 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)); 

我希望這裏的東西對你的作品。有一次,我寫了一個主題只有把簡單的產品放在購物車中,即使它起源於一個可配置的選項,所以也許值得檢查你的主題是不是這樣做(通過在默認主題開發此代碼,直到它作品)

+0

'$ item-> getProductId()'如何獲得可配置的ID? '$ option'從哪裏來?我更新了我的問題,以提供更多關於我正在嘗試做什麼的信息(即成功結賬後)。感謝您的回答,我認爲這很接近! – callmetwan

+0

我已經添加了更多代碼給我的答案,供您根據自己的情況進行嘗試。對象'$ option'被初始化並且設置在* if'($ option = $ item-> getOptionByCode('simple_product'))''conditional中。我並不總是這樣編碼,因爲它看起來像是一個打字錯誤(我們的意思是'=='),還是會引發那些可能更習慣JavaScript的編碼器,因爲這些代碼被評估爲true(因爲Javascript返回true,因爲賦值是成功的,但是PHP在'if()'子句'$ option' *是*'$ item-> getOptionByCode('simple_product')''後返回右邊的值。 – Malachy

+0

這是完美的!感謝您提供這個優秀的解釋。我非常欣賞外部的例子和教程。 – callmetwan

1

有任何直接的功能來獲取父產品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); 
    } 
    ?> 
+0

我相信這個解決方案的問題是,一個簡單的可以有多個父配置的,它會得到所有* *父可配置的ID。 – callmetwan

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

回答馬拉奇的論文大多是拉...