Send payment gateway woocommerce producct description next to product name - php

I want to send the
function getProductsJson($order,$options) {
$stupid_mode = $options['stupid_mode'];
$items=$order->get_items();
$arr = [];
foreach ($items as $item) {
$data = $item->get_data();
$arr[] = [
'productSKU' => $data['product_id'],
'description' => $data['name'],
'quantity' => $data['quantity'],
'price' => $data['total'] / $data['quantity'],
];
}
}
Here in this part:
'description' => $data['name'],
I want it to send product description next to product name as example below:
Product Name - Product Description
How can achieve this ?
Best Regards

If you need product description saved in $content, you can get this by Product_ID, and then add this to your array:
function getProductsJson($order,$options) {
$stupid_mode = $options['stupid_mode'];
$items=$order->get_items();
$arr = [];
foreach ( $items as $item ) {
$data = $item->get_data();
$product_description = get_post( $data['product_id'] )->post_content;
$arr[] = [
'productSKU' => $data['product_id'],
'name' => $data['name'],
'description' => $product_description,
'quantity' => $data['quantity'],
'price' => $data['total'] / $data['quantity'],
];
}
}
Or if you need built-in woocommerce product description, try this:
function getProductsJson($order,$options) {
$stupid_mode = $options['stupid_mode'];
$items=$order->get_items();
$arr = [];
foreach ( $items as $item ) {
$data = $item->get_data();
$product_instance = wc_get_product( $data['product_id'] );
$product_full_description = $product_instance->get_description();
$product_short_description = $product_instance->get_short_description();
$arr[] = [
'productSKU' => $data['product_id'],
'name' => $data['name'], // or $data['description']
'description' => $product_full_description, // or $product_short_description
'quantity' => $data['quantity'],
'price' => $data['total'] / $data['quantity'],
];
}
}

Related

how to condition same time product to insert and update in laravel

I am tring Order update.I need to update multi product in different row. when I update the product then it only updated created fields. i want to update current row and then new product insert in different row.
$product_id = $request->product_id;
$seller_id = Auth::user()->id;
$price = $request->product_price;
$quantity = $request->product_qty;
$payment_status = $request->payment_status;
$delivery_status = $request->delivery_status;
$shipping_type = "home_delivery";
$shipping_charge = $request->shipping_cost;
$user = Address::all()->where('user_id', $request->user_id)->first();
$cname= DB::table('users')->where('id', $user->user_id)->value('name');
$shipping_address = [
'user_id'=>$request->user_id,
'name'=> $request->name,
'phone'=>$request->phone,
'email'=>$user->email,
'city'=>$user->city,
'postal_code'=>$user->postal_code,
'country'=>$user->country,
'address'=>$request->address,
];
$order_id=$request->id;
$order = Order::findOrFail($order_id)->update([
'seller_id' => Auth::user()->id,
'shipping_address' => json_encode($shipping_address),
'payment_status' => $request->payment_status,
'payment_type' => $request->payment_option,
'payment_details' => $request->payment_option,
'coupon_discount' => $request->coupon_discount,
'round_amount' => $request->round_discount,
'grand_total' => $request->grand_total,
]);
for($i = 0; $i <= count($request->product_id)-1; $i++){
$data = [
'order_id' => $order_id,
'product_id' => $product_id[$i],
'seller_id' => $seller_id,
'price' => $price[$i],
'quantity' => $quantity[$i],
'payment_status' => $payment_status,
'delivery_status' => $delivery_status,
'shipping_type' => $shipping_type,
'shipping_cost' => $shipping_charge,
];
if(!empty(OrderDetail::where('order_id', $order_id))){
OrderDetail::where('order_id', $order_id)->update($data);
}
else{
Orderdetail::insert($data);
}
$product_stock_info = ProductStock::where('product_id', $product_id[$i])->first();
// dd($product_id[$i]);
$stock = $product_stock_info->qty;
$updateStock = $stock - $quantity[$i];
$product_stock_info->qty = $updateStock;
$product_stock_info->save();
}
my problem is here
if(!empty(OrderDetail::where('order_id', $order_id))){
OrderDetail::where('order_id', $order_id)->update($data);
} else {
Orderdetail::insert($data);
}
You can use updateOrCreate method .
the updateOrCreate method persists the model, so there's no need to manually call the save method
$flight = Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99, 'discounted' => 1]
);
as like
$whereData = [
'order_id' => $order_id
];
OrderDetail::updateOrCreate($whereData ,$data);

