I am trying to work out if it is possible to prepend a brand name to any shipping options available. I will be setting up a store whereby you can only order one brand at a time, I will have cart limitations built in to prevent mixing two brands together.
I have an external ordering system called Veeqo and without going into too much detail I need to prepend the brand name of the products in the cart to any shipping method selected so that I can filter orders by these shipping options. E.g.
BRAND-NAME UK Next Day
BRAND-NAME UK 3-5 Days
Can this be done ? If so, how ?
Realise I am asking a lot here but if anyone knows of a way to do this that would be much appreciated! :)
Perhaps a function which searches the first line item for "Brand" and then prepends this value to all shipping method titles. WooCommerce would only display relevant shopping methods to them depending on country etc.
Using a custom function hooked in woocommerce_package_rates filter hook, you will be able to prepend the cart item brand name to the shipping method label name.
As there are multiple ways to enable brands in WooCommerce, you will need to define the taxonomy used by the brand plugin that you have enabled in WooCommerce…
For the taxonomy to use, see: How to get the brand name of product in WooCommerce
add_filter( 'woocommerce_package_rates', 'prepend_brand_to_shipping_methods', 10, 2 );
function prepend_brand_to_shipping_methods( $rates, $package ){
// HERE define the taxonomy for product brand (depend of used plugin)
$taxonomy ='product_brand';
// Get the first cart item
$cart_item = reset($package['contents']);
// Get the product brand term name
$brand_name = wp_get_post_terms( $cart_item['product_id'], $taxonomy, ['fields' =>'names']);
$brand_name = reset($brand_name);
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ){
// Changing shipping method label name
$rates[$rate_key]->label = $brand_name . ' ' . $rate->label;
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
Related
I have found a plugin that creates customer specific pricing, but seems to be an external API call that seem to not be the solution I am looking for.
https://woocommerce.com/products/wisdm-customer-specific-pricing/
I was confused to how the cart works and how products are stored. But based on this post it showed me how to replace cart prices with customer ID and sku's being passed to the API.
I used this this ticket to start off my answer
So the big thing I needed to figure out is how to call the cart, loop through each product and grab it's product SKU then overwrite the price.
I added the following to my functions.php in my theme to change my cart to display the correct pricing based off of my customer API pricing. This does a loop through all my cart items then make a call to the API and returns a price for the specific customer and sku.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$response = get_api_response($value['data']->get_sku(), $customer_id);
$custom_price = $response->price;
$value['data']->set_price($custom_price);
}
}
In Woocommerce, I would like when an order is mad, to set automatically as "Featured" the purchased products (my default status for all kinds of orders is "on-hold").
So basically, if order is "on-hold", the ordered products should turn as "Featured".
Why I want this? Because I am using the "Featured" thing not in the way as intended by WC, but instead I am displaying a custom label for the sold out product (since I keep my sold out products visible in Shop for a good number of days after the purchase).
Thus, I don't want to manually mark as Featured every product once it is sold, instead I want this to be done automatically.
Could this be done using some WC hooks in my child theme's functions.php? Any help is welcome.
The following code will set to "featured" all the purchased products when the order status is set to "on-hold":
add_action('woocommerce_order_status_on-hold', 'order_status_on_hold_featured_products', 20, 2);
function order_status_on_hold_featured_products( $order_id, $order ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
$product->set_featured(true);
$product->save();
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.
I am creating an eCommerce store for a relative's business. He has products that the user must personalize on the website. I am coding a separate page that will allow the user to do this, that will link back to the store and add the item to the cart when they are done.
I found a solution to add each product to the cart separately, instead of adding to the quantity (since each product added has been personalized), and a solution to change the price of items in the cart.
WooCommerce: Add product to cart with price override?
https://businessbloomer.com/woocommerce-display-separate-cart-items-product-quantity-1/
The problem is that all the items are changed, not just the last one added.
For example: User designs a ballpoint pen with her name on it. The price is $4. The user then designs a gel pen with her name. The cart will show two separate pens with an increased calculated price, but the price will increase for all pens, not just the last one added. I need to find a way to set the price for the last item added, based on the price sent from the product building page. So far, nothing I've tried has worked.
Variable products won't work for this site.
//To display a product separately every time it is added
function bbloomer_split_product_individual_cart_items( $cart_item_data, $product_id ){
$unique_cart_item_key = uniqid();
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data','bbloomer_split_product_individual_cart_items', 10, 2 );
add_filter( 'woocommerce_is_sold_individually', '__return_true' );
//To change the item price programmatically. Changes all items with the same ID
//I need it to change only the last one added.
//Set to retrieve new price from the customizing page
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
/**
* #param $cart_obj
*/
function add_custom_price($cart_obj ) {
ob_start();
include 'createbb.php';
global $custom_price;
$target_product_id = 17;
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_obj->get_cart() as $key => $value ) {
if ($value['product_id'] == $target_product_id) {
$value['data']->set_price($custom_price);
}
}
ob_end_clean();
}
The type of functionality you are looking for is actually so common that it has a name "Product Options" (this is different to variations). This feature allows you to add configurable add-on's to the price of a product through a design on the edit product page. In a lot of eCommerce platforms like OpenCart you have this sort of functionality built in for free, however Automattic opted to keep it as a separate bolt on which they charge for:
https://woocommerce.com/products/product-add-ons/
Before you say it yes it is annoying that they charge for it, really this is such a common thing for people to request you can tell that it was a calculated decision to monetise it. And yes it is still very possible to program everything bespoke, but you are inevitably re-inventing the wheel for no reason other than to avoid paying for the feature (there are free versions of the plugin above that others have made, google is your friend).
Consider something, what if Automattic change their hooks in the future? you'll have to modify your code, assuming you find out before it borks the site. What if you wish to change options, or you need someone else to change them and you are not around?
If you weigh things up, you'll find the best option in the end is either to grab the official plug-in, or use one of the free alternatives. Trust me I am doing you a favour.
Koda
I'm using magento C.E 1.7. I use all shipping methods of magento and I'm viewing only one shipping method in the front end, which has higher priority. I'm using table rate shipping for some countries.
I wanted to add special fixed shipping price for some products when they shipped to that countries only. So that, I added new shipping method namely 'flat rate per product', by using a new module.
Based on this module, a text box is added to all the products in admin to specify the special shipping price. If I filled this field with some value, It considers it as a special shipping price. And if not filled it, it will be considered as special shipping price is $0.
So I have given least priority in the sort order for this shipping method to avoid this method from viewing on front end when products without special shipping price and products with special shipping price both are added into the cart.
So that, If only the special shipping priced product is added to the cart, It shows only table rate shipping method in front end.
I checked the code in availble.phtml. There is a foreach loop to proceed with available shipping methods. I need to check the availability of the 'flat rate per product' and it's price value for the product before start of loop execution.
I tried to get all the values of the array. But I could not. Can anyone help on this?
you can use this code:
<?php
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
$shipMethods = array();
foreach ($methods as $shippigCode=>$shippingModel)
{
$shippingTitle = Mage::getStoreConfig('carriers/'.$shippigCode.'/title');
$shipMethods[$shippigCode] = $shippingTitle;
}
return $shipMethods;
?>
I have used magento many times, but this is the ultimate challenge. Im working on a magento store which has over 400,000 products - each with its own variation / product options. Hundreds of products are added and removed daily on our master store (which is based on a custom shopping cart system and runs on MSSQL).
I have configured magento to grab all the categories, products, text, descriptio, prices, variations etc and create the product pages dynamically on the fly e.g http://www.offices-furniture.co.uk/pp?prod=mercury-reception-unit.html
The problem is I now need to be able to add these products to the shopping cart without them physically existing in the back end. I have added one product to the back end and plan to use this as a GENERAL template type product, so its always this product (variations of it) that get added to the cart e.g
http://www.offices-furniture.co.uk/frodo.php but I cannot for the life of me get the price to change.... grrrr..
If anyone could point me in the right direction on how to change the price via HTML or PHP on the front end and post it to the shopping cart without changing the price on the back end
Thanks in advance all…
Here is the code i’ve tried using to change the price;
<?php
require_once ("app/Mage.php");
umask(0);
Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));
// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product');
$product->setCustomPrice(99);
$product->setOriginalCustomPrice(99);
$product->getProduct()->setIsSuperMode(true);
$product->setTypeId('configurable');
$product->setTaxClassId(1); //none
$product->setSku(ereg_replace("\n","","videoTest2.2"));
$product->setName(ereg_replace("\n","","videoTest2.2"));
$product->setDescription("videoTest2.2");
$product->setPrice("129.95");
$product->setShortDescription(ereg_replace("\n","","videoTest2.2"));
$cart->save();
if(isset($_POST['submit'])){
// call the Magento catalog/product model
$product = Mage::getModel('catalog/product')
// set the current store ID
->setStoreId(Mage::app()->getStore()->getId())
// load the product object
->load($_POST['product']);
*/
////////////////////////////
// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product')
// set the current store ID
->setStoreId(Mage::app()->getStore()->getId())
// load the product object
->load($_POST['product']);
$product->setCustomPrice(99);
$product->setOriginalCustomPrice(99);
$product->getProduct()->setIsSuperMode(true);
$product->setTypeId('configurable');
$product->setTaxClassId(1); //none
$product->setSku(ereg_replace("\n","","videoTest2.2"));
$product->setName(ereg_replace("\n","","videoTest2.2"));
$product->setDescription("videoTest2.2");
$product->setPrice("129.95");
$product->setShortDescription(ereg_replace("\n","","videoTest2.2"));
$cart->save();
/////////////////////////////////////
// start adding the product
// format: addProduct(<product id>, array(
// 'qty' => <quantity>,
// 'super_attribute' => array(<attribute id> => <option id>)
// )
// )
$cart->addProduct($product, array(
'qty' => $_POST['qty'],
'price' => 50,
'super_attribute' => array( key($_POST['super_attribute']) => $_POST['super_attribute'][525] )
)
);
// save the cart
$cart->save();
// very straightforward, set the cart as updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
// redirect to index.php
header("Location: frodo.php");
}else{
?>
Here is a snippet which might help you out... you can definitely change the price on the fly when a product is added to the cart. I'm using this on the checkout_cart_save_before event observer, and in this example it triggers when the weight of an item is over 10 lbs.
/**
* Set the price if the weight is over 10 lbs
*
* #param Varien_Event_Observer $observer
*/
public function setPriceOnCartSaveBefore(Varien_Event_Observer $observer)
{
$cart = $observer->getCart();
$items = $cart->getItems();
foreach($items as $item) {
if ($item->getWeight() > 10) {
$item->setCustomPrice(69.99);
$item->setOriginalCustomPrice(69.99);
}
}
}
Your question is not completely clear. There are two possible ways of interpreting this (of which I'm guessing you mean the second, because the first problem has a relatively easy solution):
1) You only need the custom price in the shopping cart, but it doesn't need to last through checkout
2) You do actually need to be able to sell the product for the custom price using the Magento checkout.
Ad 1: Only change price in shopping cart
This is relatively easy. I would use JavaScript and a custom PHP script that is accessible through AJAX and can calculate the price that should be displayed. This can then be done through DOM manipulation. CSS can help you hide the price until the AJAX calculation is finished.
Another way of doing it would be to edit the price template file. Because Magento phtml files get called within the View class of the object that is currently rendering (e.g. cart or quote), you will be able to get the ProductID. You can then check if the Product that is being added is your magic custom template product and change the price accordingly.
In the base/default template you would get the item ID as such in base/default/template/checkout/cart/item/default.phtml
$product_id = $_item->getProduct()->getId();
When you figure out what combination of Weee, InclTax etc. you have used for your website (so you know where in default.pthml your price actually gets displayed), you can get an if statement in there and display the custom price somehow.
Ad 2: Keep price changed through checkout
I don't think it's possible to do this. Especially with Magento obsessing about order integrity (being that products and their info will always be available through that order, even if you delete them from the catalog).
The closest thing you'll get to this (at least as far as I can imagine) is to have the template product you set up include a custom drop-down option that enables using a variable price.
You could try to set the price of the only value for that custom drop-down option dynamically, but I doubt even that would work. The last thing you could try then is to add a value (with your custom price) to the custom option for that product every time an order is placed. That way, you keep Magento overhead to a minimum, but still satisfy Magento bureaucracy by providing Magento a way to keep a history of the physical product you have sold.
Another suggestion
There is also the possibility of using products with custom options the way they were meant to be used. If you create a basic template product with enough custom options (e.g. shirt size, color, print, fabric) and add new custom option values on the fly. This way you could also check if an option already exists and each option can have its own added price value.
Last suggestion
If you really want to go all-out, you could try writing a custom module for Magento that in turn:
Creates a product when it gets added to the basket.
Removes that product again when the order is finished or when a customer removes it from the basket.
Prunes the custom products periodically (e.g. through Mage/cron) provided they are not still in a stored basked for any of your customers.
This would create temporary products instead of no products at all.
I hope I have shared some thoughts that can help you move along!