如果可配置项已经在购物车中,我认为您可以查询购物车以找到可配置项及其简单 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.
现在,如果您在成功页面上,挑战是如何访问订单商品。 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 将指导你。
****进一步编辑***
如果您有一个$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;
}
我要补充一点,我一直在运行大量代码测试。当我在事件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));
我希望这里有一些适合你的东西。过去有一次我写了一个主题,它只将简单的产品放入购物车,即使它起源于一个可配置的选项,所以也许值得检查你的主题没有这样做(通过在默认主题,直到它工作)