简体   繁体   English

SKU - Magento将可配置产品添加到购物车中

[英]Adding configurable product to the cart by SKU - Magento

I am playing with programatic cart recreation. 我正在玩程序化的购物车娱乐。 There is no problem of adding products to cart by id ie 通过id即可将产品添加到购物车中

 $params = array(
    'product' => 272,
    'super_attribute' => array(
        22 =>30 ,
    ),
    'qty' => 2,
);


$cart = Mage::getSingleton('checkout/cart'); 
$product = new Mage_Catalog_Model_Product();
$product->load(272); 
$cart->addProduct($product, $params);
$cart->save(); 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

But how to do it if I know only product sku, I mean how to get product id and super_attributes for configurable products. 但是如果我只知道产品sku怎么做呢,我的意思是如何获得可配置产品的产品ID和super_attributes。

Mage::getModel('catalog/product')->loadByAttribute('sku','$sku');

seems to return only that product (sku) info and no special_attribute. 似乎只返回该产品(sku)信息而没有special_attribute。

Any ideas? 有任何想法吗?

cheers, /Marcin 欢呼,/ Marcin

i think, feeela didn't answer the question. 我想, 费耶拉没有回答这个问题。

general (without error-handling): 一般(没有错误处理):

$sku = 'any-sku-number';
$qty = 1;

$product = Mage::getModel('catalog/product')
    ->loadByAttribute('sku', $sku);

$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')
    ->getParentIdsByChild($product->getId());

$parent = Mage::getModel('catalog/product')->load(current($parentIds));

if ($parent->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
    $paramsSuperAttribute = array('super_attribute' => array());

    foreach ($parent->getTypeInstance(true)->getConfigurableAttributes($parent) as $attribute) {
        $paramsSuperAttribute['super_attribute'][$attribute->getProductAttribute()->getAttributeId()] =
            (int) $product->getData($attribute->getProductAttribute()->getAttributeCode());
    }
}

$params = array('qty' => $qty);

now, there are two ways adding a product to cart by sku: 现在,有两种方法可以通过sku将产品添加到购物车中:

programmatically: 编程方式:

$cart = Mage::getSingleton('checkout/cart');

$cart->addProduct(isset($parent) ? $parent : $product, array_merge($params, $paramsSuperAttribute))->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

by url: 按网址:

$addToCartUrl = Mage::helper('checkout/cart')
    ->getAddUrl(isset($parent) ? $parent : $product, $params);

if (isset($parent)) {
    $addToCartUrl .= '?' . http_build_query($paramsSuperAttribute);
}

// now you can use this url like this (controller action)
$this->getResponse()->setRedirect($addToCartUrl);
return;

You could retrieve the product by getting a collection, filtered by the SKU: 您可以通过获取由SKU过滤的集合来检索产品:

$searchForThisSku = 'p_1234';

/** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */
$productCollection = Mage::getModel( 'catalog/product' )
    ->getResourceCollection()
    ->addFieldToFilter( 'sku', $searchForThisSku );

/** @var $product Mage_Catalog_Model_Product */
$product = $productCollection->getFirstItem();

// you should have the product now, maybe it isn't even necessary to get the ID
$product->getId();

// all in one line:
$productId = (int) Mage::getModel( 'catalog/product' )
    ->getResourceCollection()
    ->addFieldToFilter( 'sku', $searchForThisSku )
    ->getFirstItem()
    ->getId();

Try 尝试

/** @product Mage_Catalog_Model_Product */
$product = Mage::getModel('catalog/product');

$sku = 'sku';
$product->load($sku, 'sku');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM