Magento get URL before current - php

I want to get URL before the current one in Magento and check if is shopping cart and checkout page. For example now I am in the My account page, I want to check if the visited page before My account was Shopping Cart page.
I try to add this function but is not work because my last URL is login, not
shopping cart
public function customerRegistration(Varien_Event_Observer $observer)
{
$lastUrl = Mage::getSingleton('core/session')->getLastUrl();
if(preg_match("#onestepcheckout/index#", $lastUrl)){
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('onestepcheckout/index'));
Mage::app()->getResponse()->sendResponse();
exit;
}
else{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
Mage::app()->getResponse()->sendResponse();
exit;
}
}
Update:
i find a solution, to redirect after register to checkout page if there exist a product in cart, but is a problem, after register is complete in the checkout the Billing fields is not complete and the user is not login. Anyone?
New Update:
This is what I made to redirect after login and it is work perfect, I need to do the same for Register. The problem with Register is because here the lastUrl is login. Anyone? with any idea?
public function customerLogin(Varien_Event_Observer $observer)
{
if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister()){
$lasturl = Mage::getSingleton('core/session')->getLastUrl();
if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false){
if (! preg_match("#customer/account/create#", $lasturl) && Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect')) {
if(Mage::getSingleton('core/session')->getIsFromCart() == 1 || Mage::getSingleton('core/session')->getIsFromCheckout() == 1){
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('onestepcheckout/index'));
Mage::app()->getResponse()->sendResponse();
exit;
}
else
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
Mage::app()->getResponse()->sendResponse();
exit;
}
}
}
}
Mage::getSingleton("core/session")->setRedirectregister(false);
Mage::getSingleton('core/session')->setIsFromCart('0');
Mage::getSingleton('core/session')->setIsFromCheckout('0');
}
Update:
A good developer told me that is a way to do this. To add an event before going to register page then add an event observer after registration is complete and then check what is necessary. But I don't know to do this, maybe someone can help me with this? Thank you
Thank you
My Original Code
public function customerLogin(Varien_Event_Observer $observer)
{
if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister()){
$lasturl = Mage::getSingleton('core/session')->getLastUrl();
if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false){
if (! preg_match("#customer/account/create#", $lasturl) && Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect')) {
if(Mage::getSingleton('core/session')->getIsFromCart() == 1 || Mage::getSingleton('core/session')->getIsFromCheckout() == 1){
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('onestepcheckout/index'));
Mage::app()->getResponse()->sendResponse();
exit;
}
else
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
Mage::app()->getResponse()->sendResponse();
exit;
}
}
}
}
Mage::getSingleton("core/session")->setRedirectregister(false);
Mage::getSingleton('core/session')->setIsFromCart('0');
Mage::getSingleton('core/session')->setIsFromCheckout('0');
}
/*method for SignUp Customerredirect*/
public function customerRegistration(Varien_Event_Observer $observer)
{
Mage::getSingleton("core/session")->setRedirectregister(true);
if (Mage::helper('customerredirect')->isEnabled() && Mage::helper('customerredirect')->isoptionEnabled('registration_customerredirect') ) {
$_session = $this->_getSession();
$_session->setBeforeAuthUrl(Mage::helper('customerredirect')->setRedirectOnSignup());
}
}
This is my config.xml of this extension
<events>
<customer_login>
<observers>
<customerredirect>
<class>customerredirect/observer_customer</class>
<method>customerLogin</method>
</customerredirect>
</observers>
</customer_login>
<customer_register_success>
<observers>
<customerredirect>
<class>customerredirect/observer_customer</class>
<method>customerRegistration</method>
</customerredirect>
</observers>
</customer_register_success>
</events>
So all I want now is the Register page to work like Login, In present the login redirection is perfect, but for Register always I am redirect to My Account page. Not to the checkout page.
New Option:
if (strpos(Mage::getSingleton('core/session')->getLastUrl(), 'checkout/cart') !== false) {
Mage::getSingleton('core/session')->setIsFromCart('1');
} else {
Mage::getSingleton('core/session')->setIsFromCart('0');
}
if (strpos(Mage::getSingleton('core/session')->getLastUrl(), 'onestepcheckout/index') !== false) {
Mage::getSingleton('core/session')->setIsFromCheckout('1');
} else {
Mage::getSingleton('core/session')->setIsFromCheckout('0');
}

