Magento 从产品中获取 Wishlistitem

Magento get Wishlistitem from Product

public function addItem(Mage_Wishlist_Model_Item $item)
{

    $superAttributes = Mage::app()->getRequest()->getParam('super_attribute');
    if ($item instanceof Mage_Wishlist_Model_Item && isset($superAttributes)) {
        $simpleItem = Mage::getModel('catalog/product_type_configurable')->getProductByAttributes($superAttributes, $item->getProduct());
    }

    return parent::addItem($simpleItem);
}

现在我的问题是我想调用需要接收 Mage_Wishlist_Model_Item 而不是 Mage_Catalog_Model_Product

的父函数
MyCustom_Wishlist_Model_Wishlist extends Mage_Wishlist_Model_Wishlist

我如何获得产品的愿望清单项目$simpleItem

public function addItem(Mage_Wishlist_Model_Item $item)
{
    $superAttributes = Mage::app()->getRequest()->getParam('super_attribute');
    if ($item instanceof Mage_Wishlist_Model_Item && isset($superAttributes)) {
        $product = Mage::getModel('catalog/product_type_configurable')->getProductByAttributes($superAttributes, $item->getProduct());
        $item = Mage::getModel('wishlist/item');
        $storeId = $product->hasWishlistStoreId() ? $product->getWishlistStoreId() : $this->getStore()->getId();
        $qty = 1;

        $item->setProductId($product->getId())
            ->setWishlistId($this->getId())
            ->setAddedAt(now())
            ->setStoreId($storeId)
            ->setOptions($product->getCustomOptions())
            ->setProduct($product)
            ->setQty($qty)
            ->save();
    }

    return parent::addItem($item);
}

这现在对我有用,但我还没有弄清楚如何设置 $qty

你检查过这个URL了吗? https://magento.stackexchange.com/questions/19678/how-do-i-add-to-wishlist-programatically

这是将产品添加到心愿单的原生方式。

如果您不想更改参数类型,则可以从您的代码中删除 $item 类型检查(这不是必需的):

$item instanceof Mage_Wishlist_Model_Item

希望对你有帮助。