Update the order items in in laravel 5.5

I have an ordering app which have order items. I was able to update the field which only have single data but I can't make the field that handle an array data work. The order items belongs to another model called OrderItems. Can you help on to deal with this properly?
here's my update function.
public function update(Request $request, Orders $order )
{
$this->validate($request, [
'user_id' => 'required',
'status_id' => 'required',
'currency_id' => 'required',
'company_id' => 'required',
'purchase_no' => 'required|unique:orders,purchase_no,'.$order->id,
'notes' => '',
'admin_notes' => '',
'delivery_date' => '',
'publish' => '',
'product_id' => 'required',
'product_code' => 'required',
'product_name' => 'required',
'quantity' => 'required'
]);
$order->user_id = $request->input('user_id');
$order->status_id= $request->input('status_id');
$order->currency_id = $request->input('currency_id');
$order->company_id = $request->input('company_id');
$order->purchase_no = $request->input('purchase_no');
$order->notes = $request->input('notes');
$order->admin_notes = $request->input('admin_notes');
$order->delivery_date = $request->input('delivery_date');
$order->publish = $request->input('publish');
$order->grandtotal = (float) str_replace(',', '', $request->input('grandtotal'));
$order->save();
$input = Orderitems::findOrFail($order->id);
for($i=0; $i<= count($input['quantity']); $i++) {
if(empty($input['quantity'][$i]) || !is_numeric($input['quantity'][$i])) continue;
$items->product_id = $input->input('product_id')[$i];
$items->product_code = $input->input('product_code')[$i];
$items->product_name = $input->input('product_name')[$i];
$items->cost = $input->input('cost')[$i];
$items->quantity = $input->input('quantity')[$i];
$items->total_cost = (float) str_replace(',', '', $input->input('total_cost')[$i]);
$orderItems->save();
}
return redirect()->route('orders.index');
}
Here's the screenshot from my update form
Thank you so much in advance!
You have written wrong code for the update.it should be like this.
You are saving $orderItemsobject which hasn't updated in your code.
$items= $order->orderItems;
// still code not updated here i want to print product _id ?
foreach($items as $key => $item) {
$item->product_id = $request->input('product_id')[$key];
$item->product_code = $request->input('product_code')[$key];
$item->product_name = $request->input('product_name')[$key];
$item->cost = $request->input('cost')[$key];
$item->quantity = $request->input('quantity')[$key];
$item->total_cost = (float) str_replace(',', '', $request->input('total_cost')[$key]);
$item->save();
}

Woocommerce rest api update product image gives error

