I am trying to get the results from my Foreach in another echo way later then the foreach but I am stuck, any help would be much appericated.
$items = $order->get_items();
// Output the loop
foreach ($order->get_items() as $item) {
// Getting some information
$product_qty = $item['qty'];
$product_variation_id = $item['variation_id'];
$product = new WC_Product($item['product_id']);
// SKU
$SKU = $product->get_sku();
print_r($SKU);
print_r($product_qty);
print_r(' ');
}
// this gives all 3 quantities and all 3 sku, added a space at the end for easier reading
// this only gives the 1st entry of both variables but i need all 3 of both variables
echo '<a href="https://www.domainname.someurl'.$product_quantity .$SKU . '" target=_blank>';
echo "<p>TEXT</p></a>";
I hope it is clear like this, thanks
Well, you're overwriting the value of $SKU and product_qty on each iteration, maybe try storing them in an array? Like create an array outsite the loop and use array_push(created_array, array($SKU, $product_qty)). Hope it helps.
$items = $order->get_items();
$push = array();
// Output the loop
foreach ($order->get_items() as $item) {
// Getting some information
$product_qty = $item['qty'];
$product_variation_id = $item['variation_id'];
$product = new WC_Product($item['product_id']);
// SKU
$SKU = $product->get_sku();
array_push($push, array('sku'=>$SKU, 'qty'=>$product_qty));
print_r($SKU);
print_r($product_qty);
print_r(' ');
}
// this gives all 3 quantities and all 3 sku, added a space at the end for easier reading
// this only gives the 1st entry of both variables but i need all 3 of both variables
$link = '<a href="https://www.domainname.someurl';
for ($i=0;$i<count($push);$i++){
$link .= $push[$i]['qty'];
$link .= $push[$i]['sku'];
}
$link .= '" target=_blank>';
echo $link;
Try the below code
$items = $order->get_items();
// Output the loop
$product_qty_string = '';
$sku_string = '';
foreach ($order->get_items() as $item) {
// Getting some information
$product_qty_string .= $item['qty']."-";
// SKU
$SKU = $product->get_sku();
$sku_string .=$SKU."-";
}
$product_quantity = rtrim($product_qty_string,'-');
$SKU = rtrim($sku_string,'-');
echo '<a href="https://www.domainname.someurl'.$product_quantity.''.$SKU.''" target=_blank>';
echo "<p>TEXT</p></a>";
Why don't you add your values in a array and then implode them.
$items = $order->get_items();
// Output the loop
$SKU = $product_qty = [];
foreach ($order->get_items() as $item) {
// Getting some information
$product_qty[] = $item['qty'];
$product_variation_id = $item['variation_id'];
$product = new WC_Product($item['product_id']);
// SKU
$SKU[] = $product->get_sku();
print_r($SKU);
print_r($product_qty);
print_r(' ');
}
$sku_string = implode('-', $SKU);
$product_qty_string = implode('-', $product_qty);
echo '<a href="https://www.domainname.someurl'.$product_qty_string .$sku_string . '" target=_blank>';
echo "<p>TEXT</p></a>";
You can implode your data with any separator you want, i use "-" for my example but its depending on your needs.
Related
Hey I am trying to build a shortcode for my order details on the order received page.
The code below will generate the last result and then on top of it, it will display the word Array. My guess is that something in the foreach loop i am creating is still an array, but I dont know what to do next.
thanks for any help.
function getOrderItemList(){
//set up array and Count
$item_data = '';
//get order ID
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order = wc_get_order( $order_id );
$order_line_items = $order->get_items();
//loop through each item in the order
foreach ($order_line_items as $item) {
$product = $item->get_product();
$product_id = $item->get_product_id();
$item_data = $item->get_data();
$product_name = $item->get_name();
$item_quantity = $item->get_quantity();
$item_total = $order->get_formatted_line_subtotal( $item );
$product_image = $product->get_image('order-received-item-image', $item);
$item_data .= '<tr class="order-item-row"><td class="order-item-image">' . $product_image . '</td><td class="order-item-name"><p>' . $product_name . ' x ' . $item_quantity . '</p></td><td class="order-item-total"><p>' . $item_total . '</p></td></tr>';
}
$item_list = $item_data;
$table .= <<<EOD
<table class="test">
$item_list
</table>
EOD;
return $table;
}
add_shortcode('order-line-item', 'getOrderItemList');
It looks like you jumbled a few things up, and had some unused variables in there. Try this.
function getOrderItemList() {
// set up array.
$item_list = '';
// get order ID.
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order = wc_get_order( $order_id );
$order_line_items = $order->get_items();
// loop through each item in the order.
foreach ( $order_line_items as $item ) {
$product = $item->get_product();
$product_name = $item->get_name();
$item_quantity = $item->get_quantity();
$item_total = $order->get_formatted_line_subtotal( $item );
$product_image = $product->get_image( 'order-received-item-image', $item );
$item_list .= '<tr class="order-item-row"><td class="order-item-image">' . $product_image . '</td><td class="order-item-name"><p>' . $product_name . ' x ' . $item_quantity . '</p></td><td class="order-item-total"><p>' . $item_total . '</p></td></tr>';
}
return "<table class=\"test\">$item_list</table>";
}
add_shortcode( 'order-line-item', 'getOrderItemList' );
I have session to add product cart.
$getProductID = mysqli_real_escape_string($con, $_POST['productID']);
$getQuantity = mysqli_real_escape_string($con, $_POST['quantity']);
foreach($_POST as $key => $value)
{
$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}
$qProduct = mysqli_query($con, "SELECT * FROM tb_product WHERE productid = '" . $getProductID . "'");
$dProduct = mysqli_fetch_array($qProduct);
$product_id = $dProduct['productid'];
$product_name = $dProduct['product_name'];
$product_price = $dProduct['product_price'];
$new_product["productid"] = $product_id;
$new_product["product_name"] = $product_name;
$new_product["product_price"] = $product_price;
$new_product["quantity"] = $getQuantity;
if(isset($_SESSION["products"]))
{
if(isset($_SESSION["products"][$new_product['productid']]))
{
unset($_SESSION["products"][$new_product['productid']]);
}
}
$_SESSION["products"][$new_product['productid']] = $new_product;
$total_items = count($_SESSION["products"]);
foreach($_SESSION["products"] as $product)
{
$product_quantity = $product["quantity"];
}
die(json_encode(array('items'=>$product_quantity)));
Then now I want to get session of product quantity
foreach($_SESSION["products"] as $product)
{
echo $product_quantity = $product["quantity"];
}
I can get the quantity as well, until I try to add another product cart it's not sum the quantity.
Example, on Product1 I add to cart 5pcs (I can see the quantity is 5)
and then I add Product2 to cart 3pc (It show me 53 that should be 8).
My question, how to sum the quantity even Product ID is different?
Change your loop as below, Sum inside loop and move echo outside of loop.
$product_quantity = 0;
foreach($_SESSION["products"] as $product)
{
$product_quantity += $product["quantity"];
}
echo $product_quantity;
I think because your coding have something wrong
//Incorrect
foreach($_SESSION["products"] as $product)
{
echo $product_quantity = $product["quantity"];
}
//first loop echo-ed 5
//second loop echo-ed 3
//so it show 53
//Correct
$product_quantity = 0;
foreach($_SESSION["products"] as $product)
{
$product_quantity += $product["quantity"];
}
echo $product_quantity;
global $wpdb,$productname,$quantity,$total,$price,$product;
global $woocommerce,$product;
$items=WC()->cart->get_cart();
foreach($items as $item => $values) {
$cart_contents_count = WC()->cart->get_cart_contents_count();
if($cart_contents_count !=0){
$productname = $values['data']->get_title();
$productweight = $values['data']->get_weight();
$quantity = $values['quantity'];
$total = WC()->cart->subtotal;
$price = $values['data']->get_price();
}
I want to insert product name,productweight, quantity,total,price into database.
in the table every values inserted correctly but $productweight is always 0.
Can anyone help me please?
I use woocommerce on wordpress. This is my code
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo $_product->ID.',';
}
?>
And this is the result of teh code:
1297,1694,1297,3911,4999,
How can I get just last id '4999' ?
Storing the ids in an array and using the end() function would be a solution:
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$ids = array();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$ids[] = $_product->ID;
}
echo 'Last item = ' . end($ids);
?>
You can do it even simpler that what Akhilesh proposed - instead of iterating through $items array go to the last item there:
end($items)['data']->post->ID;
I am not sure, but probably you would need PHP 5.3+ for that.
How can I get the product description or the product object using product ID.
$order = new WC_Order($order_id);
foreach ($order->get_items() as $item) {
$product_description = get_the_product_description($item['product_id']); // is there any method can I use to fetch the product description?
}
The code above extends class WC_Payment_Gateway
Any help would be greatly appreciated.
$order = new WC_Order($order_id);
foreach ($order->get_items() as $item)
{
$product_description = get_post($item['product_id'])->post_content; // I used wordpress built-in functions to get the product object
}
If you are using WooCommerce 3.0 or above, then you can get description with the below code.
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$product_details = $product->get_data();
$product_full_description = $product_details['description'];
$product_short_description = $product_details['short_description'];
}
Alternate way (recommended)
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$product_instance = wc_get_product($product_id);
$product_full_description = $product_instance->get_description();
$product_short_description = $product_instance->get_short_description();
}
Hope this helps!