How to get total weight of cart items in BigCommerce - javascript

I am currently using this graphQL query within the cart to retrieve each products weight
` query productWeights {
site {
products (entityIds: [${productIDs}]) {
edges {
node {
name
sku
weight {
value
}
}
}
}
}
}`
This brings back the weight of each individual item so I now need to calculate the weight of the amount of items in the cart. How can I get the quantity of items within this query to enable a calculation of the total weight added to cart?

Related

Ruby script to calculate discount using Shopify Scripts

I am using a script to calculate an addtional 5% off when the cart total is greater than $1000, but the variant price is already discounted by 25% and has a was/now pricing setup that is $29.95 was $39.95.
The problem is - i need for the script to discount the orginal item value of $39.95, not the discounted rate which is the new retail price.
Here is the script - i have tried to add 25% back to the line item price without success and there is no line item method that Shopify provides to use the orginal cart line item price in the dev docs - https://help.shopify.com/en/manual/checkout-settings/script-editor/shopify-scripts#line-item-methods
# Tiered Discounts by Spend Threshold 5% off when you spend $1000 or more
min_discount_order_amount_extra = Money.new(cents:100) * 1000 #number of dollars needed in cart to get the discount
DISCOUNT_PERCENT = 25
# calculate the discount to be applied
percent = Decimal.new(DISCOUNT_PERCENT/100) + 1
total = Input.cart.subtotal_price_was * percent
discount = if total > min_discount_order_amount_extra
0.05 #discount percentage in decimal form
else
0
end
message = "Another 5% saved"
# 30% off products excluding commercial items
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
next if line_item.variant.product.tags.any?{|x| ['commercial'].include?(x)};
line_item.change_line_price(line_item.line_price * (1-discount), message: message) unless discount == 0
end
Output.cart = Input.cart

How to subtract quantity from the equipment table when there's a new borrow transaction made?

I'm a Laravel newbie and I recently started working on a equipment management system whereby the admin adds quantity stock one each equipment to the system and when admin let the user borrow the equipment it deducts the quantity that the user wants from the equipment stock table. My only challenge is how to make the system to subtract quantity that the borrower wants from the equipment stock table?
Here's my controller for the button "borrow"
So when I click the borrow button it will deducts the ('quantity_item') to the equipment stock table ('e_quantity')
public function borrow($id){
$first = Reservation::where('id', $id)->first();
$kl = $first->name;
$mn = $first->Name_item;
$st = $first->quantity_item; //this is the quantity that the borrower wants
$op = $first->dt_item;
$qr = $first->room_item;
$first->delete();
$second = new BorrowedItems();
$second->bname = $kl;
$second->bdate = $mn;
$second->itemb = $op;
$second->bquantity = $st;
$second->broom = $qr;
$second->save();
return redirect()->back()->with('message','Item Borrowed Successfully!');
}
This is my Equipment Stock Table
This is my BorrowedItem Table
so when I click the borrow button the data from reservationtable will transfer here
This is my Reservation Table

Displaying 2 custom Data attribute items within a div. Ex: Currency & Amount

I have an E-Commerce website built on HTML, JavaScript & PHP.
On product details page user can add product to cart thus I'm displaying total amount of cart value.
I want to display decimal number always (10,2) format.
Currently my code works with minimal thing. On clicking "Add to cart" if product price is 12.00 the Counter div displays 12 only.
<span>
<a href="cart.php" class="text-white">
<i class="fa fa-shopping-cart p1" data-count="10" data-currency="€" id="total"></i>
</a>
</span>
.p1[data-count]:after{
position:absolute;
right:1%;
top:1%;
content: attr(data-count);
font-size:10px;
padding:.2em;
line-height:1em;
color: white;
background:#21468b;
text-align:center;
width: 100%;
font-weight:normal;
}
<script>
var theTotal = '0';
var ProductSellingPrice = '12.00'
$('#insert').click(function(){
theTotal = Number(theTotal) + Number(ProductSellingPrice);
$('#total').attr('data-count', theTotal);
});
</script>
So on clicking insert, the existing TheTotal & current product Price gets added. If there are no products on cart then p1 doesn't display any value, thus want to display zero always if empty/zero. If product price is 12.00 then shows 12 only. If product price is 12.50 then 12.50 is displayed.
I want it to display decimal always & also currency symbol using data attribute.
Displaying decimal problem is solved by #Simone, i m not able to find answer for displaying currency before value using data attribute.
If you want 12.00 and not 12 you have to use Number.prototype.toFixed()
So you have to convert all the single product total (quantity * price is the single total ) into Float number and when you do the total sum, take the number and do this:
Number.parseFloat(total).toFixed(2); // two decimal
Example:
var quantity = 10;
var price = 11;
var tot = parseFloat(quantity * price).toFixed(2);
console.log(tot); // "110.00"

Communication between Angular components and backend services

