How to change woommerce shopping cart total amount using parameters? - php

I am currently starting an integration with Woocommerce, where I need to just use your payment gateway via an embedded website.
The total amount, name of the customer purchasing, and other data I send to you via URL:
https://www.example.com/shop/checkout/?p_amount=100&p_user=jonhy
The syntax to obtain the parameters within the web is:
if (! empty ($_GET["p_amount"]) and ! empty ($_GET["p_user"]) )
{
$v_amount = $_GET["p_amount"];
$v_user = $_GET["p_user"];
echo $v_amount . "<br>" ;
echo $v_user . "<br>" ;
}
else
{
echo "problem with variables";
}
The above works without problem, I manage to get the parameters and display them by the page in wordpress or woocommerce.
I need to pass the value of the "Amount" parameter to the Woocomerce shopping cart.
I also need the shopping cart to have a default product so that when opening it from the embedded web, Woocommerce does not return that the cart is empty.
I am only interested in processing the total that I send via parameters.
Is it possible to do this? how can I do it?
Thank you very much for your time, I will be attentive to your answers.
Regards.

Related

How to get WooCommerce cart subtotal value?

I am currently trying to make an if else check in the checkout of a WooCommerce site.
I need to know if the total is greater or less than 100, so that it will say "You need to call to negotiate a shipping fee".
The code:
$woocommerce->cart->get_cart_total()
shows the value, but adds HTML content.
I need only the value itself.
I kind of found out how to do this the hard way.
I searched google for over 2 hours and found this page: https://woocommerce.wp-a2z.org/oik_api/wc_cartget_cart_subtotal/
global $woocommerce;
$subtotal = $woocommerce->cart->get_subtotal();
$subtax = $woocommerce->cart->get_subtotal_tax();
$subtotaltax = $subtotal+$subtax;
echo with $subtotaltax shows the value with the tax added.

Get magento shipping amount from each ordered item

I am trying to get shipping and handling amount. I was trying the snippet code below but it's not working. I am successfully sending some data to third party API after the place order .
echo $item->getShippingAmount();
but if I tried with.
echo $order->getShippingAmount();
It works but it shows total of shipping amount of all orders, but conversely I want each single amount to be shown.
item ordered * quantity
Can anybody tell me how to do that?
Maybe you can use
$order->getShippingIncTax()
Try with below code :
for magento2 :
foreach ($order->getAllItems() as $item) {
echo $item->getShippingAmount();
}

Send custom data in woocommerce webhook

