PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label shopping-cart. Show all posts
Showing posts with label shopping-cart. Show all posts

Thursday, November 10, 2022

[FIXED] How do I mark products that match shopping cart price rules

 November 10, 2022     magento, magento-1.7, php, shopping-cart     No comments   

Issue

I am using shopping cart price rules in my custom module, every thing is working fine I just want to get product ids against every rule. If there are 10 products in cart and on 3 of them had some rules applied, rule #1 on 2 products and rule #2 on 1 product. How can I mark the products ids against every rule?


Solution

Just change process function in Validator.php to

public function process($_quote) {
    $i               = 0;
    $quote           = $_quote;
    $customerSession = Mage::getSingleton('customer/session');
    foreach ($this->_rules as $rule) {
        // already tried to validate and failed
        if ($rule->getIsValid() === false) {
            continue;
        }
        if ($rule->getIsValid() !== true) { 
            $rule->afterLoad();
            if (!$rule->validate($quote)) { // quote does not meet rule's conditions , //Call Found.php
                $rule->setIsValid(false);
                continue;
            }
            $rule->setIsValid(true); // passed all validations, remember to be valid
        }

        $this->_appliedProductsIds[]                 =  Mage::getSingleton('checkout/session')->getReturnProductRuleValues();
        $this->_appliedProductsIds[$i]['program_id'] =  $rule->getProgramId();
        Mage::getSingleton('checkout/session')->unsReturnProductRuleValues($this->_ReturnValues);
        $i = $i + 1;
    }
    return $this;
}

And validate function in Found.php to

public function validate(Varien_Object $object) { 
//Called form Validator.php
    $all       = $this->getAggregator() === 'all';
    $true      = (bool)$this->getValue();
    $found     = false;

$Count =  count($object->getAllItems()); 
$i = 0;
    foreach ($object->getAllItems() as $item) {
        $found = $all ? true : false;
        foreach ($this->getConditions() as $cond) {

            $validated = $cond->validate($item); // Call to Product.php's function 'validate'

    if($validated) {
        $this->_ProductId[] = $item->getProductId();
    }

    if($i == $Count) {
        if ($all && !$validated) {
        $found = false;
        break;
        } elseif (!$all && $validated) {
        $found = true;
        break 2;
        }
    }

        }
    if($i == $Count) {
    if ($found && $true) {
        break;
    }
    }
    $i = $i + 1;
    }

$this->_ReturnValues['Product_Id'] = $this->_ProductId;
if(!empty($this->_ProductId)) {
    $this->_ReturnValues['Bool'] = true;
} else {
    $this->_ReturnValues['Bool'] = false;
}

    if ($found && $true) {
        // found an item and we're looking for existing one
    Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
        return true;
    } elseif (!$found && !$true) {
        // not found and we're making sure it doesn't exist
    Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
        return true;
    }
Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
    return false;
}

And access the values in Observer with Vadidator class object like

$validator = Mage::getModel('modulename/validator')
               ->init($customer->getWebsiteId(), $customerGroupId);

$v                   = $validator->process($quote);
$_appliedProductsIds = $v->_appliedProductsIds;

Hop this will help someone..



Answered By - Ahmed
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] When does Magento consider a cart to be abandoned? Where's that time limit set?

 November 10, 2022     magento, magento-1.7, shopping-cart     No comments   

Issue

If left to default settings, what circumstances would have to occur for Magento 1.7 to consider a cart abandoned? Where is the code that makes this determination located?

I know that this is probably set somewhere in the quote, but I can't find it for the life of me.

I've done some Googling, but like so many Magento questions I'm left empty handed. Thanks!


Solution

As you probably know the setting is located here:

Admin => system => Configuration => Sales => Checkout => Quote Lifetime (days)

This will add the setting to the database (core_config_data table) with path:

checkout/cart/delete_quote_after

This path is used in the code on:

app/code/core/Mage/Sales/Model/Observer.php line 54

So when someone is adding something to a cart it will be updated. When a customer logs in and his cart is there it will be updated. When a cart is not updated for the last 30 days. It will be removed.

Extra information:

In case you wonder when this code is used, It is used by the cronjob of magento.

