2009-05-22 18 views
10

मैं शेयर आइटम है, जो सरल उत्पादों के रूप में Magento पर अपलोड किया जा रहा है की एक बड़ी रेंज के साथ एक ग्राहक डेटाबेस मिल गया है करने के लिए सरल उत्पादों पहले से मौजूद।Magento एपीआई: बताए विन्यास उत्पादों

अब मैं उन्हें समूह की जरूरत है और उन्हें अपने आकार और रंग उनके विन्यास गुण होने के साथ विन्यास उत्पादों के लिए आवंटित।

Magento API में एक उत्पाद दिखने वाला तरीका है: कैटलॉग-उत्पाद-लिंक.सign (link), लेकिन मैं अपने जीवन के लिए यह नहीं समझ सकता कि मुझे इसके साथ काम करने के लिए क्या तर्क चाहिए कॉन्फ़िगर करने योग्य उत्पाद, यह प्रदान करना यह है कि असाइनमेंट का उपयोग कैसे किया जाना था।

+14

Magento दस्तावेज बकवास है यह नहीं है। – Dan

+0

ओह, मैं तुम्हें वहाँ सुनता हूँ! मैं अब उनसे स्पैम प्राप्त करता हूं और फिर वास्तव में उपयोगकर्ता दस्तावेज खरीदता हूं। Pfft! – keith

+0

हाँ, मुझे ट्विटर के माध्यम से भी 'सुझाव' मिल गया है। असल में, मैंने पहले ही आधिकारिक उपयोगकर्ता मार्गदर्शिका खरीदी है, जो डेवलपर के लिए उपयोग नहीं है। पीएचपी | आर्किटेक्ट की किताब भी खरीदी, जो एक अच्छा पठन है लेकिन 10 गुना मोटा होना चाहिए। –

उत्तर

5

खैर नोटों मुझे इस प्रदर्शन शुरू करने में मदद की। इसलिए मैंने सोचा कि मैं आपके साथ मौजूदा कॉन्फ़िगर करने योग्य उत्पाद में एक साधारण उत्पाद जोड़ने के लिए कोड साझा करूंगा।

यह कोड मानता है कि सरल उत्पाद जोड़ने के लिए एक वैध एक है, मुझे यकीन नहीं है कि क्या होगा यदि यह नहीं होता।

private function _attachProductToConfigurable($_childProduct, $_configurableProduct) { 
    $loader = Mage::getResourceModel('catalog/product_type_configurable')->load($_configurableProduct); 

    $ids = $_configurableProduct->getTypeInstance()->getUsedProductIds(); 
    $newids = array(); 
    foreach ($ids as $id) { 
     $newids[$id] = 1; 
    } 

    $newids[$_childProduct->getId()] = 1; 

    $loader->saveProducts($_configurableProduct->getId(), array_keys($newids));     
} 
+0

मैं इसे कमांड लाइन स्क्रिप्ट से करने की कोशिश कर रहा हूं और मैं यहां असफल रहा हूं: $ loader = mage :: getResourceModel ('catalog/product_type_configurable') -> लोड ($ _configurableProduct); (पहली पंक्ति) कोई विचार? मैं वर्तमान में इसकी जांच कर रहा हूं और परिणाम के मामले में सूचित करूंगा। –

+0

