構成可能なものがすでにカートに入っている場合は、カートに問い合わせて、構成可能なものとその単純な 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);
この Q&Aとその 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) {
この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) {
どちらもこの Q&A によるものです。
しかし、Magento に精通した Yireo による次のチュートリアルが最も役立つと思います。
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$cartItems = $order->getAllItems();
foreach($cartItems as $item) {
Yireo.com の Jisse Reitsma による
それで、あなたはすべて準備ができているはずです。最終的なコードにまとめるにはいくつかの断片がありますが、コードをtemplate/checkout/success.phtml
オブザーバーまたはオブザーバーに配置してcheckout_type_onepage_save_order_after
いると思います。上記のスニペットがガイドになります。
****さらなる編集***
その SKU を取得する必要が$product
ある場合は、次の理由により常に単純な SKU を返す呼び出しのみを呼び出す必要があります。product_type = 'configurable'
$product->getData('sku');
$product->getSku();
//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
両方とも設定可能な SKU が返されます。これにより、構成可能な から「構成可能なオプション」を確認する方法を正しくロードしていないか、理解していないと思います。しかし、オブザーバーの「$item」と success.phtml に違いがある理由がわかりません。getSku()
getData('sku')
sales/order
$item
あなたはおそらくこれをすでに知っているでしょうが、私は追加すると思いました:イベントをキャッチすると、販売/見積もりと販売/注文オブジェクトの両方にアクセスできますが、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));
ここで何かがうまくいくことを願っています。過去に、構成可能なオプションとして作成されたものであっても、単純な製品をカートに入れるだけのテーマを書いたことがあります。