I create an eCommerce store with laravel 9.
I install stripe API, but I have some difficulty displaying product data in the stripe session.
Here is the function:
public function stripe()
{
\Stripe\Stripe::setApiKey(config(key:'stripe.sk'));
$session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'EUR',
'product_data' => [
'name' => 'huile',
],
'unit_amount' => '30',
],
'quantity' => 1,
],
],
'mode' => 'payment',
'success_url' => route(name:'shop'),
'cancel_url' => route(name:'shop'),
]);
return redirect()->away($session->url);
}
I tried to create function type array return product_ price and product_name from Cart content.but always have issues. So if there are some ideas to how can i display data from the product cart to the stripe session.
I want to call product data from Cart collection. and display it in stripe session.
I tried to create function return array
public function getproducts()
{
$cart = Cart::content()->load('product');
$cartArray = $cart->toArray();
return ['cart' => $cartArray];
}
I want to set a quantity for my items in darryldecode/laravelshoppingcart but it doesn't set and when I get dd there is no quantity in item. It's my cart controller:
public function add(Request $request)
{
$product = Product::findOrFail($request->product_id);
$rowId = $product->id;
Cart::add(array(
'id' => $rowId,
'name' => $product->name,
'price' => $product->is_sale ? $product->sale_price : $product->price,
'quantity' => 1,
'attributes' => $product->toArray(),
'associatedModel' => $product
));
return redirect()->back();
}
I'm using laravel 8 and I updated the package to latest version
I'm working on trying to create a cart system in laravel, where selecting an item on a menu will add it as a cart item. If the item already exists on the cart, it should just increase the quantity. I have this almost entirely working, except that the solution I've worked out makes it so that in the case of needing to increment the quantity, it will also recreate the cart item, so that there is one both with and without the incremented quantity. I've put a lot of mental energy into struggling with this, and was hoping someone might be able to help. Here is the relevant code, in the controller for creating the cart item.
$cartItems = Cartitem::all();
if($cartItems->isEmpty()){
$cartItem = new Cartitem([
'name' => $request->name,
'image_url' => $request->image_url,
'description' => $request->description,
'price' => $request->price,
'quantity' => 1
]);
$cartItem->save();
} else {
forEach($cartItems as $item){
if($request->name != $item->name){
$cartItem = new Cartitem([
'name' => $request->name,
'image_url' => $request->image_url,
'description' => $request->description,
'price' => $request->price,
'quantity' => 1
]);
$cartItem->save();
} else {
++$item->quantity;
$item->save();
}
}
}
$oldItem = Cartitem::where('name', $request->name)->first();
if ($oldItem) {
$oldItem->increment('quantity');
}
else {
$newItem = new Cartitem([
'name' => $request->name,
'image_url' => $request->image_url,
'description' => $request->description,
'price' => $request->price,
'quantity' => 1
]);
$newItem->save();
}
session cart, i need to add same product again if product size is different:
foreach($cart_content as $cart_item)
{
if(($cart_item['id']==$_POST['product_id'])&&($cart_item['options']['size']==$_POST['optsize']))
{
$data = array(
'rowid' => $cart_item['rowid'],
'qty' => ($cart_item['qty']+$_POST['quantity']),
'options' => ($_POST['optsize'])
);
$this->cart->update($data);
unset($cart_item['rowid']);
$upd=1;
break;
}
}
Looking for advice on the sequence of Magento API calls necessary to implement this business process:
An inventory item (either physical or virtual/digital) is made available by the seller via an external channel (not the regular web storefront).
A customer initiates a payment directly to me without going through the Magento cart / checkout flow (can I lookup sales tax at this point?)
After the payment has been made, I want to trigger Magento post-processing logic to record the sale, manage inventory, etc.
For physical goods, I want to trigger Magento fulfillment logic to occur to create the shipment, etc.
I'm aware of the SOAP API, I'm looking for help to understand which actions need to be taken along the way to enact this process.
Here is very basic example how Magento API can be used for your case:
Connect to Magento via API
$user = 'apiUser'; $password = 'apiKey';
$proxy = new SoapClient('http://your_magento_host.com/api/v2_soap/?wsdl');
$sessionId = $proxy->login($user, $password);
Create or select customer
// Create customer
$customerList = $proxy->customerCustomerCreate($sessionId, array( 'email' => 'customer#gmail.com', 'firstname' => 'Will', 'lastname' => 'Smith', 'password' => 'qwerty', 'website_id' => 1, 'store_id' => 1, 'group_id' => 1 ));
$customer = (array) $customerList[0];
$customer['mode'] = 'customer';
// Or select existing customer (by email)
$filter = array(
'complex_filter' => array(
array(
'key' => 'email',
'value' => array('key' => 'in', 'value' => 'customer#gmail.com')
)
) );
$customerList = $proxy->customerCustomerList($sessionId, $filter);
$customer = (array) $customerList[0];
$customer['mode'] = 'customer';
Create cart
$cartId = $proxy->shoppingCartCreate($sessionId, 1);
$proxy->shoppingCartCustomerSet($sessionId, $cartId, $customer);
Select product (by sku)
$filter = array(
'complex_filter' => array(
array(
'key' => 'sku',
'value' => array('key' => 'in', 'value' => 'T-SHIRT001')
)
) );
$productList = $proxy->catalogProductList($sessionId, $filter);
$product = (array) $productList[0];
$product['qty'] = 1;
Add product to cart
$proxy->shoppingCartProductAdd($sessionId, $cartId, array($product));
Set billing/shipping address. You should add this addresses to customer if you just create it before.
$address = array(
array(
'mode' => 'shipping',
'firstname' => $customer['firstname'],
'lastname' => $customer['lastname'],
'street' => 'street address',
'city' => 'city',
'region' => 'region',
'telephone' => 'phone number',
'postcode' => 'postcode',
'country_id' => 'country ID',
'is_default_shipping' => 0,
'is_default_billing' => 0
),
array(
'mode' => 'billing',
'firstname' => $customer['firstname'],
'lastname' => $customer['lastname'],
'street' => 'street address',
'city' => 'city',
'region' => 'region',
'telephone' => 'phone number',
'postcode' => 'postcode',
'country_id' => 'country ID',
'is_default_shipping' => 0,
'is_default_billing' => 0
),
);
$proxy->shoppingCartCustomerAddresses($sessionId, $cartId, $address);
Set shipping mathod
$proxy->shoppingCartShippingMethod($sessionId, $cartId, 'flatrate_flatrate');
Set payment method.
$paymentMethod = array(
'po_number' => null,
'method' => 'checkmo',
'cc_cid' => null,
'cc_owner' => null,
'cc_number' => null,
'cc_type' => null,
'cc_exp_year' => null,
'cc_exp_month' => null
);
$proxy->shoppingCartPaymentMethod($sessionId, $cartId, $paymentMethod);
Place order
$orderId = $proxy->shoppingCartOrder($sessionId, $cartId, null, null);
Now check Sales->Orders in Magento admin area and you'll see new order.
More details here: http://www.magentocommerce.com/api/soap/introduction.html
Yes, you can catch information about Tax:
1) Without order saving. Step 9:
$result = $proxy->shoppingCartTotals($sessionId, $cartId);<br>
var_dump($result);
You'll see array of subtotal, taxes, discounts and total.
2) With order saving. Step 10:
$result = $proxy->salesOrderInfo($sessionId, $orderId);<br>
var_dump($result);
// cancel order<br>
$result = $proxy->salesOrderCancel($sessionId, $orderId);
More information about used API calls here:
http://www.magentocommerce.com/api/soap/checkout/cart/cart.totals.html
http://www.magentocommerce.com/api/soap/sales/salesOrder/sales_order.info.html
http://www.magentocommerce.com/api/soap/sales/salesOrder/sales_order.cancel.html