I want to update my product with product image but it is not updated. I am using woocommerce rest API for adding a product.
I am following these step for update product:
Create a view file for display all the product list
Create edit function in the controller for getting all product data
when uploading a new image from edit form it gives an error
Here is my code:
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$image->move('C:/xampp/htdocs/New-flex/wp-content/uploads/backend-product-image', $name);
$productimg_path_store_db = "http://localhost/New-flex/wp-content/uploads/backend-product-image/";
$productimg_allimg[] = $productimg_path_store_db.$name;
}
}
$AddContentText = $request->get('AddContentText');
if (!empty($AddContentText)) {
/* For List All product qnumber check in array */
$newallproducts_data = $woocommerce->get('products',array('per_page' => 100));
$array = json_decode(json_encode($newallproducts_data), True);
$partssku = array();
$partid = array();
$parturl = array();
foreach ($array as $key ) {
$partssku[] = $key['sku'];
$partid[] = $key['id'];
$parturl[] = $key['permalink'];
}
/* Create associative array product id and sku for compare */
$comb = array_combine($partssku,$partid);
/* Combine content QNUMBER and QTY in array */
$chunks = array_chunk(preg_split('/(;|,)/', $AddContentText), 2);
$result = array_combine(array_column($chunks, 0), array_column($chunks, 1));
/*Product qnumber allready avilabale array*/
$diff = array_intersect_key($result,$comb);
/*NEW PRODUCT array*/
$newproduct = array_diff($result, $diff);
/* Qnumber insert in database */
$parts_name = implode(',', array_keys($result));
/* count insert in database */
$noofpartsuse = implode(',', array_values($result));
/*echo "<pre>";
echo "main data";
print_r($result);
echo "allproduct array";
print_r($comb);
echo "Product qnumber allready avilabale";
print_r($diff);
echo "NEW PRODUCT";
print_r($newproduct);
echo "</pre>";*/
// print_r($newproduct_data);
//print_r($woocommerce->post('products', $data));
}
else{
$parts_name = '';
$noofpartsuse = '';
}
$productactive = $request->get('productactive');
if(!empty($productactive))
{
$productactive = "publish";
}
else{
$productactive = "draft";
}
$ProductListOrder = $request->get('ProductListOrder');
$producttype = $request->get('producttype');
$ProductQNumber = $request->get('ProductQNumber');
$ProductName = $request->get('name');
$ProductWidthMM = $request->get('ProductWidthMM');
$ProductLengthMM = $request->get('ProductLengthMM');
$ProductWidthInch = $request->get('ProductWidthInch');
$ProductLengthInch = $request->get('ProductLengthInch');
$Productinfotext = $request->get('Productinfotext');
$Producttechnocaldesc = $request->get('Producttechnocaldesc');
$metadescription = $request->get('metadescription');
$proimgalt = $request->get('proimg-alt');
$proyoutubelink = $request->get('proyoutubelink');
$proyoutubelinkalt = $request->get('proyoutubelink-alt');
$provimeolink = $request->get('provimeolink');
$provimeolinkalt = $request->get('provimeolink-alt');
$onshapelink = $request->get('onshapelink');
$onshapelinkalt = $request->get('onshapelink-alt');
$data = [
'name' => $ProductName,
'type' => $producttype,
'status' => $productactive,
'regular_price' => '',
'description' => $Productinfotext,
'short_description' => $Producttechnocaldesc,
'sku' => $ProductQNumber,
'categories' =>array (),
'meta_data' => [
[
'key' => 'list_order',
'value' => $ProductListOrder
],
[
'key' => 'ProductWidthMM',
'value' => $ProductWidthMM
],
[
'key' => 'ProductLengthMM',
'value' => $ProductLengthMM
],
[
'key' => 'ProductWidthInch',
'value' => $ProductWidthInch
],
[
'key' => 'ProductLengthInch',
'value' => $ProductLengthInch
],
[
'key' => 'proyoutubelink',
'value' => $proyoutubelink
],
[
'key' => 'proyoutubelink-alt',
'value' => $proyoutubelinkalt
],
[
'key' => 'provimeolink',
'value' => $provimeolink
],
[
'key' => 'provimeolink-alt',
'value' => $provimeolinkalt
],
[
'key' => 'onshapelink',
'value' => $onshapelink
],
[
'key' => 'onshapelink-alt',
'value' => $onshapelinkalt
],
[
'key' => 'UseParts-link',
'value' => $parts_name
],
[
'key' => 'Noofparts-use',
'value' => $noofpartsuse
]
],
'images' => array ()
];
/* image array for store image */
if(!empty($productimg_allimg)){
$images = &$data['images'];
$n = 0;
foreach($productimg_allimg as $id)
{
$images[] = array( //this array must be created dynamic
'src' => $id,
'position' => $n++
);
}
unset($images);
}
if(!empty($_POST['categories'])){
/* Producta category array */
$categories = &$data['categories'];
foreach($_POST['categories'] as $cat)
{
$categories[] = array( //this array must be created dynamic
'id' => $cat
);
}
unset($categories);
}
$data_insert = $woocommerce->post('products', $data);
if($data_insert){
foreach ($newproduct as $key => $value) {
$newproduct_data = [
'name' => $key,
'sku' => $key
];
$woocommerce->post('products', $newproduct_data);
}
}
return redirect('products')->with('success', 'Product has been Added');
Above code written in laravel

how to store simply multi array in session laravel 5.6