There is an easier solution.
Create an override of:
/app/code/core/Mage/Customer/controllers/AccountController.php
Copy _loginPostRedirect() method
In the newly created file, in _loginPostRedirect method edit:
FROM:
$this->_redirectUrl($session->getBeforeAuthUrl(true));
TO:
$sUrl = $session->getBeforeAuthUrl( TRUE );
// Do url exclusions and conditional checks here
// Force user to go home on login.
$sUrl = Mage::getBaseUrl();
$this->_redirectUrl( $sUrl );
Cleaned up with your conditionals (they are a bit convoluted):
$sUrl = $session->getBeforeAuthUrl( TRUE );
if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister())
{
$lasturl = Mage::getSingleton('core/session')->getLastUrl();
if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false)
{
if (! preg_match("#customer/account/create#", $lasturl) && Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect'))
{
if( Mage::getSingleton('core/session')->getIsFromCart() == 1 || Mage::getSingleton('core/session')->getIsFromCheckout() == 1 )
{
$sUrl = Mage::getUrl('onestepcheckout/index');
}
else
{
$sUrl = Mage::getUrl('customer/account');
}
}
}
}
$this->_redirectUrl( $sUrl );
Edited for register:
$sUrl = $session->getBeforeAuthUrl( TRUE );
if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister())
{
if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false)
{
if ( Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect'))
{
$lasturl = Mage::getSingleton('core/session')->getLastUrl();
if( Mage::getSingleton('core/session')->getIsFromCart() == 1 || Mage::getSingleton('core/session')->getIsFromCheckout() == 1
|| preg_match("#customer/account/create#", $lasturl) )
{
$sUrl = Mage::getUrl('onestepcheckout/index');
}
}
}
}

Related

Repeated URL post parameters issue

Hi can anyone help why URL string parameters post again and again?
HTTP://127.0.0.1/ab/1936.html?cart=yes?cart=yes
i m using this parameter to open mini cart when we added product into cart in magneto 1.9
Please help me how to protect this?
i am using this code-
<?php
if ($_GET['cart']=='yes') {
echo "<script type='text/javascript'>
jQuery('.minicart_open').show();
</script>";
}
?>
cartController.php
protected function _goBack()
{
$returnUrl = $this->getRequest()->getParam('return_url');
// print_r($returnUrl);exit;
if ($returnUrl) {
if (!$this->_isUrlInternal($returnUrl)) {
throw new Mage_Exception('External urls redirect to "' . $returnUrl . '" denied!');
}
$this->_getSession()->getMessages(true);
$this->getResponse()->setRedirect($returnUrl);
} elseif (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
&& !$this->getRequest()->getParam('in_cart')
&& $backUrl = $this->_getRefererUrl()
) {
$this->getResponse()->setRedirect($backUrl.'?cart=yes');
} else {
if (
(strtolower($this->getRequest()->getActionName()) == 'add')
&& !$this->getRequest()->getParam('in_cart')
) {
$this->_getSession()->setContinueShoppingUrl($this->_getRefererUrl());
}
$this->_redirect('checkout/cart');
}
return $this;
}
i got the answer
Just replace below line
$this->getResponse()->setRedirect($backUrl.'?cart=yes');
To
$url = $backUrl;
// Search substring
if (strpos($url, $key) == false) {
$this->getResponse()->setRedirect($backUrl.'?cart=yes');
}
else {
$this->getResponse()->setRedirect($backUrl);
}
//exit;
its working for me...Hope its helpful for you...

Prestashop remove one checkout step

