Magento: tilføj bundle produkt til cart - ingen pris beregnet
Hej Eksperter...Jeg har lavet et script der kan tilføje produkter til indkøbskurven, hvis man har forladt siden med varer i kurven. Det virker fint med alm. "simple products", men hvis jeg prøver at lægge et "bundled products" i kurven, så beregner den ikke prisen. Jeg tror jeg mangler et flag af en eller anden art, men jeg kan ikke lige finde ud af det.
Her er min kode:
public function addToMagentoBasket(){
$error = false;
if(empty($this->getId())){
Mage::log('ERROR: Not able to insert the database basket items into Magento basket - Basket not loaded correct', null, 'emailbasket.log');
return [['status' => 'error', 'message' => 'Kunne ikke tilføje varene fra databasen til kurven.']];
}
$entries = $this->getEntries();
if(empty($entries) || count($entries->getItems()) === 0){
Mage::log('ERROR: No items to insert - Basket returned no entries!', null, 'emailbasket.log');
return [['status' => 'error', 'message' => 'Der er ingen varer at tilføje']];
}
// Empty the cart
try{
// Initialize cart
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
// If the items already is in the basket, delete them to avoid duplicates
$cart->truncate();
$cart->save();
}
catch(Exception $e){
$error = true;
$status = [['status' => 'notice', 'message' => 'Kurven kunne ikke tømmes for tidligere varer!']];
Mage::log('Previous products could not be removed from Magento basket - '.$e->getMessage(), null, 'emailbasket.log');
}
// Setting the coupon code to the basket quote
if($error === false){
try{
// If a coupon was set to the database cart, apply it to the session cart
if(!empty($this->getCouponCode())){
Mage::log('Setting coupon code on the quote: ' . $cart->getQuote()->getId() . ' - ' . $this->getCouponCode(), null, 'emailbasket.log');
$cart->getQuote()->setCouponCode($this->getCouponCode())->save();
}
// Remove previously added coupon code
else{
$cart->getQuote()->setCouponCode('');
$status[] = ['status' => 'Notice', 'message' => 'Kupon slettet fra kurven', 'error_code' => 'no-coupon-added'];
}
}
catch(Exception $e){
$error = true;
$status[] = ['status' => 'error', 'message' => 'Kunne ikke sætte eller slette tidligere kuponer', 'error_code' => 'fail-coupon'];
Mage::log('ERROR: Could not get/set the coupon code - Exception: '.$e->getMessage(), null, 'emailbasket.log');
}
}
if($error === false){
try{
$session = Mage::getSingleton('checkout/session');
// Add all products from database basket to the session cart
foreach($entries as $entry){
// Setting some variables from the database basket
$quantity = $entry->getQuantity();
$productId = intval($entry->getProductId());
$product = Mage::getModel('catalog/product')->load($productId);
$isBundle = false;
// Resetting $params
$params = [];
// Setting default params (quantity)
$params['qty'] = (float)$quantity;
// If current item is of type bundled product
if($product->getTypeId() == "bundle"){
$isBundle = true;
// Getting all the default options for the product
$bundled_items = [];
$optionCollection = $product->getTypeInstance()->getOptionsCollection();
$selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
$options = $optionCollection->appendSelections($selectionCollection);
/**
* Looping through and creating the bundled options array for passing
* the addProduct()
*/
foreach($options as $option) {
$_selections = $option->getSelections();
foreach($_selections as $selection) {
$bundled_items[$option->getOptionId()][] = $selection->getSelectionId();
$bundled_items_option[$option->getOptionId()] = $selection->getSelectionQty();
}
}
// Setting all the options for the bundle
$params['bundle_option'] = $bundled_items;
$params['bundle_option_qty'] = $bundled_items_option;
$params['related_product'] = null;
$params['product'] = $productId;
}
//echo '<pre>'; var_dump($params); die('</pre>');
/**
* Don't know why this has to be here, but every tutorial online has
* it. I think it's because the quantity can be a decimal and there
* is a difference between countries on how to do that.
*
* But I pretty sure it can be removed.
*/
if(isset($params['qty'])){
$filter = new Zend_Filter_LocalizedToNormalized([
'locale' => Mage::app()->getLocale()->getLocaleCode()
]);
$params['qty'] = $filter->filter($params['qty']);
}
/**
* This is really weird but we have to instantiate the product a second
* time to function properly. Maybe it's because it returns a "type
* instance" earlier.
*/
$product = Mage::getModel('catalog/product')->load($productId);
$product->setPriceCalculation(true);
echo '<pre>'; var_dump($product->getData()); die('</pre>');
$varien = new Varien_Object();
$varien->setData($params);
// Finally added the product to the cart
$cart->addProduct($product, $varien);
$cart->saveQuote();
}
// Set a flag to inform the cart to recalculate cart prices
$cart->getQuote()->setTotalsCollectedFlag(false);
$cart->save();
$session->setCartWasUpdated(true);
// Set a flag for later, if customer buys the products, we flag the database
// entry, for statistics.
Mage::getSingleton('core/session')->setEmailBasketAdded('1');
$status = [['status' => 'success', 'message' => 'Alle varer er nu tilføjet til kurven']];
}
catch(Exception $e){
$error = true;
$status = [['status' => 'error', 'message' => 'Der skete en fejl. En eller flere varer kunne ikke tilføjes til indkøbskurven!']];
Mage::log('Products could not be added to Magento basket - ' . $e->getMessage(), null, 'emailbasket.log');
}
}
if($error === false){
// Sets the cookie again to update the basket if not bought
$helper = Mage::helper('emailbasket');
if(!$helper->setUbivoxCookie($this->getUbivoxMail())){
Mage::log('ERROR: Ubivox cookie mail could not be reset after adding product via email', null, 'emailbasket.log');
}
}
return $status;
}
Jeg skal måske sige at produkterne allesammen fint bliver tilføjet til kurven, men prisen bliver ikke kalkuleret. Hvis jeg trykker opdate item i min ajax mini cart eller går til siden checkout, så re-kalkulerer den fint priserne og så virker det også fremadrettet.
Håber I kan hjælpe, da jeg nu har siddet med det i 4 stive dage!
Med venlig hilsen
// Ulrik