मैंने फिर से Magento के हाल के संस्करणों में काम करने के लिए स्किमन द्वारा कोड अपडेट किया: [नीचे देखें] (http://stackoverflow.com/a/14461333/219467) – aeno

1

मैं इस एक अन-शिक्षित अनुमान है, लेकिन मुझे लगता है कि अपने मौजूदा एपीआई के साथ नहीं किया जा सकता के लिए पूछ रहा। आपको अपना खुद का लिखना होगा या सीधे डीबी को मिलना होगा।

+1

ईएवी डीबी स्कीमा के साथ वे सीधे डीबी तक पहुंचने पर 'बस' नहीं हैं। दर्द!!! –

2

मैं अब यह सही कर रही पर काम कर रहा हूँ।

अब तक मैं संदर्भ के रूप में उपयोगी इन मदों मिल गया है:

मैं अब तक मेरी कोड पोस्ट करेंगे, और उम्मीद है कि इसे अद्यतन एक बार यह काम करता है ..

// Set 'item_size' as the super attribute # choose your own attribute! 
// this is the 'choose-able' field that differenciates products 
$super_attributes=array(Mage::getModel('eav/entity_attribute') 
    ->loadByCode('catalog_product','item_size') 
    ->getData('attribute_id') 
); 
$product_collection=Mage::getModel('catalog/product')->getCollection(); 

// Fetch configurable orders 
$product_collection->addFieldToFilter('type_id',Array('eq'=>"configurable")); 
#$product_collection->addFieldToFilter('sku',Array('eq'=>"ASMCL000002")); 

$product_collection->addAttributeToSelect('*'); 

$count=0; 
foreach($product_collection as $product) { 
    $sku = $product->getSku(); 
    echo "SKU: $sku\n"; 

    $simple_children_collection = Mage::getModel('catalog/product')->getCollection(); 
    $simple_children_collection->addAttributeToSelect('*'); 
    $simple_children_collection->addFieldToFilter('sku',Array('like'=>$sku . "-%")); 
    echo "children: "; 
    foreach($simple_children_collection as $child) { 
     $child_sku = $child->getSku(); 
     echo "$child_sku "; 
     #visiblity should be 'nowhere' 
    } 
    echo "\n"; 

if (!$product->getTypeInstance()->getUsedProductAttributeIds()) { 
    # This is a new product without the Configurable Attribue Ids set 
    $product->getTypeInstance() 
    ->setUsedProductAttributeIds($super_attributes); 

    //$product->setConfigurableAttributesData(array($_attributeData)); 
    $product->setCanSaveConfigurableAttributes(true); # Not sure if this is needed. 

    $product->setConfigurableProductsData(''); # Use this to add child products. 

} 


    $count++; 

    try { 
     $product->save(); 
     $productId = $product->getId(); 
     echo $product->getId() . ", $sku updated\n"; 
    } 
    catch (Exception $e){ 
     echo "$sku not added\n"; 
     echo "exception:$e"; 
    } 

} 
echo "\nCount is $count\n"; 

ठीक है, यह 'item_size' का उपयोग उस विशेषता के रूप में करता है जो "सरल" उत्पादों को अलग करता है। साथ ही, यह मानता है कि "कॉन्फ़िगर करने योग्य" पैरेंट एसकेयू बच्चे एसकेयू की जड़ है। उदाहरण के लिए, एबीसी 001 माता-पिता है जबकि एबीसी 001-लघु और एबीसी 001-लार्ज सरल बच्चे हैं।

उम्मीद है कि किसी की मदद करता है। यहाँ

+0

मुझे नहीं पता कि आप अभी भी इस पर काम कर रहे हैं लेकिन मुझे लगता है कि मैंने इसे क्रैक कर दिया है। – Scimon

1

यहां हैक-वाई तरीका है जिसे मैंने सीधे PHP के साथ किया है। तीन संबंधित टेबल हैं। मैं अपने गुणों के रूप में रंग और आकार का उपयोग कर रहा था। मेरे मूल उत्पाद (कॉन्फ़िगर करने योग्य) वास्तव में मेरे कैटलॉग में मौजूद नहीं हैं। वे अनिवार्य रूप से मॉडल स्तर हैं और फिर उत्पाद एसकेयू स्तर हैं। तो बच्चों के लिए 'parentproductsku%' काम करता है।

$query1 = "SELECT * FROM mage_catalog_product_entity WHERE type_id= 'configurable'"; 
    //Find the parent id 
    $statusMessage = "Ok, found a product with a confgurable attribute"; 
    $result1 = $this->runQuery($query1, "query1", $statusMessage); 
    while ($row1 = mysql_fetch_assoc($result1)) { //entering the first loop where products are configurable 
     $this->parentId = $row1['entity_id']; 
     $this->parentSku = $row1['sku']; 

     echo "The SKU was $this->parentSku" . "<br />"; 

    //insert these into the link table for association 
    $query2 = "SELECT * FROM mage_catalog_product_entity WHERE type_id= 'simple' AND sku LIKE '" . $this->parentSku . "%';"; 
    // find the child ids that belong to the parent 
    $statusMessage = "Found some children for $this->parentSku"; 
    $result2 = $this->runQuery($query2, "query2", $statusMessage); 
    while ($row2 = mysql_fetch_assoc($result2)) {//entering the second loop where SKU is like model sku 
     $this->childId = $row2['entity_id']; 
     $this->childSku = $row2['sku']; 

     echo "Now we're working with a child SKU $this->childSku" . "<br />"; 

     //"REPLACE INTO catalog_product_super_attribute SET product_id='".$product->entity_id."', attribute_id='".$attribute->attribute_id."', position='".$position."'"; 
     $query3 = "REPLACE INTO mage_catalog_product_super_attribute (product_id, attribute_id, position) VALUES ('" . $this->childId . "', '76', '0');"; 
     $message3 = "Inserted attribute for color for ID $this->childId SKU $this->childSku"; 
     $result3 = $this->runQuery($query3, "query3", $message3); 

     $query4 = "REPLACE INTO mage_catalog_product_super_attribute_label (product_super_attribute_id, store_id, use_default, value) VALUES (LAST_REPLACE_ID(), '0', '0', 'Color');"; 
     $message4 = "Inserted attribute for Color SKU $this->childSku ID was $this->db->insert_id"; 
     $result4 = $this->runQuery($query4, "query4", $message4); 

     $query5 = "REPLACE INTO mage_catalog_product_super_attribute (product_id, attribute_id, position) VALUES ('" . $this->childId . "', '529', '0');"; 
     $message5 = "Inserted attribute for Product Size SKU $this->childSku"; 
     $result5= $this->runQuery($query5, "query5", $message5); 


     $query6 = "REPLACE INTO mage_catalog_product_super_attribute_label (product_super_attribute_id, store_id, use_default, value) VALUES (LAST_REPLACE_ID(), '0', '0', 'Size');"; 
     $message6 = "Inserted attribute for Size SKU $this->childSku ID was $this->db->insert_id"; 
     $result6 = $this->runQuery($query6, "query6", $message6); 

     $query7 = "REPLACE INTO mage_catalog_product_super_link (product_id, parent_id) VALUES ('" . $this->childId . "', '" . $this->parentId . "');"; 
     $message7 = "Inserted $this->childId and $this->parentId into the link table"; 
     $result7 = $this->runQuery($query7, "query7", $message7); 

     $query8 = "REPLACE INTO mage_catalog_product_relation (parent_id, child_id) VALUES ('" . $this->parentId . "', '" . $this->childId . "');"; 
     $message8 = "Inserted $this->childId and $this->parentId into the link table"; 
     $result8 = $this->runQuery($query8, "query8", $message8); 

     } //end while row 2 the child ID 

      } //end while row 1 the parent id 
1

हैरानी की बात है, इस काम करता है, अपने सभी सरल उत्पादों समान मूल्य साझा करता है, तो:

 $childProducts = $configurable->getTypeInstance(true)->getUsedProductIds($configurable); 

     // Don't add this product if it's already there 
     if(!in_array($child->getId(), $childProducts)) {  
      $childProducts[] = $child->getId(); 
     } 


     $existingIds = $configurable->getTypeInstance(true)->getUsedProductAttributeIds($configurable); 
     $newAttributes = array(); 

     foreach($configurable->getTypeInstance(true)->getSetAttributes($configurable) as $attribute) { 

     if(!in_array($attribute->getId(), $existingIds) && $configurable->getTypeInstance(true)->canUseAttribute($attribute) 
      && $child->getAttributeText($attribute->getAttributeCode())) { 

      // Init configurable attribute 
      $configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute') 
       ->setProductAttribute($attribute); 

      // Add new attribute to array 
      $newAttributes[] = array(
       'id'    => $configurableAtt->getId(), 
       'label'   => $configurableAtt->getLabel(), 
       'position'  => $attribute->getPosition(), 
       'values'   => $configurableAtt->getPrices() ? $configurable->getPrices() : array(), 
       'attribute_id' => $attribute->getId(), 
       'attribute_code' => $attribute->getAttributeCode(), 
       'frontend_label' => $attribute->getFrontend()->getLabel(), 
      ); 
     } 
    } 

    if(!empty($newAttributes)) { 

     $configurable->setCanSaveConfigurableAttributes(true); 
     $configurable->setConfigurableAttributesData($newAttributes); 
    } 
     $configurable->setConfigurableProductsData(array_flip($childProducts)); 
     $configurable->save(); 
3

Scimon द्वारा स्वीकार कर लिया जवाब से कोड अब और (Magento के हाल के संस्करणों में कम से कम में काम नहीं करता 1.7)। लेकिन सौभाग्य से, आप फिर से काम कर इसे पाने के लिए सिर्फ एक छोटे से ठीक की जरूरत है:

private function _attachProductToConfigurable($_childProduct, $_configurableProduct) { 
    $loader = Mage::getResourceModel('catalog/product_type_configurable')->load($_configurableProduct, $_configurableProduct->getId()); 

    $ids = $_configurableProduct->getTypeInstance()->getUsedProductIds(); 
    $newids = array(); 
    foreach ($ids as $id) { 
     $newids[$id] = 1; 
    } 

    $newids[$_childProduct->getId()] = 1; 

    //$loader->saveProducts($_configurableProduct->getid(), array_keys($newids));     
    $loader->saveProducts($_configurableProduct, array_keys($newids));     
} 
+0

मेरे पास एक अलग जवाब था? कुछ समय पहले, मैंने एक साल के लिए कोई Magento विकास नहीं किया है या तो अब इसके साथ जाओ। – Scimon

+0

शायद यह स्वीकार्य उत्तर IMHO में एक संपादन होना चाहिए था। –

+0

@ जोसेफ: लेखन के समय मुझे ऐसा करने के लिए पर्याप्त प्रतिष्ठा नहीं थी, इसलिए मैंने एक नया जवाब पोस्ट किया। – aeno

0

@ Aeno के समाधान मेरे लिए काम नहीं किया है, इसलिए मैं इसे थोड़ा परिष्कृत किया।यह Mage::getModel('catalog/product')->load() विधि के माध्यम से तत्काल उत्पाद का उपयोग करके परीक्षण किया गया है।

private function _attachProductToConfigurable($childProduct, $configurableProduct) 
{ 
    $childIds = $configurableProduct->getTypeInstance()->getUsedProductIds(); 
    $childIds[] = $childProduct->getId(); 
    $childIds = array_unique($childIds); 

    Mage::getResourceModel('catalog/product_type_configurable') 
     ->saveProducts($configurableProduct, $childIds); 
} 
संबंधित मुद्दे