如果配置在车已经我想你可以询问车到找到可配置的和简单的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