I'm new to prestashop and I'm having major trouble removing the address(I want to have only Summary=Shrnutí, Login/Guest Checkout=Přihlásit se, Delivery=Doručení and Payment=Platba here https://www.enakupak.cz/objednavka?step=1) step,. I am using prestashop 1.6.1.5
I know I have to modify order-carrier.tpl file and have followed several posts here and there but couldn't get it done right.
Does any of you have any actual idea on how to do this ?
I think that it will be change in this part of OrderController.php but dont know how to concretly change it
switch ((int)$this->step) {
case OrderController::STEP_SUMMARY_EMPTY_CART:
$this->context->smarty->assign('empty', 1);
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
case OrderController::STEP_DELIVERY:
if (Tools::isSubmit('processAddress')) {
$this->processAddress();
}
$this->autoStep();
$this->_assignCarrier();
$this->setTemplate(_PS_THEME_DIR_.'order-carrier.tpl');
break;
case OrderController::STEP_PAYMENT:
// Check that the conditions (so active) were accepted by the customer
$cgv = Tools::getValue('cgv') || $this->context->cookie->check_cgv;
if ($is_advanced_payment_api === false && Configuration::get('PS_CONDITIONS')
&& (!Validate::isBool($cgv) || $cgv == false)) {
Tools::redirect('index.php?controller=order&step=2');
}
if ($is_advanced_payment_api === false) {
Context::getContext()->cookie->check_cgv = true;
}
// Check the delivery option is set
if ($this->context->cart->isVirtualCart() === false) {
if (!Tools::getValue('delivery_option') && !Tools::getValue('id_carrier') && !$this->context->cart->delivery_option && !$this->context->cart->id_carrier) {
Tools::redirect('index.php?controller=order&step=2');
} elseif (!Tools::getValue('id_carrier') && !$this->context->cart->id_carrier) {
$deliveries_options = Tools::getValue('delivery_option');
if (!$deliveries_options) {
$deliveries_options = $this->context->cart->delivery_option;
}
foreach ($deliveries_options as $delivery_option) {
if (empty($delivery_option)) {
Tools::redirect('index.php?controller=order&step=2');
}
}
}
}
$this->autoStep();
// Bypass payment step if total is 0
if (($id_order = $this->_checkFreeOrder()) && $id_order) {
if ($this->context->customer->is_guest) {
$order = new Order((int)$id_order);
$email = $this->context->customer->email;
$this->context->customer->mylogout(); // If guest we clear the cookie for security reason
Tools::redirect('index.php?controller=guest-tracking&id_order='.urlencode($order->reference).'&email='.urlencode($email));
} else {
Tools::redirect('index.php?controller=history');
}
}
$this->_assignPayment();
if ($is_advanced_payment_api === true) {
$this->_assignAddress();
}
// assign some informations to display cart
$this->_assignSummaryInformations();
$this->setTemplate(_PS_THEME_DIR_.'order-payment.tpl');
break;
default:
$this->_assignSummaryInformations();
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
}
What if you cann this code after first case - break:
case OrderController::STEP_SUMMARY_EMPTY_CART:
$this->context->smarty->assign('empty', 1);
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
After this case add this case:
case OrderController::STEP_ADDRESSES:
$this->_assignAddress();
$this->processAddressFormat();
if (Tools::getValue('multi-shipping') == 1) {
$this->_assignSummaryInformations();
$this->context->smarty->assign('product_list', $this->context->cart->getProducts());
$this->setTemplate(_PS_THEME_DIR_.'order-address-multishipping.tpl');
} else {
$this->autoStep();
$this->_assignCarrier();
$this->setTemplate(_PS_THEME_DIR_.'order-carrier.tpl');
}
break;
Check, is it work.

My code breaks WooCommerce Multilingual Currency Switcher

WPML Woocommerce Multilingual does not support currency setting depending on user location, so we made our own code:
function geoIPLocator() {
global $woocommerce_wpml;
$currency='EUR';
$geo=new WC_Geolocation();
$geo->init();
$country=$geo::geolocate_ip($geo::get_ip_address() );
if(isset($_SESSION['locator'])) {
if($_SESSION['locator']['IP']= =$_SERVER['REMOTE_ADDR'] && strlen($_SESSION['locator']['IP' ])>0) {
$woocommerce_wpml->multi_currency_support->s et_client_currency($_SESSION['locator'][ 'currency']);
return;
}
}
if($country['country']=="RU" || $country['country']=="BY") {
$woocommerce_wpml->multi_currency_support->s et_client_currency('RUB');
$currency='RUB';
} else {
$woocommerce_wpml->multi_currency_support->s et_client_currency('EUR');
$currency='EUR';
}
$_SESSION['locator']=array("IP" =>$_SERVER['REMOTE_ADDR'], "ISO"=>$country['country'], "currency"=>$currency);
}
add_action( 'init', 'geoIPLocator', 5);
The problem is that it breaks regular currency switcher. When this code is on, we can't switch currency to any other. Is it because we don't save or check if a user already has some currency set up to him (like the manual switcher does)?
This is corrected code
function geoIPLocator() {
try {
global $woocommerce;
if ( isset($woocommerce) && gettype($woocommerce) == 'object' && property_exists($woocommerce,'session') && gettype($woocommerce->session) == 'object' && property_exists($woocommerce->session,'get') ) {
$manual_switch=$woocommerce->session->get('client_currency', null);
if ( $manual_switch == null ) {
global $woocommerce_wpml;
$currency='EUR';
$geo=new WC_Geolocation();
$geo->init();
$country=$geo::geolocate_ip($geo::get_ip_address());
if(isset($_SESSION['locator'])) {
if($_SESSION['locator']['IP']==$_SERVER['REMOTE_ADDR'] && strlen($_SESSION['locator']['IP'])>0) {
$woocommerce_wpml->multi_currency_support->set_client_currency($_SESSION['locator']['currency']);
return;
}
}
if($country['country']=="RU" || $country['country']=="BY") {
$woocommerce_wpml->multi_currency_support->set_client_currency('RUB');
$currency='RUB';
}
else {
$woocommerce_wpml->multi_currency_support->set_client_currency('EUR');
$currency='EUR';
}
$_SESSION['locator']=array("IP"=>$_SERVER['REMOTE_ADDR'], "ISO"=>$country['country'], "currency"=>$currency);
}
}
} catch (Exception $e) {
$e->getMessage();
}
}
add_action( 'init', 'geoIPLocator', 5);

Can visitors still send POST if they're not logged in?

I have a code that goes the following:
if($_SESSION["verified"] !== true) {
header('location:login.php');
exit();
}
else {
//do code...
if(isset($_POST["submit"])) {
//do stuff with code...
}
}
Do I need to specify in the 2nd if tag that it should only run if they're logged in?
if($_SESSION["verified"] !== true) {
header('location:login.php');
exit();
}
else {
//do code...
if(isset($_POST["submit"]) && $_SESSION["verified"] === true) {
//do stuff with code...
}
}
To prevent people from submitting a POST on a page they shouldn't actually be able to access?
Or is there some better way to do it?

How to add Buy Now button in cs-cart

I'm creating a buy-now button cs-cart add-on. I've created an add-on and I've made the add-to-cart functionality work. But cant redirect it to checkout page. I have used "fn_redirect("checkout")" function for the redirection. And used this function below this:
"fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);"
What should I do for redirection?
Edit:
My controller (buy_now.php)
if ($mode == 'add') {
if (empty($auth['user_id']) && Registry::get('settings.General.allow_anonymous_shopping') != 'allow_shopping')
{
return array(CONTROLLER_STATUS_REDIRECT, "auth.login_form?return_url=" . urlencode($_REQUEST['return_url']));
}
// Add to cart button was pressed for single product on advanced list
if (!empty($dispatch_extra)) {
if (empty($_REQUEST['product_data'][$dispatch_extra]['amount'])) {
$_REQUEST['product_data'][$dispatch_extra]['amount'] = 1;
}
foreach ($_REQUEST['product_data'] as $key => $data) {
if ($key != $dispatch_extra && $key != 'custom_files') {
unset($_REQUEST['product_data'][$key]);
}
}
}
$prev_cart_products = empty($cart['products']) ? array() : $cart['products'];
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
fn_save_cart_content($cart, $auth['user_id']);
$previous_state = md5(serialize($cart['products']));
$cart['change_cart_products'] = true;
fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);
if (md5(serialize($cart['products'])) != $previous_state && empty($cart['skip_notification'])) {
$product_cnt = 0;
$added_products = array();
if (!empty($added_products)) {
Registry::get('view')->assign('added_products', $added_products);
if (Registry::get('config.tweaks.disable_dhtml') && Registry::get('config.tweaks.redirect_to_cart')) {
Registry::get('view')->assign('continue_url', (!empty($_REQUEST['redirect_url']) && empty($_REQUEST['appearance']['details_page'])) ? $_REQUEST['redirect_url'] : $_SESSION['continue_url']);
}
fn_redirect(Registry::get('config.https_location') . "/checkout");
// $msg = Registry::get('view')->fetch('views/checkout/components/product_notification.tpl');
//fn_set_notification('I', __($product_cnt > 1 ? 'products_added_to_cart' : 'product_added_to_cart'), $msg, 'I');
$cart['recalculate'] = true;
} else {
fn_set_notification('N', __('notice'), __('product_in_cart'));
}
}
unset($cart['skip_notification']);
$_suffix = '.checkout';
}
`
hook template : add_to_cart.post.tpl
{$id = "buy_now_{$product.product_id}"}
<button id="opener_{$id}" name="dispatch[buy_now.add..{$product.product_id}]" class=" vs-button buynow_btn_">Buy Now</button>
I'd consider putting your fn_redirect("checkout") code into an if/else statement to see what's happening with it.
If you have this code:
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
Then surely you can put the redirection to the checkout page underneath it?
$redirect_url = "http://testcheckout.com?test=test"; //change this!
$errorMessage = "I should have redirected already....";
if (!empty($redirect_url)) {
//if it's not empty then it should redirect.
fn_redirect($redirect_url);
//Then put a die statement in here just to check it's not executing
//anything else and you can see if the redirect has a problem:
die(print_r($errorMessage, true ));
}
else {
//otherwise there is a problem with the supplied redirect url (empty)
die(print_r($redirect_url, true ));
}
I'd also take a look at this page which might help more:
http://forum.cs-cart.com/topic/6873-direct-buy-button/
Hope that helps!

Categories