How to get price of actual product in Drupal Commerce? - drupal-modules

Hi guys im new in drupal, i start to learn drupal 3 month before.
Im trying to get the price number of the product i get the total cart like this
$user_id = \Drupal::currentUser()->id();
$orders = \Drupal::entityTypeManager()
->getStorage('commerce_order')
->loadByProperties(['uid' => $user_id]);
$orderPrice = [];
foreach($orders as $key => $value) {
$orderPrice[] = $value->getTotalPrice()->getNumber();
}
$total = $orderPrice[count($orderPrice)-1];
$total= number_format($total, 2);
Im trying to do something similar with te product.
For example if iam in the product 1 get the price of product 1, if i am in the product 2 get the price of product 2.
Thanks for your time :)

Related

Get the names of a dynamic product group with a subscriber in Shopware 6.1

how can I get the names of the dynamic product groups of a product?
Especially the names of that groups. The names are in the table 'product_stream_translation'.
I have a subscriber and have added this Criteria. When I am doing it like this:
$criteria->addFilter(new EqualsFilter('id', $id));
$criteria->addAssociation('streams');
$dynamicProductGroups = $this->productRepository->search($criteria, $context)->getEntities();
I just got back an empty streamEntity.
#streams: Shopware\Core\Content\ProductStream\ProductStreamCollection {#11805 ▼
#elements: []
#extensions: []
}
When I am doing it like this:
$criteria->addFilter(new EqualsFilter('productId', $id));
$dynamicProductGroups = $this->productStreamMappingRepository->searchIds($criteria, $context);
I just got back the Id I put in:
product_stream_mapping
I wonder how I will get the name of the dynamic product group.
With a query I can get all the assign 'product_stream_id's from the table 'product_stream_mapping' like this:
SELECT * FROM product_stream_mapping WHERE product_id =0x000000000000000000123b313030524b
And then get the associated name of the dynamic product group. With is like this:
SELECT psm.product_id, psm.product_stream_id, p.product_number, pst.name
FROM product_stream_mapping psm
JOIN product_stream_translation pst ON pst.product_stream_id = psm.product_stream_id
JOIN product p ON p.id = psm.product_id
WHERE psm.product_id = 0x000000000000000000123b313030524b
How can I get it in the Subscriber?
Do I have to use criteria or do I have to user a repository?
This line is wrong:
$dynamicProductGroups = $this->productRepository->search($criteria, $context)->getEntities();
It won't return dynamic product groups but a product collection. To get a product stream collection (dynamic product groups) replace it with:
/** #var ProductEntity $product */
$product = $this->productRepository->search($criteria, $context)->first();
$dynamicProductGroups = $product->getStreams();
Then you can read the names of the streams with:
$names = array_values(
$dynamicProductGroups->map(fn (ProductStreamEntity $productStream) => $productStream->getName())
);

NetSuite Tax Rate not showing

So I was doing a simple custom search but somehow the tax rate is not showing. I am still new to NetSuite and I tried to find workarounds about this. Any suggestions?
var columns = new Array();
columns[0] = new nlobjSearchColumn('internalid');
columns[1] = new nlobjSearchColumn('name');
columns[2] = new nlobjSearchColumn('itemid');
columns[3] = new nlobjSearchColumn('rate');
columns[4] = new nlobjSearchColumn('taxtype')
var res = nlapiSearchRecord('salesTaxItem', null, null, columns);
wcache('res---> '+ JSON.stringify(res));
return;
not sure if it matters but the name should be 'salestaxitem' not 'salesTaxItem' Also do you know if you have any tax items or codes in your account?
If you are on a One World account you need to make sure you have access to the subisdiary that has tax items.
I see all the rates when I run your code in my account.
The Netsuite record browser has this to say about the rate field:
Enter the appropriate tax rate as a percentage. Example: 8% This percentage will be calculated when you select this tax item on transactions. This field is available only if the SuiteTax feature is disabled in your account.

Magento 2 pagination is not working in join with product collection and custom table

$page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 4;
$collection = $this->productcollection->create()->addAttributeToSelect(array('group_name','name','status'));
$collection->addAttributeToFilter('status',1);
$collection->getSelect()
->join(['firmware_product' => $collection->getTable('firmware_product')],'firmware_product.product_id = e.entity_id',[]);
$collection->getSelect()
->join(['firmware' => $collection->getTable('firmware')],'firmware.entity_id = firmware_product.firmware_id',[])
->columns(array("firmware.version_no","firmware.build_date","firmware.is_active"));
$collection->getSelect()->where("firmware.is_active = ?",1);
$collection->getSelect()->order('firmware.entity_id', 'DESC');
$collection->setPageSize($pageSize);$collection->setCurPage($page);
The code above gives me the same entity_id as already exists. I reset the column and fetch only the firmware records and it still gives me the same error in the firmware entity_id. If I use this code
$collection->getSelect()->group('e.entity_id');
then the pagination does not work. Can anyone tell me what the solution for this is?

Opencart: how to change item price when adding it into the cart

I am trying to implement a functionality in opencart where it will be possible to enter a custom price at the product page via text area and when item is added to the cart if custom price entered it will apply the price specified in the custom price field.
Similar question was asked here someone kindly provided a good solution which applies to OpenCart 1.5.x. However I have tried to follow this approach on OpenCart 2 without any success. I have checked everything over and over for the last few days but I don't seem to be able to get this working as I am a newbie in programming world
I wondering if anyone is able to point me to right direction to what I may be missing.
I have search the web but unable to find any relevant information
I have checked and noticed that AJAX request is changed to #product div in 2.x, so I have enter my price input within this div underneath the quantity
<input name="custom_price" id="custom_price" value="" title="custom_price" class="input-text custom_price" type="textarea">
I have than moved on to the controller checkout/cart/add within the Add() method I have added this code
if(isset($this->request->post['custom_price'])) {
$custom_price = $this->request->post['custom_price'];
} else {
$custom_price = false;
}
Further down, I have changed this line
$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option, $recurring_id);
to:
$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option, $custom_price, $recurring_id);
Next, in the system/library/cart.php I have change the definition of the Add() method to the following
public function add($product_id, $qty = 1, $option = array(), $recurring_id = 0, $custom_price = false) {
Before the end of the Add()method I have added the following
if($custom_price) {
if(!isset($this->session->data['cart']['custom_price'])) {
$this->session->data['cart']['custom_price'] = array();
}
$this->session->data['cart']['custom_price'][$key] = $custom_price;
}
Within the GetProduct() I have added these lines
if(isset($this->session->data['cart']['custom_price'][$key])) {
$price = $this->session->data['cart']['custom_price'][$key];
}
right after this line:
$price = $product_query->row['price'];
Finally at after the array where product price is set to price + option price
'price' => ($price + $option_price),
I have added the following
if(isset($this->session->data['custom_price'][$key])) {
$this->data[$key]['price'] = $this->session->data['custom_price'][$key];
}

Entity framework - group by and count clause

I want to write an query with group by and count clause in Entity framework. like employees and managers and display all employees based on manager
any code samples
Thanks
var employeesByManager = db.Employees.
GroupBy(emp => emp.Manager.EmployeeName, emp => emp).
ToList();
var employeeCountsByManager = db.Employees.
Select(emp => new { ManagerName = emp.EmployeeName, EmployeeCount = emp.Employees.Count() }).
Where(a => a.EmployeeCount > 0).
ToList();
You can, of course, get the employees grouped by manager in a similar manner as counts. The GroupBy method is just a simpler way to do this.

Resources