Calculate quantity based on different text values - php

Good day
I'd be grateful for some expertise with this issue of mine.
Assume we have the following variables in a hypothetical situation:
$uom = (either "PLT" or "CRT")
$prow = item ID in cart (starts from 1 and goes up depending on how many items)
$quantity = quantity of each item
$Name = item name
$quantityplt = new variable for PLT quantity
$quantitycrt = new variable for CRT quantity
This information is acquired for each product from a cart shopping system on the website. So once the user adds an item to the cart, that information is displayed for each item in the cart that they chose.
I have two text inputs which need to display the total_quantity for all the items: depending on whether or not the $uom is "CRT" or "PLT".
<input type="text" id="plt_sum" readonly/>
<input type="text" id="crt_sum" readonly/>
Now, I need to calculate the $quantity depending on $uom.
It must then display the value in either "#plt_sum" or "#crt_sum"...
I'm not confident in using loops so I don't really know how to:
<?php
foreach($prow){
if($uom=="PLT"){
$quantityplt value == (perform $quantity addition WHERE $uom is "PLT");
echo $quantityplt (in the #plt_sum text input);
}
elseif($uom=="CRT"){
$quantitycrt value == (perform $quantity addition WHERE $uom is "CRT");
echo $quantitycrt (in the #crt_sum text input);
}
}
?>
I do hope this piece of php makes any sense at all. I realise my syntax and everything else is most likely wrong. I'm still very new to it and learning things every day.
Thank you for reading and I appreciate any help.

Related

How to add Custom option value on coupon rules Magneto 2

How to add Custom option value on coupon rules Magneto 2. I have a product which have some custom option like size, color etc. with different value, I want give discount on basic of size = 7'. I don't want use sku custom option.. Let me know how I fix it.
https://github.com/Turiknox/magento2-custom-total
Moulde I use for Discount but i have face a problem.
one Data I can't get cart information
$obj = $bootstrap->getObjectManager();
// Set the state (not sure if this is neccessary)
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
// Getting the object managers dependencies
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote();
$helper = $obj->get('\Magento\Checkout\Helper\Cart');
// Get quote and cart items collection
$quote = $helper->getQuote();
$quoteitems = $quote->getAllItems();
$coupon_code = 'Test23';
// Get cart contents
$cart= $helper->getCart();
foreach ($quoteitems as $item)
{
}
It goes to the infinity loop.
This one Run fast that's why here we can't load quote total, For Discount given you must set session core cookies which allow giving discount...

Exclude several shipping classes from a fee based on cart items dimensions in Woocommerce

I've been getting great help here during the year, regarding a woocommerce issue where I needed an automatic fee added based on total cart height in this thread and then later updated with width in this thread.
Now my only issue is that I need to exclude some shipping classes from the function, as they are shipped differently and don't need a Bulky fee added.
I've been playing around with the answer to "Add a fee based on cart items dimensions in Woocommerce", trying to add a class variable to it:
// Initializing variables
$total_height = 0;
$class = 1390,1392,1389;
$apply_fee = false;
But it instantly breaks the website. I've been searching on stackoverflow.com and Google for a way to do this but no luck, and I'm still not qualified enough for that type of advanced editing of code.
Appreciate any help I can get.
You can't have a list of ids like that with just commas between them. To fix this put the ids in an array. Within the foreach loop, check the shipping class id of the product, and if it's in the array exclude it.
$exclClasses = array(1390,1392,1389);
// Checking item with
if ( $cart_item['data']->get_width() > $width_threshold ) {
$apply_fee = true;
}
// make sure product isn't in excluded shipping classes
if (in_array( $cart_item['data']->get_shipping_class_id(), $exclClasses))
{
$apply_fee = false;
}
or to use 1 statement instead of 2
if ( $cart_item['data']->get_width() > $width_threshold && !in_array( $cart_item['data']->get_shipping_class_id(), $exclClasses)) {
$apply_fee = true;
}

Multiple inputs with names that represent a product id

I'm developing an order system in Laravel and I encountered a problem when trying to process the products of the order.
The page looks like this:
Each Product entry represents an item in a table called Products that has id,name and price as columns.
When I'm sending the form , each QTY input has as name the product's id(for Cabbage is 1 , for Water is 3, for Bread is 4 ).
Something like this(for cabbage):
<input style="width:70px;margin-bottom:9px;" class="qty form-control input-lg" required="required" name="1" type="number" value="">
One of the problems is that those aren't the single inputs on the page and I can't really identify them from the rest.
When the form is sent I would want to take those values , search in the db , get the name of each product based on id and make a simple list like:
Cabbage - 2 qty
Water - 1 qty
Bread - 3 qty
What I'm asking is what would be the best way to get the list above as the products will be dynamic(removed , added , modified , etc).
Initially I thought of something like:
//all inputs with name products[] and as value the product id
$items = Input::get('products');
foreach($items as $item){
$prodid = Product::find($item);
echo $prodid['name'];
}
But doing so I wouldn't know the qty value , I'm pretty much stuck.
I appreciate any ideas that would help me achieve what I explained.
Thank you!
You could sent the quantities also as an array, so name you qte inputs like quantities[], then get them by index in the controller :
$product_ids = Input::get('products'); //input name products[]
$quantities = Input::get('quantities'); //input name quantities[]
foreach($product_ids as $index=>$product_id){
$product = Product::find($product_id);
$quantity = $quantities[$index];
echo $product['name'].' : '.$quantity;
}

return woocommerce item names from cart only if they are a certain variation

I'm more or less a total newbie to php. My goal is to get the names of items from the logged in user's cart and dynamically populate a GravityForms template with the information. I've successfully managed to do that with the code below, but there are three things that I'm failing to do. 1: I only want to populate the form with all items of a certain variation. 2: the echo function will list all of the item names, but not inside the relevant field, and the return function will populate the field, but only with the first item name. 3: I'd like to have the output items listed with some form of separator in between each item name. Here's what I have so far:
<?php
add_filter( 'gform_field_value_beat_names', 'beat_names_function' );
function beat_names_function( $value ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach ($items as $item) {
$product_variation_id = $item['variation_id'];
$license_id = new WC_Product($product_variation_id);
$license_string = $license_id->get_formatted_name();
$beat_id = $item['data']->post;
$beat_name = $beat_id->post_title;
if (strpos($license_string, 'basic') !== false) {
echo $beat_name;
}
}
}?>
As you can see, I'm attempting to use strpos to isolate the particular item variation I want to target, using a certain word found in the name of the variation option, being "basic". I'm sure there's a more secure way of doing that, but it works for now. The problem lies with the return function I set up inside the conditional strpos statement, being that it will still just return the entire list of cart items, as opposed to only the items that I'm trying to isolate with the strpos conditional.
The ultimate goal is to create a license agreement that is dynamically populated with the relevant information, including the names of the items being licensed. The item variations are different license options that I have available for the products in my store. The purpose of the above code is to filter cart items by license type so that the wrong item names don't get listed on the wrong license agreement at checkout.
Any tips would be appreciated
Figured it out after some more tinkering. I broke it down step by step to help anyone as clueless as I am
add_filter( 'gform_field_value_basic_beat_names', 'basic_beat_names_function' );
function basic_beat_names_function( $value ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
// the item names you want will be stored in this array
$license_check = array();
// Loop through cart items
foreach ($items as $item) {
// get the id number of each product variation in the cart. this won't work for "simple" products
$product_variation_id = $item['variation_id'];
// translate that id number into a string containing the name of the product, and the options applied to the product
$license_id = new WC_Product($product_variation_id);
$license_string = $license_id->get_formatted_name();
// check to see if the word "basic" is found anywhere in that string. "basic" is contained in the name of the product option being targeted
if (strpos($license_string, 'basic') !== false) {
// if the above condition is met, fetch the name of any products in the cart that have that option applied. The product name will be fetched from the title of the product's page
$beat_id = $item['data']->post;
$beat_name = $beat_id->post_title;
// store the retrieved product names in the $license_check array
$license_check [] = $beat_name;
}
}
// pull the stored product names from the $license_check array, and format them using implode and concatenation, then return the final result to the form field
return "\"" . implode("\", \"", $license_check) . "\"";
}
As a warning, the strpos method is a little hacky. It'll only work properly if your string is specific enough to target ONLY the option you're looking for.
As an example, here's the format for the product variation string being fed into the strpos function:
item-name-option-one-name-option-two-name – variation #xxx of item page title
So, if you want to filter items ONLY by option one, your safest bet would be to write the strpos function like so:
if (strpos($license_string, 'option-one-name') !== false) {
//do something
}
When all is said and done, the final result should look like: "item1", "item2", "item3", etc.
Then, to do the same with any other option, and output the result in a different field, or separate form altogether, I'll just duplicate this code, and replace any mention of the word "basic" with some different unique string contained in the other option. Don't forget to configure the gravity forms field as necessary too.

Get price of an order, by manufacturer or brand on MAGENTO

I am developing a module to receive commissions from companies that are selling in my Magento store. It is already working, but need to add a new feature that I'm not getting.
I need to add an extra value in the commission, if the application of any company is greater than "X"
I did in a way that did not work:
foreach ($order->getAllVisibleItems() as $key => $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$price = $item->getPrice();
$valor_comissao = (int)$product->getAttributeText('comissao_vendedor')/100;
$comissao_valor2= $price-($price*$valor_comissao);
$qty = $item->getQtyOrdered();
$valor_pedido= ($price*$qty);
if($valor_pedido > 150) {
$comissao_valor= ($comissao_valor2*$qty-17);
} else {
$comissao_valor=($comissao_valor2*$qty-2);
}
$comissoes[] = array (
'Razao' => 'Produto '.$item->getName().' id '.$item->getProductId().' x '.$item->getQtyToInvoice().' sku: '. $item->getSku(),
'Login' => $product->getAttributeText('login'),
'ValorFixo' => $comissao_valor
);
}
then only if the product go from $ 150.00 the value is added and if you have two selections from costing $ 100.00, the value is not added. I need the value to be added if the entire order of the brand exceeds $ 150.00.
The logic that I'm trying to follow is as follows:
Taking the total value of the order
Filter by tags, ie total value of the order by brand
If the product is a brand that exceeded "x" and she has not yet received the additional value, add shipping
I wonder if someone has gone through something similar and could help me.
Grateful.
Taking the total value of the order
You can retrieve the total of the order via:
$order->getGrandTotal()
Filter by tags, ie total value of the order by brand
I would recommend using Magento's resource model object and building a query on it.
This post shows how this can be done: https://stackoverflow.com/a/5139391/3784145
note
Mage::getModel('catalog/product')->getCollection()
can be used instead of
Mage::getResourceModel('sales/order_collection')
Explanation on the difference between Model and Resource:
https://magento.stackexchange.com/a/4579
If the product is a brand that exceeded "x" and she has not yet received the additional value, add shipping
I don't really know what you mean with: If the product is a brand that exceeded "x"
But I think this would require another collection query.

Categories