Magento:捆绑包未添加到购物车
我做了一个自定义Magento扩展,允许捆绑产品类型与“父”捆绑产品相关联 例如,如果我在销售计算机部件,我创建了一系列简单的产品类型(如键盘、鼠标、显示器等),我创建了一个捆绑产品,捆绑了键盘和鼠标,可以很好地使用Magento提供的现成产品。如果我创建了另一种捆绑产品类型,但这次我想捆绑我刚刚制作的捆绑产品(带有键盘和鼠标的产品),我无法做到这一点。因此,我所做的是创建一个扩展,允许我将捆绑产品与捆绑产品相关联 我在“local”和下面创建了一个新的扩展名(我称之为Magento:捆绑包未添加到购物车,magento,bundle,shopping-cart,checkout,Magento,Bundle,Shopping Cart,Checkout,我做了一个自定义Magento扩展,允许捆绑产品类型与“父”捆绑产品相关联 例如,如果我在销售计算机部件,我创建了一系列简单的产品类型(如键盘、鼠标、显示器等),我创建了一个捆绑产品,捆绑了键盘和鼠标,可以很好地使用Magento提供的现成产品。如果我创建了另一种捆绑产品类型,但这次我想捆绑我刚刚制作的捆绑产品(带有键盘和鼠标的产品),我无法做到这一点。因此,我所做的是创建一个扩展,允许我将捆绑产品与捆绑产品相关联 我在“local”和下面创建了一个新的扩展名(我称之为Company) app/
Company
)
app/code/local/Company/etc/config.xml
app/code/local/Company/Bundle/Block/Adminhtml/Catalog/Product/Composite/Fieldset/Options/Type/Checkbox.php
app/code/local/Company/Bundle/Model/Product/Type.php
这就是一切。有对LIST.phtml的引用,但如果需要,可以将其更改为复选框。然而,如果你在一个包中添加一个包——它会一直工作到我把它添加到我的购物车。如果您能帮我解决这个问题,我们将不胜感激
更新:
因此,我能够找到为什么我不能将“家长”捆绑产品添加到我的购物车中
protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
它位于Type.php下,我发现这个问题可能与
public function getSelectionsByIds($selectionIds, $product = null)
因为它不返回“选择”(与具有简单产品类型的捆绑产品相比,不是null,但没有选择),并且因为它为getItems()
提供null。如果我删除
->addFilterByRequiredOptions()
通过函数getSelectionsByIds
,我可以从getItems()
获取一些信息,但它会在
$_result = $selection->getTypeInstance(true)->prepareForCart($buyRequest, $selection);
。
请帮忙!我只是需要一些方法,将这个“家长”捆绑包添加到我的购物车中,就像普通捆绑产品一样。谢谢你抽出时间 开箱即用的建议,无需如此复杂的解决方案。我们在magento销售捆绑包套件,但我将其设置为简单产品,并简单地使用相关产品分配任何类型的产品(在您的情况下是其他捆绑包套件或任何其他类型的产品)。我们只需在产品页面中添加一个新的块,该块显示我们所需的相关产品,并将其标题改为工具包内容。当用户将项目添加到购物车时,它只是一个简单的产品。您还可以向购物车页面添加一个块,在购物车列表页面上的项目下显示相关项目。从历史上看,我们在magento和bundle工具包方面存在问题,有时人们在不删除并重新添加它的情况下无法结账,而此解决方案完全解决了这一问题,并将为您提供所需的功能。虽然我们不使用管理员来管理产品或订单(都是通过api和后台完成的),但原则可以被纳入订单等的管理员中。您在这方面有什么进展吗?谢谢我已经开始悬赏这个问题。我正在使用Magento 1.7,在复制Simon的步骤后,我达到了相同的状态。当尝试将产品添加到购物车(通过管理面板)时,它会在后台崩溃。希望有人能帮助我们。@Matheusjarimb谢谢你开始悬赏,不幸的是我在这方面没有任何进展。我放弃了这一点,因为我无法追踪这个问题。
<?php
class Company_Bundle_Model_Product_Type extends Mage_Bundle_Model_Product_Type
{
/**
* Checking if we can sale this bundle
*
* @param Mage_Catalog_Model_Product $product
* @return bool
*/
public function isSalable($product = null)
{
$salable = Mage_Catalog_Model_Product_Type_Abstract::isSalable($product);
if (!is_null($salable)) {
return true; /* OVERRIDE! */
}
$optionCollection = $this->getOptionsCollection($product);
if (!count($optionCollection->getItems())) {
return false;
}
$requiredOptionIds = array();
foreach ($optionCollection->getItems() as $option) {
if ($option->getRequired()) {
$requiredOptionIds[$option->getId()] = 0;
}
}
$selectionCollection = $this->getSelectionsCollection($optionCollection->getAllIds(), $product);
if (!count($selectionCollection->getItems())) {
return false;
}
$salableSelectionCount = 0;
foreach ($selectionCollection as $selection) {
if ($selection->isSalable()) {
$requiredOptionIds[$selection->getOptionId()] = 1;
$salableSelectionCount++;
}
}
return (array_sum($requiredOptionIds) == count($requiredOptionIds) && $salableSelectionCount);
}
/**
* Retrive bundle selections collection based on used options
*
* @param array $optionIds
* @param Mage_Catalog_Model_Product $product
* @return Mage_Bundle_Model_Mysql4_Selection_Collection
*/
public function getSelectionsCollection($optionIds, $product = null)
{
$keyOptionIds = (is_array($optionIds) ? implode('_', $optionIds) : '');
$key = $this->_keySelectionsCollection . $keyOptionIds;
if (!$this->getProduct($product)->hasData($key)) {
$storeId = $this->getProduct($product)->getStoreId();
$selectionsCollection = Mage::getResourceModel('bundle/selection_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addAttributeToSelect('tax_class_id') //used for calculation item taxes in Bundle with Dynamic Price
->setFlag('require_stock_items', true)
->setFlag('product_children', true)
->setPositionOrder()
->addStoreFilter($this->getStoreFilter($product))
->setStoreId($storeId);
//->addFilterByRequiredOptions() OVERRIDE!
//->setOptionIdsFilter($optionIds) OVERRIDE!
if (!Mage::helper('catalog')->isPriceGlobal() && $storeId) {
$websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
$selectionsCollection->joinPrices($websiteId);
}
$this->getProduct($product)->setData($key, $selectionsCollection);
}
return $this->getProduct($product)->getData($key);
}
/**
* Prepare product and its configuration to be added to some products list.
* Perform standard preparation process and then prepare of bundle selections options.
*
* @param Varien_Object $buyRequest
* @param Mage_Catalog_Model_Product $product
* @param string $processMode
* @return array|string
*/
protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
{
$result = Mage_Catalog_Model_Product_Type_Abstract::_prepareProduct($buyRequest, $product, $processMode);
if (is_string($result)) {
return $result;
}
$selections = array();
$product = $this->getProduct($product);
$isStrictProcessMode = $this->_isStrictProcessMode($processMode);
$skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck();
$_appendAllSelections = (bool)$product->getSkipCheckRequiredOption() || $skipSaleableCheck;
$options = $buyRequest->getBundleOption();
if (is_array($options)) {
$options = array_filter($options, 'intval');
$qtys = $buyRequest->getBundleOptionQty();
foreach ($options as $_optionId => $_selections) {
if (empty($_selections)) {
unset($options[$_optionId]);
}
}
$optionIds = array_keys($options);
if (empty($optionIds) && $isStrictProcessMode) {
return Mage::helper('bundle')->__('Please select options for product.');
}
$product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
$optionsCollection = $this->getOptionsCollection($product);
$selectionIds = array();
foreach ($options as $optionId => $selectionId) {
if (!is_array($selectionId)) {
if ($selectionId != '') {
$selectionIds[] = (int)$selectionId;
}
} else {
foreach ($selectionId as $id) {
if ($id != '') {
$selectionIds[] = (int)$id;
}
}
}
}
// If product has not been configured yet then $selections array should be empty
if (!empty($selectionIds)) {
$selections = $this->getSelectionsByIds($selectionIds, $product);
// Check if added selections are still on sale
foreach ($selections->getItems() as $key => $selection) {
if (!$selection->isSalable() && !$skipSaleableCheck) {
$_option = $optionsCollection->getItemById($selection->getOptionId());
if (is_array($options[$_option->getId()]) && count($options[$_option->getId()]) > 1) {
$moreSelections = true;
} else {
$moreSelections = false;
}
if ($_option->getRequired()
&& (!$_option->isMultiSelection() || ($_option->isMultiSelection() && !$moreSelections))
) {
return Mage::helper('bundle')->__('Selected required options are not available.');
}
}
}
$optionsCollection->appendSelections($selections, false, $_appendAllSelections);
$selections = $selections->getItems();
} else {
$selections = array();
}
} else {
$product->setOptionsValidationFail(true);
$product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
$optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
$optionIds = $product->getTypeInstance(true)->getOptionsIds($product);
$selectionIds = array();
$selectionCollection = $product->getTypeInstance(true)
->getSelectionsCollection(
$optionIds,
$product
);
$options = $optionCollection->appendSelections($selectionCollection, false, $_appendAllSelections);
foreach ($options as $option) {
if ($option->getRequired() && count($option->getSelections()) == 1) {
$selections = array_merge($selections, $option->getSelections());
} else {
$selections = array();
break;
}
}
}
if (count($selections) > 0 || !$isStrictProcessMode) {
$uniqueKey = array($product->getId());
$selectionIds = array();
// Shuffle selection array by option position
usort($selections, array($this, 'shakeSelections'));
foreach ($selections as $selection) {
if ($selection->getSelectionCanChangeQty() && isset($qtys[$selection->getOptionId()])) {
$qty = (float)$qtys[$selection->getOptionId()] > 0 ? $qtys[$selection->getOptionId()] : 1;
} else {
$qty = (float)$selection->getSelectionQty() ? $selection->getSelectionQty() : 1;
}
$qty = (float)$qty;
$product->addCustomOption('selection_qty_' . $selection->getSelectionId(), $qty, $selection);
$selection->addCustomOption('selection_id', $selection->getSelectionId());
$beforeQty = 0;
$customOption = $product->getCustomOption('product_qty_' . $selection->getId());
if ($customOption) {
$beforeQty = (float)$customOption->getValue();
}
$product->addCustomOption('product_qty_' . $selection->getId(), $qty + $beforeQty, $selection);
/*
* Create extra attributes that will be converted to product options in order item
* for selection (not for all bundle)
*/
$price = $product->getPriceModel()->getSelectionFinalTotalPrice($product, $selection, 0, $qty);
$attributes = array(
'price' => Mage::app()->getStore()->convertPrice($price),
'qty' => $qty,
'option_label' => $selection->getOption()->getTitle(),
'option_id' => $selection->getOption()->getId()
);
$_result = $selection->getTypeInstance(true)->prepareForCart($buyRequest, $selection);
if (is_string($_result) && !is_array($_result)) {
return $_result;
}
if (!isset($_result[0])) {
return Mage::helper('checkout')->__('Cannot add item to the shopping cart.');
}
$result[] = $_result[0]->setParentProductId($product->getId())
->addCustomOption('bundle_option_ids', serialize(array_map('intval', $optionIds)))
->addCustomOption('bundle_selection_attributes', serialize($attributes));
if ($isStrictProcessMode) {
$_result[0]->setCartQty($qty);
}
$selectionIds[] = $_result[0]->getSelectionId();
$uniqueKey[] = $_result[0]->getSelectionId();
$uniqueKey[] = $qty;
}
// "unique" key for bundle selection and add it to selections and bundle for selections
$uniqueKey = implode('_', $uniqueKey);
foreach ($result as $item) {
$item->addCustomOption('bundle_identity', $uniqueKey);
}
$product->addCustomOption('bundle_option_ids', serialize(array_map('intval', $optionIds)));
$product->addCustomOption('bundle_selection_ids', serialize($selectionIds));
return $result;
}
return $this->getSpecifyOptionMessage();
}
/**
* Retrieve message for specify option(s)
*
* @return string
*/
public function getSpecifyOptionMessage()
{
return Mage::helper('bundle')->__('Please specify product option(s).');
}
}
<?php
class Company_Bundle_Model_Resource_Option_Collection extends Mage_Bundle_Model_Resource_Option_Collection
{
/**
* Append selection to options
* stripBefore - indicates to reload
* appendAll - indicates do we need to filter by saleable and required custom options
*
* @param Mage_Bundle_Model_Resource_Selection_Collection $selectionsCollection
* @param bool $stripBefore
* @param bool $appendAll
* @return array
*/
public function appendSelections($selectionsCollection, $stripBefore = false, $appendAll = true)
{
if ($stripBefore) {
$this->_stripSelections();
}
if (!$this->_selectionsAppended) {
foreach ($selectionsCollection->getItems() as $key => $_selection) {
if ($_option = $this->getItemById($_selection->getOptionId())) {
$_selection->setOption($_option);
$_option->addSelection($_selection);
}
}
$this->_selectionsAppended = true;
}
return $this->getItems();
}
}
protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
public function getSelectionsByIds($selectionIds, $product = null)
->addFilterByRequiredOptions()
$_result = $selection->getTypeInstance(true)->prepareForCart($buyRequest, $selection);