I'm trying to create a snippet at the checkout of woocommerce to take a dropdown list with the label course1 and add all product titles as selections.
as im new to php in general, im struggling to figure out how I would pull the names of the products in the cart.
any tips would be very much appreciated.
Thanks
You can get cart items from global $woocommerce object
global $woocommerce;
$items = $woocommerce->cart->get_cart();
echo "<select name='products-list'">;
foreach($items as $item => $values) {
$_product = $values['data']->post;
$price = get_post_meta($values['product_id'] , '_price', true);
echo "<option value=".$values['product_id'].">".$_product->post_title."-".$price."</option>";
}
echo "</select>";
Use this PHP snippet where you want to show dropdown list
Related
I want to display added product quantity in cart in single product page as text image
This is currently what i'm using in functions.php, I want to display the quantity number between.
add_action('woocommerce_before_add_to_cart_form', 'gripsquantity', 5);
function gripsquantity(){
echo 'Choose ';
echo 'Grips';
}
Thank you
Get cart object
$items = WC()->cart->get_cart();
Loop through cart items
foreach( $items as $item => $values ) {
if( $values ['product_id'] === $yourProductId ){
// Get product qty in cart
$quantity_in_cart = $values['quantity'];
}
}
I have written this to extract the product variation prices
global $product;
if ( $product->is_type('variable') ) {
function get_product_variation_price($variation_id) {
global $woocommerce;
$product = new WC_Product_Variation($variation_id);
return $product->get_price_html();
}
$product_variations = $product->get_available_variations();
$arr_variations_id = array();
foreach ($product_variations as $variation) {
$product_variation_id = $variation['variation_id'];
$product_price = get_product_variation_price($product_variation_id);
}
$amount = get_product_variation_price($product_variation_id);
} else {
$amount = str_replace(".", ",", $product->get_price());
}
What I am trying to achieve is that if the product is a variable product, the amount variable changes to what currently selected variants set price is, however, this will always get me the first variants price. How can I achieve that?
I don't see any reason to create a plugin for showing variation prices because this is the default from woocommerce. Can you share why are you creating this plugin? Does the default function not work on your site? If you are only looking to change decimals for prices, you can change these settings from currency options.
I'm currently working on a WooCommerce theme and attempting to add a sidebar to the product detail page.
I've been able to get the sidebar added (specifically, this one: http://woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)
Now, I'm trying to figure out how to add a class of "active" to the currently selected product and can't seem to figure it out?
In other words, how do I do something along the lines of if the current product id is equal to the product id in the sidebar add class="active" to the li?
I've made numerous searches and haven't been able to come up with anything useful, so I'm turning here.
Any help greatly appreciated!!
2017 Update - since WooCommerce 3:
global $product;
$id = $product->get_id();
Woocommerce doesn't like you accessing those variables directly. This will get rid of any warnings from woocommerce if your wp_debug is true.
If the query hasn't been modified by a plugin for some reason, you should be able to get a single product page's "id" via
global $post;
$id = $post->ID
OR
global $product;
$id = $product->id;
EDIT: As of WooCommerce 3.0 this needs to be
global $product;
$id = $product->get_id();
Since WooCommerce 2.2 you are able to simply use the wc_get_product Method. As an argument you can pass the ID or simply leave it empty if you're already in the loop.
wc_get_product()->get_id();
OR with 2 lines
$product = wc_get_product();
$id = $product->get_id();
Retrieve the ID of the current item in the WordPress Loop.
echo get_the_ID();
hence works for the product id too. #tested #woo-commerce
The correct method is:
global $product;
$id = $product->get_id();
Save the current product id before entering your loop:
$current_product = $product->id;
Then in your loop for your sidebar, use $product->id again to compare:
<li><a <? if ($product->id == $current_product) { echo "class='on'"; }?> href="<?=get_permalink();?>"><?=the_title();?></a></li>
your can query woocommerce programatically
you can even add a product to your shopping cart.
I'm sure you can figure out how to interact with woocommerce cart once you read the code.
how to interact with woocommerce cart programatically
====================================
<?php
add_action('wp_loaded', 'add_product_to_cart');
function add_product_to_cart()
{
global $wpdb;
if (!is_admin()) {
$product_id = wc_get_product_id_by_sku('L3-670115');
$found = false;
if (is_user_logged_in()) {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->get_id() == $product_id)
WC()->cart->remove_cart_item($cart_item_key);
}
}
} else {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->id == $product_id)
$found = true;
}
// if product not found, add it
if (!$found)
WC()->cart->add_to_cart($product_id);
} else {
// if no products in cart, add it
WC()->cart->add_to_cart($product_id);
}
}
}
}
I'm currently working on a WooCommerce theme and attempting to add a sidebar to the product detail page.
I've been able to get the sidebar added (specifically, this one: http://woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)
Now, I'm trying to figure out how to add a class of "active" to the currently selected product and can't seem to figure it out?
In other words, how do I do something along the lines of if the current product id is equal to the product id in the sidebar add class="active" to the li?
I've made numerous searches and haven't been able to come up with anything useful, so I'm turning here.
Any help greatly appreciated!!
2017 Update - since WooCommerce 3:
global $product;
$id = $product->get_id();
Woocommerce doesn't like you accessing those variables directly. This will get rid of any warnings from woocommerce if your wp_debug is true.
If the query hasn't been modified by a plugin for some reason, you should be able to get a single product page's "id" via
global $post;
$id = $post->ID
OR
global $product;
$id = $product->id;
EDIT: As of WooCommerce 3.0 this needs to be
global $product;
$id = $product->get_id();
Since WooCommerce 2.2 you are able to simply use the wc_get_product Method. As an argument you can pass the ID or simply leave it empty if you're already in the loop.
wc_get_product()->get_id();
OR with 2 lines
$product = wc_get_product();
$id = $product->get_id();
Retrieve the ID of the current item in the WordPress Loop.
echo get_the_ID();
hence works for the product id too. #tested #woo-commerce
The correct method is:
global $product;
$id = $product->get_id();
Save the current product id before entering your loop:
$current_product = $product->id;
Then in your loop for your sidebar, use $product->id again to compare:
<li><a <? if ($product->id == $current_product) { echo "class='on'"; }?> href="<?=get_permalink();?>"><?=the_title();?></a></li>
your can query woocommerce programatically
you can even add a product to your shopping cart.
I'm sure you can figure out how to interact with woocommerce cart once you read the code.
how to interact with woocommerce cart programatically
====================================
<?php
add_action('wp_loaded', 'add_product_to_cart');
function add_product_to_cart()
{
global $wpdb;
if (!is_admin()) {
$product_id = wc_get_product_id_by_sku('L3-670115');
$found = false;
if (is_user_logged_in()) {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->get_id() == $product_id)
WC()->cart->remove_cart_item($cart_item_key);
}
}
} else {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->id == $product_id)
$found = true;
}
// if product not found, add it
if (!$found)
WC()->cart->add_to_cart($product_id);
} else {
// if no products in cart, add it
WC()->cart->add_to_cart($product_id);
}
}
}
}
I'm setting up a dataLayer in the WooCommerce cart trying to pass the first item value to it. The code below passes both the values as one variable and that's not what I want - I want an array to be built for each item in the cart.
One array for the prices, and one for the quantity.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'firstItemUnitPrice': '<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$price = get_post_meta($values['product_id'] , '_price', true);
echo $price;
}?>'
});
</script>
The output from the code above is:
firstItemunitprice: 1144
There are two products in cart, one worth $11 and one $44.
I know how to separate them but I want them to be added as an array to the dataLayer. Help please?
I understand I need to loop through the cart, but I want each item price and each item quantity to be placed in a dataLayer.