Magento: add product automatically via observer - magento-1.9

I'm trying to add a product automatically via observer when a given one is added to the cart.
I've searched but can only find
adding products to the cart automatically via a checkbox and
adding to cart for all products selected automatically.
Can someone give me an example of say, productID == 123 added manually then productID == 333 is added automatically to cart via observer?

In your config.xml
<events>
<checkout_cart_product_add_after>
<observers>
<your_module>
<class>your_module/observer</class>
<method>cartProductAddAfter</method>
</your_module>
</observers>
</checkout_cart_product_add_after>
</events>
Then in your Observer.php
public function cartProductAddAfter($observer)
{
$product = $observer->getEvent()->getProduct();
if ($product->getId() == 123) {
$productIdToAdd = 333;
$qty = 1;
$productToAdd = Mage::getModel('catalog/product')->load($productIdToAdd);
$this->_addProductToCart($productToAdd, $qty);
}
}

Related

Add contacts to Google Contacts' main Contacts list (not hidden)

I'm using the GAS code bellow to add a new contact into Google Contacts (https://contacts.google.com/):
var contact = ContactsApp.createContact('Bbbb', 'Cccc', 'mymail#mails.com').addUrl(ContactsApp.Field.WORK_WEBSITE, 'https://sites.google.com/site/...');
The code works perfectly but for a single detail: it adds the new contact to a hidden list and not to the main or visible «Contacts» list.
I know it works because when I use the lookup box to search for the newly created contact it's there, in the «Other contacts» list. I need it to be created in the main «Contacts» list from the beginning, otherwise I would have to do it manually using the «Add to contacts» icon with every contact created (I'm planning to add some thousands of contacts.)
Thanks.
For me, I have to add them to the ContactGroup named System Group: My Contacts.
function finishAddingContact(contact) {
var mainGroup = ContactsApp.getContactGroup("System Group: My Contacts");
if(mainGroup)
mainGroup.addContact(contact);
}
Note that getContactGroup(string) is an exact name match.
I recommend inspecting your ContactGroups for system groups, and then selecting the appropriate one from that list. It's probably the one with the most contacts in it:
function inspect() {
var groups = ContactsApp.getContactGroups();
for(var g = 0; g < groups.length; ++g) {
if(groups[g].isSystemGroup()) {
Logger.log(groups[g].getName() + ": " + groups[g].getContacts().length + " members, id=" + groups[g].getId());
}
}
}
Thanks a lot tehhwch.
I used your second code to get the right group ("System Group: My Contacts"), and then:
var contact = ContactsApp.createContact('Wunder', 'Bar', 'excellent#help.com');
var group = ContactsApp.getContactGroup("System Group: My Contacts");
group.addContact(contact);
It works, and the contact is immediately visible in my Android device.

woocommerce wordpress billing form update data

what happened is when already registered user come to checkout page and fill billing form. The data update the current user data.
for example first name must go to billing_first_name but it's update first_name also with the same value.
I want it to update billing_first_name only
I want to reverse this process:
add_action('woocommerce_checkout_order_processed', 'custom_process_order1', 10, 1);
function custom_process_order1($order_id) {
    $current_user = wp_get_current_user();
    $current_user_id = get_current_user_id();
    update_user_meta($current_user_id, "first_name", $current_user->billing_first_name);
    update_user_meta($current_user_id, "last_name", $current_user->billing_last_name);
}
here it's update first name with the value of billing_first_name and this is what I want to prevent.
how can I do that ?
use correct meta field name,
add_action('woocommerce_checkout_order_processed', 'custom_process_order1', 10, 1);
function custom_process_order1($order_id) {
    $current_user = wp_get_current_user();
    $current_user_id = get_current_user_id();
    update_user_meta($current_user_id, "billing_first_name", $current_user->billing_first_name);
    update_user_meta($current_user_id, "billing_last_name", $current_user->billing_last_name);
}
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
on this page, you can get correct field names

BigCommerce Stencil - Product Variant Stock Levels

