I'm adding a "buy now" button to my site. I'm using the code from here.
When I click the button, my site tries to go to example.com/checkout/#93=4
, which then results in it going to example.com/checkout/noroute
. Where is the #93=4
part coming from? I've tried to step through the Magento code in a debugger, and as far as I can tell, the extension is trying to go to example.com/checkout/
, and I can't figure out what's adding the #93=4
. But I'll be honest; I don't understand Magento's routing.
Here are the parts of the code that I think are relevant:
buynow.js
define([
'jquery'
], function ($) {
"use strict";
return function (config, element) {
$(element).click(function () {
var form = $(config.form);
var baseUrl = form.attr('action'),
buyNowUrl = baseUrl.replace('checkout/cart/add', 'buynow/cart/add');
form.attr('action', buyNowUrl);
form.trigger('submit');
form.attr('action', baseUrl);
return false;
});
}
});
Add.php
I've used the debugger to verify that the line $baseUrl = $this->_objectManager->get('\Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl();
towards the bottom of the code does return example.com/checkout/
(no #93=4
)
class Add extends \Magento\Checkout\Controller\Cart\Add
{
/**
* Add product to shopping cart action
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
if (!$this->_formKeyValidator->validate($this->getRequest())) {
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
$params = $this->getRequest()->getParams();
try {
if (isset($params['qty'])) {
$filter = new \Zend_Filter_LocalizedToNormalized(
['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()]
);
$params['qty'] = $filter->filter($params['qty']);
}
$product = $this->_initProduct();
$related = $this->getRequest()->getParam('related_product');
if (!$product) {
return $this->goBack();
}
$cartProducts = $this->_objectManager->create('Test\BuyNowButton2\Helper\Data')
->getConfig('buynow/general/keep_cart_products');
if (!$cartProducts) {
$this->cart->truncate(); //remove all products from cart
}
$this->cart->addProduct($product, $params);
if (!empty($related)) {
$this->cart->addProductsByIds(explode(',', $related));
}
$this->cart->save();
$this->_eventManager->dispatch(
'checkout_cart_add_product_complete',
['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()]
);
if (!$this->_checkoutSession->getNoCartRedirect(true)) {
$baseUrl = $this->_objectManager->get('\Magento\Store\Model\StoreManagerInterface')
->getStore()->getBaseUrl();
return $this->goBack($baseUrl.'checkout/', $product);
}
} catch (\Exception $e) {
// ...
}
}
}