1

I am work on the creation of a module to do Cross-Selling.

I already take all of the command who have more than 1 product to make relation with that. But now, to complete my code and module i wanted to know how can i recovery the command with more than 1 article and take also all the command of the customer (a lot of customer have buy 1 by 1 some products).

I want to have the product in a same command but now i want to implemant to get all of the command past by the same client.

Right now, the module put in a table (work with a cron), all data every day but let see the code :

Module Block file:

    class MyCompany_Crosssell_Block_Crosssell extends Mage_Catalog_Block_Product_Abstract {

    public function _prepareLayout() {
        return parent::_prepareLayout();
    }

    public function getTitle() {
        $title = Mage::getStoreConfig('catalog/crosssell/title');
        return $title;
    }

    public function getEnable() {
        return Mage::getStoreConfig('catalog/crosssell/enable');
    }


    public function getProductsBought(){
        return Mage::getModel('MyCompany_crosssell/crosssell')
                ->getBoughtProducts(Mage::helper('MyCompany_crosssell')->getcollectProductBoughtId(Mage::registry('current_product')))
                ->setPageSize(Mage::getStoreConfig('catalog/crosssell/products'));        
    }
}

Module Helper Class

   class Mycompany_Crosssell_Helper_Data extends Mage_Core_Helper_Abstract {
    public function getcollectProductBoughtId($product){
        return explode(',',Mage::getModel('Mycompany_crosssell/crosssell')->load($product->getId())->getProducttype());
    }
}

Module Model class

    class Mycompany_Crosssell_Model_Observer extends Varien_Event_Observer {

    public function __construct() {

    }

    public function crosssell($observer) {

        $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
        $collection->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
        $collection->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
        $connexion = Mage::getSingleton('core/resource')->getConnection('core_write');
        $connexion->query("TRUNCATE TABLE `crosssell_crosssell`");
        $values = array();
        $cpt = 0;
        foreach ($collection as $product) {
            $productids = Mage::getModel('Mycompany_crosssell/crosssell')->collectProductBoughtId($product);
            /* $model = Mage::getModel('Mycompany_crosssell/crosssell');
              $model->setData('productid', $product->getId());
              $model->setData('producttype', implode(',', $productids));
              $model->save(); */

            if ($productids) {
                $values[] = "(" . $product->getId() . ", '" . implode(',', $productids) . "')";
                if ($cpt++ > 500) {
                    $connexion->query("
                            INSERT IGNORE INTO `crosssell_crosssell`(productid, producttype) VALUES " . implode(',', $values));
                    $cpt = 0;
                    $values = array();
                }
            }
        }
        $connexion->query("
                            INSERT IGNORE INTO `crosssell_crosssell`(productid, producttype) VALUES " . implode(',', $values));
    }

}

    class Mycompany_Crosssell_Model_Crosssell extends Mage_Core_Model_Abstract {

    public function _construct() {
        parent::_construct();
        $this->_init('Mycompany_crosssell/crosssell');
    }

    public function collectProductBoughtId($product) {
        $_order = '';
        $_productOption = array();
        $productOption = array();
        $_orderId = '';
        $_order = Mage::getResourceModel('sales/order_item_collection')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('product_id', $product->getId());
        foreach ($_order as $order) {
            $_orderId[] = $order->getOrderId();
        }
        $_product = Mage::getResourceModel('sales/order_item_collection') 
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('order_id', $_orderId)
                ->addAttributeToFilter('product_id', array('neq' => $product->getId()));
        foreach ($_product as $_productData) {
            $_productId[] = $_productData->getProductId();
        }

        return array_unique($_productId);
    }

    public function getBoughtProducts($product) {
        $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToFilter('entity_id', array('in', $product));
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        $attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
        $collection->addAttributeToSelect($attributes)
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents();
        $collection->getSelect()->order(new Zend_Db_Expr('RAND()'));

        return $collection;
    }

}

    class Mycompany_Crosssell_Model_Mysql4_Crosssell_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{

    public function _construct(){
        $this->_init('Mycompany_crosssell/crosssell');
    }

}



      class Mycompany_Crosssell_Model_Mysql4_Crosssell extends Mage_Core_Model_Mysql4_Abstract
{
    protected function _construct()
    {
        $this->_init('Mycompany_crosssell/crosssell', 'productid');
    }
}

Installer file

     <?php
$installer = $this;
$installer->startSetup();
$table = $installer->getConnection()
    ->newTable($installer->getTable('Mycompany_crosssell/crosssell'))
    ->addColumn('productid', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'auto_increment' => false,
        'identity'  => false,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'ID product')
    ->addColumn('producttype', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array(
        'auto_increment' => false,
        'identity'  => false,
        'unsigned' =>false,
        'nullable' =>false,
        'primary'  =>false,
    ), 'type product');

$installer->getConnection()->createTable($table);
$installer->endSetup();
  • I tried to use my foreach to get customer_id and entity_id to do a join on the table but it doesn't work... – felegento Apr 16 '15 at 10:02

Browse other questions tagged or ask your own question.