I'm trying to select the chosen_shipping_method price in woocommerce using this code:
$packages = WC()->shipping->get_packages();
foreach ($packages as $i => $package) {
$chosen_method = isset(WC()->session->chosen_shipping_methods[$i]) ? WC()->session->chosen_shipping_methods[$i] : '';
}
echo $chosen_method;
the code works, but it prints the ID, and i cannot figure out how to make it print out the price.
Here's package structure:
What i get with my code is table_rate_shipping_shipping_self_install, but what i need is the cost for the selected element, and not the id.
i tried to change the code like this:
foreach ($packages as $i => $package['rates']) {
$chosen_method = isset(WC()->session->chosen_shipping_methods[$i]) ? WC()->session->chosen_shipping_methods[$i] : '';
}
But it printed out the id, same as before.
Any ideas? sorry i'm a bit new to php.
Thanks in advance
Did you try this inside your foreach loop?
$rate = $package['rates'][$chosen_method]->cost;
If yo need to get price of shipping method that was chosen by user and stored in session, you should try this:
$chosen_shipping_method_price = WC()->session->get('cart_totals')['shipping_total'];
Related
I found this question, but no one gave a working answer:
Woocommerce Set Default Variations
What is mentioned here doesn't work anymore and I don't understand what the error is:
https://quadlayers.com/default-product-attributes-woocommerce/
Maybe someone knows how to make sure that when entering a variable product, at least some option is selected and the add to cart button is active?
The code that unfortunately no longer works:
add_action('woocommerce_before_single_product_summary', 'quadlayers_product_default_attributes');
function quadlayers_product_default_attributes() {
global $product;
if (!count($default_attributes = get_post_meta($product->get_id(), '_default_attributes'))) {
$new_defaults = array();
$product_attributes = $product->get_attributes();
if (count($product_attributes)) {
foreach ($product_attributes as $key => $attributes) {
$values = explode(',', $product->get_attribute($key));
if (isset($values[0]) && !isset($default_attributes[$key])) {
$new_defaults[$key] = sanitize_key($values[0]);
}
}
update_post_meta($product->get_id(), '_default_attributes', $new_defaults);
}
}
}
You should have the option to select a 'Default Form Values:' inside variations tab which lets you select the default option for your variant.
Like this:
I was wondering how to get all the items in my cart as an array.
I need this because I have to use array_intersect, this way I can compare them.
I haven't found anything about this on the internet.
You can access the cart contents through the WC()->cart object, like so:
<?php
$cart_contents = WC()->cart->cart_contents;
$cart_item_names = array();
foreach($cart_contents as $item) {
array_push($cart_item_names, $item['data']->post->post_title);
}
I'm trying to echo getSku() on the success page, but it returns 0. I can use getGrandTotal() or getId(), but I can't get getSku() to work. Any ideas?
The code:
$customer_email = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
$collection = Mage::getModel('sales/order')
->getCollection()
->getItemsCollection()
->addFieldToFilter('status', 'pending_payment')
->addAttributeToFilter('customer_email',array('like'=>$customer_email));
foreach($collection as $order){
//do something
//echo round($order->getGrandTotal(),2) . " ";
$sum += $order->getSku();
} echo $sum;
This is because the full product isn't being loaded. The following code should do the trick for you:
foreach($collection as $order){
// load the product from the ID
$product = Mage::getModel('catalog/product')->load($order->getProductId());
//do something
//echo round($order->getGrandTotal(),2) . " ";
$sum += $product->getSku();
}
I'm not sure if adding the SKU is what you want to do though as the SKU would usually be alphanumeric? Sorry if I've misunderstood!
so you can load that product using that id and then get sku as well.
$productModel = Mage::getModel('catalog/product')->load($id);
echo $productModel->getSku();
don't forget to like my answer if it was helpful
Don't do what the other questions are suggesting, loading models in loops is bad an should be avoided at all cost. Neither should you call getItemsCollection inside your loop.
Your current code doesn't even run, because there is no method called getItemsCollection inside an order collection. That's part of the order object.
Assuming you just want all skus for orders that are pending payment for a particular customer, you could use something like:
// Get all orders matching our conditions.
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status', 'pending_payment')
->addFieldToFilter('customer_email', array('like' => $customer_email));
// Get all items associated with those orders.
$itemCollection = Mage::getModel('sales/order_item')->getCollection()
->addFieldToFilter('order_id', array('in' => $orderCollection->getAllIds()));
// Now do whatever you want with your order item.
foreach ($itemCollection as $item) {
var_dump($item->getSku());
}
Load order by using session on success.phtml
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
Now load order
$order = Mage::getModel('sales/order');
$order->load($lastOrderId);
ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
//item detail
echo $item->getItemId(); //product id
echo $item->getSku();
echo $item->getQtyOrdered();
//ordered qty of item
echo $item->getName();
// etc.
}
Hi I'm trying to get a product attribute admin value or id (since it's multilanguage) from the cart items. I've tried many versions of code like this one:
$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllItems() as $item) {
$_product = Mage::getModel('catalog/product')->load($item->getId());
$attribute = $_product->getAttribute('producttype');
}
But I only ever get false or null. Also how can I be sure to not get the store specific language value, but the attributes admin value/id? Maybe there's an even better way to read out the item attributes directly from the quote items without having to load the product first? Thanks in advance!
Solved with:
$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllVisibleItems() as $_item)
{
$_product = Mage::getModel('catalog/product')->load($_item->getProductId());
$attributeId = $_product->getProducttype();
}
and comparing by value ID instead of text.
If you need to get the product's value for a specific shop, while the items in the quote belong to a different store view, you can do the folowing:
$_product = Mage::getModel('catalog/product')
->setStoreId($adminStoreId)
->load($item->getId());
$value = $_product->getData('producttype');
I want to retrieve all product IDs that has a specific (custom) option value (provided by the customer in the frontend) from all orders - regardless of store/website.
The option is a text field (Mage_Catalog_Model_Product_Option_Type_Text), the SKU (and title) is test_option and the value I'm looking for is green.
I would prefer to do this with a raw MySQL query, but a solution using Magentos models/collections would definitely suffice.
try with below code
<?php $collection = Mage::getResourceModel('sales/order_collection');
$productIds = array();
foreach($collection as $order) {
$order = Mage::getModel('sales/order')->load($order->getId());
$items = $order->getAllItems();
foreach ($items as $item){
$proOptions = $item->getProductOptions();
if($proOptions['options']){
$productIds[] = $item->getProductId();
}
}
}
?>
$productIds variable returns all product ids which has a specific (custom) option value.
May it helps you !
For direct sql query you can try this. If you want to put some condition, you can add according to your requirement.
SELECT sfoi.product_id FROM sales_flat_order e
LEFT JOIN
sales_flat_order_item sfoi
ON
( e.entity_id = sfoi.order_id )