كيفية الانضمام إلى جدول مخصص مع مجموعة المنتجات

magento.stackexchange https://magento.stackexchange.com//questions/29859

  •  11-12-2019
  •  | 
  •  

سؤال

لقد قمت بإنشاء وحدة بائع مخصصة.كيف يمكنني الحصول على اسم البائع مع مجموعة المنتجات؟.

لدي سمة بائع تأتي قيمها من وحدة البائع.

حاولت

$collection = Mage::getModel('catalog/product')->getCollection()
$collection->getSelect()->join( array('tvendor'=>$this->getTable('vendor/vendor')), 'main_table.vendor = tvendor.vendor_option_id', array('tvendor.filename'));

لكنها لا تعمل.أين أخطئ؟

هل كانت مفيدة؟

المحلول

الأعمال التالية .
لقد توصلت إلى هذا بناءً على الطريقة التي يقوم بها Magento بفرز مجموعة المنتجات بناءً على سمة تحتوي على خيارات.رؤية المزيد من التفاصيل حول هذا في Mage_Eav_Model_Entity_Attribute_Source_Table::addValueSortToCollection و Mage_Eav_Model_Resource_Entity_Attribute_Option::addOptionValueToCollection

//===>configure the fields below based on your needs
$attributeCode = 'vendor_id'; //replace this with the attribute code that holds the vendor
//get the attribute object
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attributeCode);
//attribute id
$attributeId = $attribute->getId();
$tablePkName = 'vendor_id'; //primary key name of the vendor table
$nameField = 'name'; //vendor table name field - the one you need
//get table that holds the vendors
$table = Mage::getSingleton('core/resource')->getTableName('vendor/vendor');
//<=== end of configuration
$collection = Mage::getModel('catalog/product')->getCollection();
//add the vendor attribtue to select
$collection->addAttributeToSelect($attributeCode);
$valueTable1    = $attributeCode . '_t1';
$valueTable2    = $attributeCode . '_t2';
//perform 2 left joins in case the attribute has the scope website or store view. 
//It works even if the attribute has the scope global. this covers all the cases
$collection->getSelect()
    ->joinLeft(
        array($valueTable1 => $attribute->getBackend()->getTable()),
        "e.entity_id={$valueTable1}.entity_id"
            . " AND {$valueTable1}.attribute_id='{$attributeId}'"
            . " AND {$valueTable1}.store_id=0",
        array())
    ->joinLeft(
        array($valueTable2 => $attribute->getBackend()->getTable()),
        "e.entity_id={$valueTable2}.entity_id"
            . " AND {$valueTable2}.attribute_id='{$attributeId}'"
            . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
        array()
    );
$valueExpr = $collection->getSelect()->getAdapter()
    ->getCheckSql("{$valueTable2}.value_id > 0", "{$valueTable2}.value", "{$valueTable1}.value");
$optionTable   = $attributeCode . '_option_value_t1';
$tableJoinCond = "{$optionTable}.{$tablePkName}={$valueExpr}";
//join with the vendor table
$collection->getSelect()
    ->joinLeft(
        array($optionTable => $table),
        $tableJoinCond,
        array($nameField));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top