check: App/code/core/Mage/Sales/etc/config.xml line 1732

<crontab>
    <jobs>
        <sales_clean_quotes>
            <schedule>
                <cron_expr>0 0 * * *</cron_expr>
            </schedule>
            <run>
                <model>sales/observer::cleanExpiredQuotes</model>
            </run>
        </sales_clean_quotes>

Hope this helps.



Answered By - khoekman
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, March 11, 2022

[FIXED] Searching items in cart using laravel crinsane cart

 March 11, 2022     laravel, laravel-5, php, shopping-cart     No comments   

Issue

Am trying to get an item from a cart using Crinsane Larvel Shopping Cart. The documentation talks about using a closure. I do not understand the example provided.

I am using Laravel 5.3, am trying to search items from the shopping cart using information from the Request object. This is my code :

View

@foreach($cart as $item)
<td>
    <div>
        <a href='{{url("cart?id=$item->id&add=1")}}'> + </a>
        <input type="text" name="qty" value="{{$item->qty}}" autocomplete="off" size="2">
        <a href='{{url("cart?id=$item->id&minus=1")}}'> - </a>
    </div>
</td>
@endforeach

Route

Route::get('/cart', 'CartController@getCart');

Controller

public function getCart(){

    //increment the quantity
    if (Request::get('id') && (Request::get('add')) == 1) {

        $inputItem = Request::get('id');
        //$rowId = Cart::search(['id' => Request::get('id')]);
        $cartItem = Cart::get($rowId);
        Cart::update($rowId, $cartItem->qty+1);
    }

    //decrease the quantity
    if (Request::get('id') && (Request::get('minus')) == 1) {

        $inputItem = Request::get('id');
        //$rowId = Cart::search(['id' => Request::get('id')]);
        $cartItem = Cart::get($rowId);
        Cart::update($rowId, $cartItem->qty-1);
    }

}

Cart Content Collection

`Collection {#318 ▼
  #items: array:2 [▼
    "027c91341fd5cf4d2579b49c4b6a90da" => CartItem {#319 ▼
      +rowId: "027c91341fd5cf4d2579b49c4b6a90da"
      +id: "1"
      +qty: 1
      +name: "Banana"
      +price: 35.0
      +options: CartItemOptions {#320 ▼
        #items: []
      }
      -associatedModel: null
      -taxRate: 21
    }
    "370d08585360f5c568b18d1f2e4ca1df" => CartItem {#321 ▼
      +rowId: "370d08585360f5c568b18d1f2e4ca1df"
      +id: "2"
      +qty: 7
      +name: "Melon"
      +price: 64.0
      +options: CartItemOptions {#322 ▼
        #items: []
      }
      -associatedModel: null
      -taxRate: 21
    }
  ]
}`

How can I use Cart::search() to find the Cart item with id 1 (banana)?


Solution

You can do it just like this:

Cart::search(function($cartItem, $rowId) {
    return $cartItem->id == 1;
});

The search method is a wrapper for Laravel's filter method on collections.

A Closure is simply a function passed to a function that may or may not receive variables as arguments from the parent function.

If you want to use the $request variable inside of this Closure you will have to use() it:

Cart::search(function($cartItem, rowId) use($request) {
    return $cartItem->id == $request->id;
});

Of course make sure that $request->has('id') before you attempt to access that property.



Answered By - Ohgodwhy
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, January 25, 2022

[FIXED] CakePHP 3 : Cookies tutorial

 January 25, 2022     cakephp, cakephp-3.2, cookies, e-commerce, shopping-cart     No comments   

Issue

Is there any good tutorial on CakePHP 3 cookie component.

I'm new to CakePHP and Cookie as well. I am writing an e-commerceapplication in CakePHP 3.2 and want to used Cookie to build shopping cart which can store item name, quantity, and price.

Is there any good tutorial you can suggest to get hand on Cookie component of CakePHP.

I followed CakePHP documentation but I can only get about how to read and write to cookie.


Solution

You can read more about cookies and how to use them in PHP here, for specific information about using cookies in CakePHP the CakePHP cookbook provides the best information.

Hope it helps



Answered By - TrueStory
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing