Magento从产品中获取愿望清单

时间:2018-08-29 07:08:34

标签: php magento e-commerce magento-1.9

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的愿望清单

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您检查过此URL吗? https://magento.stackexchange.com/questions/19678/how-do-i-add-to-wishlist-programatically

这是将产品添加到愿望清单的本机方法。

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

$item instanceof Mage_Wishlist_Model_Item

希望它对您有帮助。