I have a backend API written in C#, and my Angular App has a service layer of angular components between this API and the visual components. 3 components in particular are using a shopping cart component service. They are:
A shopping cart icon on a navmenu component - it shows a count of the number of items in the cart.
A cart summary page - shows items in the cart and allows you to increase/decrease the qty of an item or delete it from the cart entirely using the quantity component (3).
Quantity object. This is attached to a product page as well as line items in the cart. It can increase or decrease the number of items in the cart for the product page the component sits on.
The exposed methods for the cart service are:
//Add item to cart or increment qty of item by 1
addToCart(product: Product): Observable<ShoppingCart> { ... }
//Decrement qty of item by 1 or delete if qty = 0
removeFromCart(product: Product): Observable<ShoppingCart>{.... }
//Get all items in cart
getCartItems(): Observable<ShoppingCart>{ ...}
// Delete all items in cart
async clearCart(): Promise<Observable<ShoppingCart>>{ ... }
// Delete entire cart
async removeCart(): Promise<void> { ... }
Because the service is detached from the API and injected into each of the components separately (product component, quantity component and navmenu component), clicking the plus() or minus (-) button on the quantity component is updating the quantity object, but the navmenu is not getting updated as the numbers change.
For instance, I have this on my cart component,
<ng-container *ngIf="cart$ | async as cart">
<div class="card">
<div class="card-body">
<h6 class="card-subtitle card-mb-4 card-tb-4 text-muted">
You have {{cart.itemCount}} items in your cart.
with the following code in the .ts file
ngOnInit() {
this.getCart();
}
async getCart() {
this.cart$ = await this.cartSvc.getCartItems();
}
The navmenu component has this code in the page
<li class="nav-item">
<a class="nav-link" routerLink="/cart">
<span class='fa fa-shopping-cart'></span>
<span *ngIf="cart$ | async as cart">
<span class="badge badge-warning badge-pill" *ngIf="cart.itemCount">{{ cart.itemCount }}</span>
</span>
</a>
</li>
with this code in the .ts file, my intent being that the number of items should increase in the badge-pill when I clicked the + on a product to add it to the cart.
async ngOnInit() {
// No need to unsubscribe as only 1 instance in DOM for lifetime of application
this.cart$ = this.cartSvc.getCartItems();
}
I passed Observable as return results in all my cart functions to update the local variables in the quantity component and the product component - this is causing them to update and re-render properly, but the update never bubbles up to affect the navmenu, so the only way I can get the quantity there to update is to refresh the page.
Why does the cart count, an observable I am subscribing to, not update when I add items to the cart? Forgive my ignorance, I am new to Angular and still trying to get a handle on all its features.
Thanks in advance!
Your service should have a behavior subject for the cart and then the methods on the service should be updating the data in the cart. You should not be returning observables from the update methods.
cart$ = new BehaviorSubject<ShoppingCart>({ products: [] });
addToCart(product: Product) {
const cart = this.cart$.value;
this.cart$.next({ ...cart, products: [ ...cart.products, product] });
}
removeFromCart(product: Product) {
const cart = this.cart$.value;
this.cart$.next({ ...cart, products: cart.products.filter(p => p.id !== product.id) });
}
clearCart() {
const cart = this.cart$.value;
this.cart$.next({ ...cart, products: [] });
}
and in your component
cart$ = this.cartSvc.cart$;

Display Table Values Based on Dropdown Selection

I have a dropdown list that is imported with data from a table in a database. The dropdown values consist of a value MR_ID that is concatenated with MR_Name that is located in a different table. Depending on what is selected, I want to display a table that shows the necessary information that correlates to what was selected in the dropdown list. So, for example, if I select "1 - Company A" in the dropdown list, I want the Supp_ID values (can be more than 1 value) that correlate to the "1 - Company A" value to be displayed in a table. How can I do this?
The table is only 2 columns, MR_ID (which is what is displayed in the dropdown list) and Supp_ID.
I had this working before but I had to change my queries to use CTE instead and it threw me off.
Here were my original queries on my main page and also the query that the ajax calls...
// Query on main page
$sql = "SELECT DISTINCT CAST(MR_ID AS INT) AS MR_ID FROM Stage_Rebate_Index";
// Query that is used in ajax call (test-table.php)
$sql_one = "SELECT CAST(Supp_ID AS INT) AS Supp_ID, CAST(MR_ID AS INT) AS MR_ID FROM Stage_Rebate_Index WHERE MR_ID = '$mr_id'";
Here are my new queries using CTE that I cant figure out how to get them working...
// Query on main page
$sql = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID
, Stage_Rebate_Index.MR_ID AS sort_column
FROM Stage_Rebate_Index
LEFT JOIN Stage_Rebate_Master
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID
)
SELECT MR_ID
FROM
cte
ORDER BY
sort_column;";
// Query that is used in ajax call (test-table.php)
$sql_one = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID
, Stage_Rebate_Index.MR_ID AS sort_column, CAST(Stage_Rebate_Index.Supp_ID as INT) AS Supp_ID
FROM Stage_Rebate_Index
LEFT JOIN Stage_Rebate_Master
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID
)
SELECT MR_ID, Supp_ID
FROM
cte
WHERE
MR_ID = '$mr_id'
ORDER BY
sort_column;";
This is my ajax along with a line of code that brings in the $_POST value...
// Reads what the user selects from the drop down list and displays table when a selection is made
function updatetable(myForm) {
function show() { document.getElementById('index-table').style.display = 'block'; }
var selIndex = myForm.selectedIndex;
console.log();
var selName = $( "#mr_id option:selected" ).text();
// Ajax sends POST method to Stage_Rebate_Index table and pulls information based on drop down selection
$.ajax ({
url: "test-table.php",
method: "POST", //can be post or get, up to you
data: {
mr_id : selName
},
beforeSend: function () {
//Might want to delete table and put a loading screen, otherwise ignore this
},
success: function(data){
$("#table_div").html(data); // table_div is the div you're going to put the table into, and 'data' is the table itself.
}
});
}
$_POST method brought in on same script as the query that is used in ajax call...
$mr_id = $_POST['mr_id'];

Categories