如果配置在車已經我想你可以詢問車到找到可配置的和簡單的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&A和that 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
的觀察者處,上面的代碼片段會引導你。
****進一步修改***
如果你有一個$product
是product_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));
我希望這裏的東西對你的作品。有一次,我寫了一個主題只有把簡單的產品放在購物車中,即使它起源於一個可配置的選項,所以也許值得檢查你的主題是不是這樣做(通過在默認主題開發此代碼,直到它作品)
'$ item-> getProductId()'如何獲得可配置的ID? '$ option'從哪裏來?我更新了我的問題,以提供更多關於我正在嘗試做什麼的信息(即成功結賬後)。感謝您的回答,我認爲這很接近! – callmetwan
我已經添加了更多代碼給我的答案,供您根據自己的情況進行嘗試。對象'$ option'被初始化並且設置在* if'($ option = $ item-> getOptionByCode('simple_product'))''conditional中。我並不總是這樣編碼,因爲它看起來像是一個打字錯誤(我們的意思是'=='),還是會引發那些可能更習慣JavaScript的編碼器,因爲這些代碼被評估爲true(因爲Javascript返回true,因爲賦值是成功的,但是PHP在'if()'子句'$ option' *是*'$ item-> getOptionByCode('simple_product')''後返回右邊的值。 – Malachy
這是完美的!感謝您提供這個優秀的解釋。我非常欣賞外部的例子和教程。 – callmetwan