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?
Related
i'm a bit stuck for this.. i'm trying to remove a condition whenever the user adds a specific product, in this case a box of wines
So when i add a bottle of wine there's a minium amount condition so you have to add 3, but when you add a box the condition must be removed
add_action('woocommerce_after_cart_contents', 'box_special_function', 1, 1);
function box_special_function()
{
// getting cart items
$cart = WC()->cart->get_cart();
$terms = [];
// Getting categories of products in the cart
foreach ($cart as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$terms[] = get_the_terms( $product->get_id(), 'product_cat' );
}
// Cycling categories to find if there is a box inside of the cart
foreach($terms as $term => $item){
foreach($item as $key){
if($key->name == "BOX"){
// The only thing i did is to remove notices (which doesn't even work .-.)
$notices = WC()->session->get('wc_notices', array());
foreach($notices['error']){
wc_clear_notices();
}
}
}
}
}
I can't even force to checkout so i'm stuck with this.. can somebody clear my mind?
You can update your minimum product add to cart functionality, and remove validation for your specific products like this:
add_filter( 'woocommerce_quantity_input_args', 'custom_woocommerce_quantity_changes', 10, 2 );
function custom_woocommerce_quantity_changes( $args, $product ) {
$product_id = $product->get_id();
$product_title = $product->get_title();
//echo "<pre>"; print_r($product_title); echo "</pre>";
if($product_id == 1002 || $product_title == 'BOX'){
$args['input_value'] = 1;
$args['min_value'] = 1;
$args['max_value'] = 30;
$args['step'] = 1;
}else{
$args['input_value'] = 3; // Start from this value (default = 1)
$args['max_value'] = 30; // Max quantity (default = -1)
$args['min_value'] = 3; // Min quantity (default = 0)
$args['step'] = 1; // Increment/decrement by this value (default = 1)
}
return $args;
}
print your specific product id or product name and use same name or id in your if condition :
if($product_id == 1002 || $product_title == 'BOX')
For rest of product set min, max and input product validation from "else" parts.
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;
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.
I'm writing a plugin and cannot get the correct variation title
In Woocommerce the variation i use is called "unfolded laminated". but when i try to get the title of a variation i get: "Variation #781 of Chart"
This is the code is use:
foreach ($order->get_items() as $item) {
$product_variation_id = $item['variation_id'];
if ($product_variation_id)
{
$product = new WC_Product($product_variation_id);
$productname = get_item_variations($product_variation_id);
}
else
{
$product = new WC_Product($item['product_id']);
$productname = get_the_title($item['product_id']);
}
}
How do i get the correct title?
I was actually looking this up trying to figure it out and here's my solution because the variation title was returning a slug.
Tested on WC 2.4.13
$variation = new WC_Product_Variation($variation_id);
$title_slug = current($variation->get_variation_attributes());
$results = $wpdb->get_results("SELECT * FROM wp_terms WHERE slug = '{$title_slug}'", ARRAY_A);
$variation_title = $results[0]['name'];
I stumbled across this just now looking for a 'quick' fix. Since no-one has posted an answer I thought I would.
I just used WordPress get_post_meta to fetch attribute_options. This is where all the option titles are stored (variation titles are product options).
$product_options = get_post_meta($item['variation_id'], 'attribute_options');
Will return an array
[attribute_options] => Array
(
[0] => Some Variation Title
)
Here is a solution for this problem which is tested with WooCommerce 3.2.6
//Loop through each item from the cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//get the current product ID
$product_id = $cart_item['product_id'];
//first - check if product has variations
if(isset($cart_item['variation']) && count($cart_item['variation']) > 0 ){
//get the WooCommerce Product object by product ID
$current_product = new WC_Product_Variable($product_id);
//get the variations of this product
$variations = $current_product->get_available_variations();
//Loop through each variation to get its title
foreach($variations as $index => $data){
get_the_title($data['variation_id']);
}
}
}
In this function to return the title of the input variation, you can change the delimiter ', ' to a space or whatever is needed.
function get_variation_title($variation) {
$str_name = '';
$arr_attributes = $variation -> get_variation_attributes(FALSE);
if (is_array($arr_attributes)) {
foreach($arr_attributes as $attribute_slug=> $variation_slug) {
$term = get_term_by('slug', $variation_slug, $attribute_slug);
if (isset($term)) {
if (strlen($str_name) > 0) $str_name.= ', ';
$str_name.= $term -> name;
}
}
}
return $str_name;
}
I am using the below code and want to increase the quantity in session array but its not working
if(!empty($_SESSION['quote']))
{
$prdata=$_SESSION['quote'];
}
$product_id = $data['id'];
$product_image = $data['image'];
$product_name = $data['pname'];
$product_quantity = $data['quantity'];
$product_price = $data['price'];
foreach ($_SESSION["quote"] as $cart_itm){
if($cart_itm['product_id'] == $product_id){ //the item exist in array
$newdat = array('product_id' => $product_id,'quantity'=>$product_quantity,'price'=>$product_price,'name'=>$product_name,'image'=>$product_image);
$found = true;
}else{
$newdat = array('product_id' => $product_id,'quantity'=>$product_quantity,'price'=>$product_price,'name'=>$product_name,'image'=>$product_image);
}
You don't need to put there product_id etc. again, change just amount. Your code will be shorter, clearer and you avoid to other possible typos, etc.
if($cart_itm['product_id'] == $product_id) {
$cart_itm['quantity'] += $product_quantity; // expect in $product_quantity is the amount you want to add to the current one
// it's a shorter variant of $cart_itm['quantity'] = $cart_itm['quantity'] + $product_quantity;
}