A client wants to set up A/B testing on the Product Detail Page related to the stock_level of a product's variants. Once the user selects their options, if the quantity is less than 5, I'd show something like "Hurry, only 3 more in stock"...
I believe I have the correct Inventory settings enabled, because I can retrieve the stock_level of a product without options.
Has anyone had success pulling variant SKU stock_levels in stencil?
Thanks
This can be done using javascript in the assets/js/theme/common/product-details.js file. On initial page load and each time a product option is changed, there is a function updateView(data) that is called. The data parameter contains all the info you need for the selected variation.
Starting on line 285, replace this:
updateView(data) {
const viewModel = this.getViewModel(this.$scope);
this.showMessageBox(data.stock_message || data.purchasing_message);
with this:
updateView(data) {
const viewModel = this.getViewModel(this.$scope);
if(data.stock < "5") {
data.stock_message = "Hurry, only " + data.stock + " left!";
}
this.showMessageBox(data.stock_message || data.purchasing_message);

Retrieve all rows from table except few rows in laravel

I am using Laravel 5.1 & MySQL as backend to serve REST-API requests made from the mobile app.
I have a Cart table and Items table. So whenever user comes to 'Items screen' in his mobile App, In the backend I should perform 2 tasks.
First check if any Items are there in his cart. If yes (i.e.,there are items in Cart table), then fetch those items from Cart table and the remaining items from Item table.
If there are no items in Cart, then I will easily fetch all items from the Items table and show it to user.
I am stuck not being able to perform the task 1. Because I am first retrieving all the item_ids from the Cart table. It will return a collection. Now I should check if these item_ids(from cart table) are present in Items table. If yes, don't fetch those items from Items table, BUT fetch all other items from Items table. Combine those items from Items table & items from Cart table and show it to user. How can I achieve this?
Currently, problem is, I am getting all 'item_ids' from Cart table. It returns a collection of item-ids. Using foreach() loop, for every item_id, I am querying Items table as follows:
("*where('item_id','!=',$getItemId)->get();*")
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('item_id','!=',$getItemId)->get();
And it returns collection checking against each individual item. and replaces collection with every new iteration in foreach loop.
Here is the function in my CartController:
public function getItemsWithCartItems($uuid,$store_id,$category_id,$subcategory_id,$subsubcategory_id=null)
{
try
{
$getCartItems = UserCartDetailBng::where('uuid','=',$uuid)->get();
if($getCartItems->isEmpty()) //Performing task 2. No problem here.
{
if($subsubcategory_id==null || $subsubcategory_id==0) // Bcoz, subsubcategory_id is optional
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store Item not found");
}
}
else
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('subsubcategory_id','=',$subsubcategory_id)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store Item not found");
}
}
$count = $itemDetails->count();
$data = array('count'=>$count,'result'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}
else //Performing task 1. Here is the problem.
{
// I am using "where('item_id','!=',$getItemId)->get()" And it returns collection checking against each individual item. and replaces collection with every new iteration in foreach loop.
foreach($getCartItems as $getCartItem)
{
$getItemId = $getCartItem->id;
if($subsubcategory_id==null || $subsubcategory_id==0)
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('item_id','!=',$getItemId)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store items not found");
}
}
else
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('subsubcategory_id','=',$subsubcategory_id)->where('item_id','!=',$getItemId)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store items not found");
}
}
$count = $itemDetails->count();
$data = array('count'=>$count,'result'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}
}
}
please help me solve this problem, TIA.
Considering your models are set correctly, try this code
//get the cart items with the item
$cartItems = UserCartDetailBng::where('uuid','=',$uuid)->with('itemsBng')->get();
if($cartItems->isEmpty())
{
//
}
else
{
//find items excluding of cart items
$itemDetails = ItemBng::where('store_id','=',$store_id)
->where('category_id','=',$category_id)
->where('subcategory_id','=',$subcategory_id)
->whereNotIn('id', $cartItems->lists('item_id'))->get();
$data = array('cartItems'=>$cartItems,'generalItems'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}
Consider adding the following to your code
Add this after try
$getCartItemsIds = UserCartDetailBng::where('uuid','=',$uuid)->lists('id');
Remove the foreach block and $getItemId from the else statement then have the $itemdetails retrieved using
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->whereNotIn($getCartItemsIds)->get();
please note you have two different retrievals for $itemDetails edit the second one to also use the ->whereNotIn($getCartItemsIds)
Lastly as per your intention you should edit the $data returned to include also what is on the cart.
$data = array('count'=>$count,'result'=>$itemDetails, 'cart'=>$getCartItems);

Get information from checkbox for creating groups

I must create a web app with angularjs.
I must create a web app where I have the possibility to create groups of athlete. I must store them in localstorage. The elements of the athletes which I must store are the id. How can I do this?
Someone can help me?
I have this code in controller:
$scope.selection=[];
// toggle selection for a given employee by name
$scope.toggleSelection = function toggleSelection(id_athlete) {
var idx = $scope.selection.indexOf(id_athlete);
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
$scope.selection.push(id_athlete);
}
window.localStorage['$scope.selection'] = JSON.stringify($scope.selection);
$scope.selection = JSON.parse(window.localStorage['$scope.selection'] || '{}');
the problem of this part is that in this way I don't see the id but I only see the name but it's not important.
The html code is :
<input ng-hide="see" id="group.name" type="checkbox" ng-checked="selection.indexOf(group.id) >" ng-click="toggleSelection(group.id)" />
<!-- -->
<button ng-hide="see" ng-click="save(group.id)" >Salva</button>
Ok now how can I create the group?
Please help me