Bundle.php 4.94 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Bundle\Model\ResourceModel;

/**
 * Bundle Resource Model
 *
 * @api
 * @since 100.0.2
 */
class Bundle extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\Relation
     */
    protected $_productRelation;

    /**
     * @var \Magento\Quote\Model\ResourceModel\Quote
     */
    protected $quoteResource;

    /**
     * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
     * @param \Magento\Catalog\Model\ResourceModel\Product\Relation $productRelation
     * @param \Magento\Quote\Model\ResourceModel\Quote $quoteResource
     * @param string $connectionName
     */
    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\Relation $productRelation,
        \Magento\Quote\Model\ResourceModel\Quote $quoteResource,
        $connectionName = null
    ) {
        parent::__construct($context, $connectionName);
        $this->_productRelation = $productRelation;
        $this->quoteResource = $quoteResource;
    }

    /**
     * Resource initialization
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('catalog_product_entity', 'entity_id');
    }

    /**
     * Preparing select for getting selection's raw data by product id
     * also can be specified extra parameter for limit which columns should be selected
     *
     * @param int $productId
     * @param array $columns
     * @return \Magento\Framework\DB\Select
     */
    protected function _getSelect($productId, $columns = [])
    {
        return $this->getConnection()->select()->from(
            ["bo" => $this->getTable('catalog_product_bundle_option')],
            ['type', 'option_id']
        )->where(
            "bo.parent_id = ?",
            $productId
        )->where(
            "bo.required = 1"
        )->joinLeft(
            ["bs" => $this->getTable('catalog_product_bundle_selection')],
            "bs.option_id = bo.option_id AND bs.parent_product_id = bo.parent_id",
            $columns
        );
    }

    /**
     * Retrieve selection data for specified product id
     *
     * @param int $productId
     * @return array
     */
    public function getSelectionsData($productId)
    {
        return $this->getConnection()->fetchAll($this->_getSelect($productId, ["*"]));
    }

    /**
     * Removing all quote items for specified product
     *
     * @param int $productId
     * @return void
     */
    public function dropAllQuoteChildItems($productId)
    {
        $connection = $this->quoteResource->getConnection();
        $select = $connection->select();
        $quoteItemIds = $connection->fetchCol(
            $select->from(
                $this->getTable('quote_item'),
                ['item_id']
            )->where(
                'product_id = :product_id'
            ),
            ['product_id' => $productId]
        );

        if ($quoteItemIds) {
            $connection->delete(
                $this->getTable('quote_item'),
                ['parent_item_id IN(?)' => $quoteItemIds]
            );
        }
    }

    /**
     * Removes specified selections by ids for specified product id
     *
     * @param int $productId
     * @param array $ids
     * @return void
     */
    public function dropAllUnneededSelections($productId, $ids)
    {
        $where = ['parent_product_id = ?' => $productId];
        if (!empty($ids)) {
            $where['selection_id NOT IN (?) '] = $ids;
        }
        $this->getConnection()->delete($this->getTable('catalog_product_bundle_selection'), $where);
    }

    /**
     * Save product relations
     *
     * @param int $parentId
     * @param array $childIds
     * @return $this
     */
    public function saveProductRelations($parentId, $childIds)
    {
        $this->_productRelation->processRelations($parentId, $childIds);

        return $this;
    }

    /**
     * Add product relation (duplicate will be updated)
     *
     * @param int $parentId
     * @param int $childId
     * @return $this
     * @since 100.1.0
     */
    public function addProductRelation($parentId, $childId)
    {
        $this->_productRelation->addRelation($parentId, $childId);
        return $this;
    }

    /**
     * Add product relations
     *
     * @param int $parentId
     * @param array $childIds
     * @return $this
     */
    public function addProductRelations($parentId, $childIds)
    {
        $this->_productRelation->addRelations($parentId, $childIds);
        return $this;
    }

    /**
     * Remove product relations
     *
     * @param int $parentId
     * @param array $childIds
     * @return $this
     */
    public function removeProductRelations($parentId, $childIds)
    {
        $this->_productRelation->removeRelations($parentId, $childIds);
        return $this;
    }
}