I would like to ask you for some help or even just a comparison.
On my site I have bundles that are dimanically priced with select products, some with radio and some with default.
Magento 2 however shows two different prices between listing and product page. I have checked at code level and on the product page there is a manipulation via js that changes the price. In fact when the page loads you see a price before the update. Have you ever implemented some solution to make the same prices appear? This is the piece of code I am trying to implement but without success as it returns the same price because the products are set to default even though they are not selected on the product page.
$product = $this->productRepository->get($product_sku);
$selectionCollection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$children_ids = $this->bundleType->getChildrenIds($product->getId(), true);
$bundle_items = [];
$optionsCollection = $product->getTypeInstance(true)
->getOptionsCollection($product);
foreach ($optionsCollection as $options) {
$optionArray[$options->getOptionId()]['option_title'] = $options->getDefaultTitle();
$optionArray[$options->getOptionId()]['option_type'] = $options->getType();
if ($options->getType() !== 'checkbox' && $options->getRequired() === "1") {
foreach ($selectionCollection as $selection) {
foreach ($children_ids as $bundle_id) {
if ((array_values($bundle_id)[0] === $selection->getEntityId())
&& $options->getId() === $selection->getOptionId()) {
$price = $selection->getPrice();
$qty = $selection->getSelectionQty();
$bundle_items[] = $price * $qty;
}
}
}
}
}
$finale_price = array_sum($bundle_items);
I finally solved it by creating a function that would return the tier price. Mine is a bit more customised because I have different prices depending also on the customer group. I'll leave the code.
public function getBundlePrice($product_sku, $product_sal)
{
$product = $this->productRepository->get($product_sku);
$selectionCollection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$children_ids = $this->bundleType->getChildrenIds($product->getId(), true);
$bundle_items = [];
$optionsCollection = $product->getTypeInstance(true)
->getOptionsCollection($product);
foreach ($optionsCollection as $options) {
$optionArray[$options->getOptionId()]['option_title'] = $options->getDefaultTitle();
$optionArray[$options->getOptionId()]['option_type'] = $options->getType();
if ($options->getType() !== 'checkbox' && $options->getRequired() === "1") {
foreach ($selectionCollection as $selection) {
foreach ($children_ids as $bundle_id) {
if ((array_values($bundle_id)[0] === $selection->getEntityId())
&& $options->getId() === $selection->getOptionId()) {
$price = $selection->getPrice();
$qty = $selection->getSelectionQty();
if ($qty > 1 || $selection->getTierPrice()) {
$price = $this->getCustomPrice($selection);
} else if ($this->customerSession->getCustomer()->getGroupId() === "2") {
$price = $selection->getPartnerPrice();
}
$bundle_items[] = $price * $qty;
}
}
}
}
}
return $this->calculator->getAmount(array_sum($bundle_items), $product_sal);
}
Related
I want to move my first order to the bottom of the list and not to delete the first order as well.
here is my code. This is deleting/unset first order as well.
`
if (isset($request['defer_order']) && $request['defer_order'] != null) {
$order_id = $request['defer_order'];
foreach ($order_items as $key => $order_item) {
$last = count($order_items);
if ($order_item->order_id == $order_id) {
unset($order_items[$key]);
$order_items[$last + 1] = $order_item;
}
}
$order_items = $order_items;
}
`
I am trying to move first order to the bottom of the list.
Since you are using unset, the order is removed from the array. You can use the code below instead. It will add your desired order at the end without changing anything in the array
if (isset($request['defer_order']) && $request['defer_order'] != null) {
$order_id = $request['defer_order'];
$order_items = (array)$order_items;
$myOrder = '';
foreach ($order_items as $order_item) {
if ($order_item['order_id'] == $order_id) {
$myOrder = $order_item;
unset($order_items[$key]);
break;
}
}
if ($myOrder != '') {
$order_items[] = $myOrder;
}
$order_items = (object)$order_items;
}
Updated the code as per your requirement
I'm new to the laravel community but have to make a quick fix for my eCommerce website,
Currently, I have an OrderController that saves information to the database after Checking out,
The problem is, The Current Controller saves multiple products purchase at one time in multiple
new Order Columns in the Database I have been trying to fix on my own for a couple of days now without fully learning laravel as my website is live and I can't spend time learning Laravel.
This is The Code Block That fetches & saves Order details in the Controller:
public function store(Request $request)
{
$carts = Cart::where('user_id', Auth::user()->id)
->get();
if ($carts->isEmpty()) {
flash(translate('Your cart is empty'))->warning();
return redirect()->route('home');
}
$shipping_info = Address::where('id', $carts[0]['address_id'])->first();
$shipping_info->name = Auth::user()->name;
$shipping_info->email = Auth::user()->email;
if ($shipping_info->latitude || $shipping_info->longitude) {
$shipping_info->lat_lang = $shipping_info->latitude . ',' . $shipping_info->longitude;
}
$seller_products = array();
foreach ($carts as $cartItem){
$product_ids = array();
$product = Product::find($cartItem['product_id']);
if(isset($seller_products[$product->user_id])){
$product_ids = $seller_products[$product->user_id];
}
array_push($product_ids, $cartItem);
$seller_products[$product->user_id] = $product_ids;
}
//Order Details Storing
foreach ($seller_products as $cartItem) {
$product = Product::find($cartItem['product_id']);
$subtotal += $cartItem['price'] * $cartItem['quantity'];
$tax += $cartItem['tax'] * $cartItem['quantity'];
$coupon_discount += $cartItem['discount'];
$product_variation = $cartItem['variation'];
$product_stock = $product->stocks->where('variant', $product_variation)->first();
if ($product->digital != 1 && $cartItem['quantity'] > $product_stock->qty) {
flash(translate('The requested quantity is not available for ') . $product->getTranslation('name'))->warning();
$order->delete();
return redirect()->route('cart')->send();
} elseif ($product->digital != 1) {
$product_stock->qty -= $cartItem['quantity'];
$product_stock->save();
}
}
$order_detail = new OrderDetail;
$order_detail->order_id = $order->id;
$order_detail->seller_id = "$seller_products";
$order_detail->product_id = $product->id;
$order_detail->variation = $product_variation;
$order_detail->price = $cartItem['price'] * $cartItem['quantity'];
$order_detail->tax = $cartItem['tax'] * $cartItem['quantity'];
$order_detail->shipping_type = $cartItem['shipping_type'];
$order_detail->product_referral_code = $cartItem['product_referral_code'];
$order_detail->shipping_cost = $cartItem['shipping_cost'];
$shipping += $order_detail->shipping_cost;
if ($cartItem['shipping_type'] == 'pickup_point') {
$order_detail->pickup_point_id = $cartItem['pickup_point'];
}
//End of storing shipping cost
$order_detail->quantity = $cartItem['quantity'];
$order_detail->save();
In Prestashop CMS, address1 is only required if there is a virtual product in the cart.
How can this be achieved? Does anybody have the solution for this?
$context = Context::getContext();
$cart = $context->cart;
$is_virtual = $cart->isVirtualCart();
Works perfectly. Example:
if (in_array('address1', $arr) && $is_virtual) {
$this->def['fields']['address1']['required'] = 0;
} else if (in_array('address1', $arr)) {
$this->def['fields']['address1']['required'] = 1;
$this->fieldsRequired[] = 'address1';
} else {
$this->def['fields']['address1']['required'] = 0;
}
Using Magento 1.8.1, on the checkout page, I'm trying to add a product to the cart in the code. Here is the code I'm using:
$totals = Mage::getSingleton('checkout/cart')->getQuote()->getTotals();
$subtotal = $totals["subtotal"]->getValue();
$free_samples_prod_id = 1285;
if ( $subtotal >= 50 ) {
$this->addProduct($free_samples_prod_id, 1);
}
else{
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $free_samples_prod_id) {
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
break;
}
}
}
The code is in: app\code\core\Mage\Checkout\Model\Cart.php under public function init()
The product does get added sucessfully, however, everytime somebody visits their cart page, the quantity increases by one. How can I modify that code so the quantity is always 1?
Thank you
Axel makes a very good point, but to answer your immediate question, why not test for the product's presence before you add it
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
$subtotal = $totals["subtotal"]->getValue();
$free_samples_prod_id = 1285;
if ( $subtotal >= 50 ) {
$alreadyAdded = false;
foreach ($items as $item) {
if($item->getId() == $free_samples_prod_id) { $alreadyAdded = true; break; }
}
if(!$alreadyAdded) { $this->addProduct($free_samples_prod_id, 1); }
}
I cannot seem to get the custom options field to display in my magento code below - can any of you good people help me out?
public function getProductName($product)
{
$value = '<b>'.$product->getname().'</b>';
if ($product->getproduct_type() == 'configurable')
{
//add sub products
$collection = mage::getModel('sales/order_item')
->getCollection()
->addFieldToFilter('parent_item_id', $product->getitem_id());
foreach ($collection as $subProduct)
{
$value .= '<br><i>'.$subProduct->getname().'</i>';
//add product configurable attributes values
$attributesDescription = mage::helper('ProductReturn/Configurable')->getDescription($subProduct->getproduct_id(), $product->getrp_product_id());
if ($attributesDescription != '')
$value .= '<br>'.$attributesDescription;
}
}
return $value;
}
There are issue in $product->getproduct_type() is not wrong it should try it $product->getTypeId()