i store one array in to session('cart') last click [add to cart] , when i add another array in session('cart'), but it store one array then session can not save both.
help me !
public function addtocart(Request $req,$id){
$product = _prod::find($id)->toArray();
$item = [
'name' => $product['pName'],
'description' => $product['pDesc'],
'price' => $product['pPrice'],
];
$cart = [
'qtyTotal' => 0,
'priceTotal' => 0,
'item' => [$item]
];
$req->session()->put('cart',$cart);
$a = session()->get('cart');
}
change this
$req->session()->put('cart',$cart);
$a = session()->get('cart');
dd($a);
to this:
$cartvalues[] = $cart;
$req->session()->put('cart',$cartvalues);
$a = session('cart');
dd($a);
because you keep overwriting the previous value inside the session cart
Here you are overwriting cart variable in session every time you add a new item. So store cart items as an array and add item to array. Code should be:
public function addtocart(Request $req, $id) {
$product = _prod::find($id)->toArray();
$item = [
'name' => $product['pName'],
'description' => $product['pDesc'],
'price' => $product['pPrice'],
];
$cart = [
'qtyTotal' => 0,
'priceTotal' => 0,
'item' => [$item]
];
$cartItems = session()->get('cart');
if (empty($cartItems)) {
$cartItems = [];
}
$cartItems[] = $cart;
$req->session()->put('cart', $cartItems);
return view($cartItems);
}
Here is the simple example to update your cart in your case
`
public function addtocart(Request $req,$id){
//return redirect('Resouce/product');
$product = _prod::find($id)->toArray();
$item = [
'name' => $product['pName'],
'description' => $product['pDesc'],
'price' => $product['pPrice'],
];
if($req->session()->has('cart')){
$oldCart = $req->session()->get('cart');
$newCart = [
'qtyTotal' => 0,
'priceTotal' => 0,
'item' => array_merge($item,$oldCart['item'])
];
$req->session()->put('cart',$newCart);
}
$cart = [
'qtyTotal' => 0,
'priceTotal' => 0,
'item' => [$item]
];
$req->session()->put('cart',$cart);
$a = session()->get('cart');
dd($a);
}`

PHP: Is this the right way to create an array by using a loop?

I'm working on this e-commerce site and I'm trying to create a jSON array containing the cart items in PHP.
So far I have:
for ($i=0; $i < count($_SESSION['cart']); $i++) {
$prodid = $_SESSION['cart'][$i][0];
$sizeId = $_SESSION['cart'][$i][1];
$colorId = $_SESSION['cart'][$i][2];
$qty = $_SESSION['cart'][$i][3];
$inslagning = $_SESSION['cart'][$i][4];
$wrapCost += ($inslagning == 'YES' ? 20 : 0);
$row = get_product_buy($prodid, $sizeId, $colorId);
$prodname = $row['prodname'];
$color = $row['color'];
$size = $row['size'];
$prodCatid = $row['catid'];
$image = $row['biggerimage'];
$box = $row['box_number'];
for ($j=0;$j<$qty;$j++) {
$cart = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
}
I know I have the $cart var inside the loop which is probably wrong. The end result should be like this:
$cart = array(
array(
'reference' => '123456789',
'name' => 'Klarna t-shirt',
'quantity' => 1,
'unit_price' => $att_betala * 100,
'discount_rate' => 0,
'tax_rate' => 2500
),
array(
'reference' => '123456789',
'name' => 'Klarna t-shirt',
'quantity' => 1,
'unit_price' => $att_betala * 100,
'discount_rate' => 0,
'tax_rate' => 2500
)
);
All help is appreciated!
You have to append a new child to $cart instead of overwriting it. To append values to an array (the easy way), use $array[] = …. PHP increments the child's ID automatically.
Not required, but please initialize $cart first and use descriptive variables.
To inspect an array (or other data), use var_dump.
// Initialize an empty array. Not needed, but correct to do.
$cart = array();
for ($i=0; $i < count($_SESSION['cart']); $i++) {
$prodid = $_SESSION['cart'][$i][0];
$sizeId = $_SESSION['cart'][$i][1];
$colorId = $_SESSION['cart'][$i][2];
$qty = $_SESSION['cart'][$i][3];
$inslagning = $_SESSION['cart'][$i][4];
$wrapCost += ($inslagning == 'YES' ? 20 : 0);
$row = get_product_buy($prodid, $sizeId, $colorId);
$prodname = $row['prodname'];
$color = $row['color'];
$size = $row['size'];
$prodCatid = $row['catid'];
$image = $row['biggerimage'];
$box = $row['box_number'];
// Append products $qty times.
for ($productCount=0; $productCount<$qty; $productCount++) {
// Append a new product to $cart.
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
}
Use like this
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
Try Using
for ($j=0;$j<$qty;$j++) {
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
$json_enc = json_encode($cart);
You are not appending to the $cart variable, you are overwriting it on every pass of the loop.
Use the [] syntax to append to an array:
$cart[]=...
Also, its good to declare an empty array at the top of the code:
$cart=array();

Categories