I have a store working with woocommerce plugin for wordpress and everytime a customer purchase something I record that sale in an internal software I'm using to track my shipments and inventory. To do this I'm using the webkooks feature of woocommerce and everything works fine, but now, I want to send some custom data in the webhook request. I have tried using "custom fields" or "product attributes" in my product but neither those fields are sent by the webhook request. So my question is, is there a way I can send aditional data in the webhook?
Thanks in advance.
I got it working, I used the following code to achieved it:
function add_order_item_meta($item_id, $values) {
$key = 'an_identifier';
$value = get_post_meta( $values['product_id'], 'custom_field_name', true );
woocommerce_add_order_item_meta($item_id, $key, $value);
}
add_action('woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 2);

Unable to clear the cart after paypal checkout Opencart 2.0.3.1

I am working on opencart 2.0.3.1 and I run into this issue, when using Paypal standard payment method:
When a user checks out using paypal, his cart does not get cleared , even though the order gets placed.
The only way user's cart gets cleared is if he returns to the success page manually, however it's an unreliable method, because most users don't bother to comeback to merchant's site.
I am looking for a solution from one of these options:
An opencart extension which adds a functionality to clear the cart during the payment process
A block of code that clears the cart, so that I can paste it into the paypal payment processing file.
Solution:
Add this line of code
$this->cart->clear();
to this file: catalog/model/checkout/order.php
Go to system/library/cart.php
Replace
$this->session->data['cart'] = array();
with
if (isset($this->session->data['customer_id'])) {
$customer_id = $this->session->data['customer_id'];
$this->db->query("UPDATE " . DB_PREFIX . "customer SET cart = '' WHERE customer_id = '" . (int)$customer_id . "'");
}
You can also write a Vqmod for it, if you do not feel comfortable hardcoding your system files.

Magento - Insufficient Stock Notification on Product Page

I am using Magento 1.7.0.2. Whilst on the product page, if a customer attempts to add a quantity greater than we have in stock they receive a message stating ".. the requested quantity is not available".
Is there any way for magento to either email or log when this occurs? I.e. I receive an automatic email stating a customer has attempted to add X number of item X? This would allow me to identify lost sales due to us not having enough stock of a particular item?
Has anyone come across anything like this before or is this even possible?
Thank you in advance
Mike Prentice
yes this is possible
You have to code for this.
I came across this problem one time and what i have do like this below.
I have make one observer event to check if customer is requesting quantity more then available if so i sent email to admin.
What you can do is create one observer for chekout_cart_add_before event in this event you can put your logic.
Or otherwise you can use magento feature Backorders you can find this in inventory tab,if you enable this then customer can order even requested quantity > available quantity, customer can see one message in cart page about backorder.
There is no standart functionality to notify about low quantity products by email.
But there is RSS notification http://www.magentocommerce.com/wiki/modules_reference/english/mage_adminhtml/system_config/edit/cataloginventory
Extend this functionality to match your needs.
You could write some script which would parse RSS, and send email etc.
EDIT
Here is some extension you may like http://www.magentocommerce.com/magento-connect/low-stock-email-notification.html
But is is not free.
Here's how I've done it so that it sends a google analytics tracking event whenever a customer tries to order more than the available stock level.
First copy: app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
To: app/code/local/Mage/CatalogInventory/Model/Stock/Item.php
so that you're not modifying a core file.
In app/code/local/Mage/CatalogInventory/Model/Stock/Item.php add this function
public function notifyOutOfStock($productId){
$session = Mage::getSingleton('checkout/session');
//Initialise as empty array, or use existing session data
$outOfStockItems = array();
if ($session->getOutOfStock()){
$outOfStockItems = $session->getOutOfStock();
}
try {
$product = Mage::getModel('catalog/product')->load($productId);
$sku = $product->getSKu();
if($sku){
//Add the current sku to our out of stock items (if not already there)
if(! isset($outOfStockItems[$sku]) ) {
$outOfStockItems[$sku] = 0;
}
}
} catch (Exception $e){
//Log your error
}
Mage::getSingleton('checkout/session')->setOutOfStock($outOfStockItems);
}
In that same file is another function called checkQuoteItemQty.
Inside that function you need to call your new function using $this->notifyOutOfStock($this->getProductId()); right after it sets each of the error messages and before the return statement.
So:
public function checkQuoteItemQty($qty, $summaryQty, $origQty = 0)
{
....
if ($this->getMinSaleQty() && ($qty) < $this->getMinSaleQty()) {
$result->setHasError(true)
->setMessage(
$_helper->__('The minimum quantity allowed for purchase is %s.', $this->getMinSaleQty() * 1)
)
->setQuoteMessage($_helper->__('Some of the products cannot be ordered in requested quantity.'))
->setQuoteMessageIndex('qty');
//** Call to new function **
$this->notifyOutOfStock($this->getProductId());
return $result;
}
.....
->setQuoteMessageIndex('qty');
//** Call to new function **
$this->notifyOutOfStock($this->getProductId());
return $result;
.....
What this does is add your product sku to an array in the checkout session.
This means you will have access to that info in the template file right after your page loads displaying the "Insufficient stock" notification.
So in one of your template files you can add some code to render the necessary JavaScript.
I've chosen header.phtml since it loads on every page. (Users can add quantities of items to the cart in the cart page as well as the product view page).
app/design/frontend/CUSTOMNAME/default/template/page/html/header.phtml
Somewhere down the bottom of the code add this:
<!-- GA tracking for out of stock items -->
<script>
try {
<?php
$session = Mage::getSingleton('checkout/session');
if ($session->getOutOfStock()){
$outOfStockItems = $session->getOutOfStock();
foreach($outOfStockItems as $sku=>$value) {
if($value==0){
//Render the GA tracking code
echo "_gaq.push(['_trackEvent', 'AddToCart', 'ProductQtyNotAvailable', '".$sku."']); \r\n";
//Set it to 1 so we know not to track it again this session
$outOfStockItems[$sku] = 1;
}
}
//Update the main session
Mage::getSingleton('checkout/session')->setOutOfStock($outOfStockItems);
}
?>
}
catch(err) {
//console.log(err.message);
}
</script>
Can confirm this works well and in my opinion is better than an email or RSS feed as you can analyse it along with the rest of your analytics.

Categories