Woocommerce - Email text based off product category - wordpress

I was wondering if there is a way to separate my processing orders email via the purchased products category.
I know that's not very clear :P
For example:
I have two products. Test1 and Test2. Test1 has the category 'ICT Sale', while Test2 has 'Private Sale'.
If someone purchases Test1 I would like the email to say 'Thank you for buying an ICT product'
If someone purchases Test2 I want it to say 'This is a private sale!'
Unfortunately I'm more fluent with JS than PHP and WP hooks (and I'm assuming a if / else statement won't work here :P)
Thanks for any help in advance!

You can copy the email template in your theme and then do what ever you want.
For exemple, copy woocommerce/templates/emails/customer-processing-order.php to themes/yourtheme/woocommerce/emails/customer-processing-order.php
then edit customer-processing-order.php something like this should to your trick (you need to complete then to display whatever you want)
$ict = false;
$private = false;
foreach ( $order->get_items() as $item ){
$categories = get_the_terms( $item['product_id'] , 'product_cat' );
foreach( $categories as $categorie ) {
if ($categorie->slug == 'ICT') {
$ict = true;
}elseif( $categorie->slug == 'private-sale-slug' ) {
$private = true;
}
}
}

Hi bro Please go to wocommerce product page (wp-admin/edit.php?post_type=product) and
click on the quickedit of test2 product. Here see you selected checkbox on private so
comout message ('This is a private sale!') when purchage product test2 .please remove
selected checkbox on private.

Related

Is there a hook that precedes Woocommerce API's duplicate SKU check?

I have a site with Woocommerce and WPML + Multilingual Woocommerce installed. My problem is that I try to insert a product as a translation of a previously entered product without being aware of the ID of the main product. If I enter the ID as translation_of it works; both products share the same SKU and the translation has the SKU field disabled, which is how I want it to work. But I don't want to enter translation_of into the data that gets sent to Woocommerce. I want to only use the SKU and then let Wordpress first check if a product with that SKU already exists and replace sku with translation_of if it does.
This is how I went about it:
add_filter('woocommerce_api_create_product_data', '__create_product_data', -100, 2);
function __create_product_data($data, $api) {
if(isset($data['sku']) && $product_id = wc_get_product_id_by_sku($data['sku'])) {
$product_id = apply_filters('wpml_object_id', $product_id, 'product');
$data['translation_of'] = $product_id;
unset($data['sku']);
}
return $data;
}
But it seems to me that execution arrives at this point long after the SKU has been checked, because I noticed that I can return nothing and I still get product_invalid_sku error back. What would be the correct hook or does such a hook even exist?
My own solution:
add_filter('rest_pre_dispatch', '__rest_pre_dispatch', 10, 3);
function __rest_pre_dispatch($result, $server, $request) {
$sku = $request->get_param('sku');
if ($sku) {
$id = wc_get_product_id_by_sku($sku);
if ($id) {
$product_id = apply_filters('wpml_object_id', $id, 'product');
$request->set_param('translation_of', $product_id);
$request->offsetUnset('sku');
}
}
return $result;
}

Woocommerce - Edit account issue

My Woocommerce is setup to generate username automatically. I'm trying to use the code below to change username before save. I would like to change user name to be equal a custom field filled in billing form.
My code is:
function wc_cpf_as_username ( $user_login ) {
if( !empty($_POST['billing_cpf'] ) ) {
$user_login = $_POST['billing_cpf'];
}
elseif (!empty( $_POST['billing_cnpj'] )){
$user_login = $_POST['billing_cnpj'];
}
else{
$user_login = $_POST['billing_email'];
}
return $user_login;
}
add_filter( 'pre_user_login' , 'wc_cpf_as_username' );
The code work to create user, but this code do not work to edit user in my account page (/my-account/edit-account). Woocommerce show success message (Account details changed successfully.), but data is not changed.
I do not know what is the issue.
Could you help me?
Why you are making that complex function if you have a hook available for this. edit_user_profile_update hook i.e. located in /wp-admin/user-edit.php.
update_user_meta($user_id, 'custom_meta_key', $_POST['custom_meta_key']).
update_user_meta thats for update user meta field based on user ID.
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
update_user_meta($user_id, 'Custom_field', $_POST['your_field']);
}

How to make Woocommerce checkout page fields to read-only

For edit woocommerce (WordPressss plugin) account page fields and make to read-only, easy edit woocommerce/templates/myaccount/form-edit-account.php file and only add read-only to end of fields tags end (sorry my English and programming is not professional and good)
But for edit woocommerce checkout page fields and make one, or two and more fields, for example, email field, to read-only, cannot do the previous way
Please help me to do it, I have not idea.
It would be much better if, teach me to do it from functions.php easy and better and durable way (if it is possible).
thank you again
You can use the following code in your theme's "function.php" file.
add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();;
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
if($key == 'billing_address_1' || $key == 'billing_address_2'){
$key_value = get_user_meta($user_id, $key, true);
if( strlen($key_value)>0){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
This function will check if the address fields have value (i.e. if the address is specified), and if it has value, makes the field/s readonly. Else keeps the fields open to add data for the user. You can apply the same concept for each required fields.
Hope this helps.
Thank You!
very thank you for answer mr amritosh pandey
this code work correctly
for only email field , remove:
if($key == 'billing_address_1' || $key == 'billing_address_2'){
and i change to
if($key == 'billing_email'){
and work perfect and correctly
my new queston is > if use for two field , example: mobile (tel) and email or more field
Should i repeat all codes?
from add_action to return $checkout_fields; }
or can repeat
if($key line 2 or 3 or more?
example:
...
...
if($key == 'billing_email'){
if($key == 'billing_phone'){
if($key == 'billing_postcode'){
...
...
thank you

Woocommerce "No Shipping methods" message: Customise message based on the Zipcode customer enters

Our store has been setup to process orders only within Sydney city. We manage this in Woocommerce by setting the allowed postcodes for a Flat Rate Delivery.
We are now extending deliveries to other other areas but only via telephone (no online orders for these new postcodes). Woocommerce displays a standard No Shipping message but we would to customise such that
Customer enters a postcode within the city, allow online orders. No
change to current behaviour.
Customer enters a postcode for which
telephone orders are allowed, show a customised message asking the
customer to make the call.
All other postcodes, disallow orders.
Any technical direction will be greatly appreciated.
Thanks.
Below code should help you. Change the value of array variable $zip_array in both the functions as a comma separated list of zip codes, which you want to show a custom message. Also, change the string value of $custom_msg to your custom message. More details, please refer this article.
// For Cart Page.
add_filter( 'woocommerce_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
// For Checkout page
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
function wf_customize_default_message( $default_msg ) {
$zip_array = array(
'30031',
);
if ( in_array( WC()->customer->get_shipping_postcode() , $zip_array) ) {
$custom_msg = "Call us for quotation - 1-800-XXX-XXXX";
if( empty( $custom_msg ) ) {
return $default_msg;
}
return $custom_msg;
}
return $default_msg;
}
add_filter('woocommerce_package_rates', 'wf_remove_shipping_options_for_particular_zip_codes', 8, 2);
function wf_remove_shipping_options_for_particular_zip_codes($rates, $package)
{
global $woocommerce;
$zip_array = array(
'30031',
);
if ( in_array( $woocommerce->customer->get_shipping_postcode() , $zip_array) ) {
$rates = array();
}
return $rates;
}

How do I get product category information using collections in Magento

I am trying to output all the products from our Magento shop - the following code works, however I also need to grab the category id & the parent category name too. Can anyone suggest how I can do this?
$product = Mage::getModel('catalog/product');
$productCollection = $product->getCollection()
->addAttributeToSelect('*');
foreach ( $productCollection as $_product ) {
echo $_product->getName().'<br/>';
}
In some instances $_product->getCategory() can return empty and cause an error.
A better solution is to fetch categories by ID:
$categoryIds = $_product->getCategoryIds();
foreach($categoryIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
echo $category->getName();
echo $category->getUrlPath();
}
Since products can be assigned to multiple categories, I think your concept may be a bit off unless you are loading a collection for each category. What do you anticipate seeing if there are multiple categories for a given product?
Regardless, from within a category page, you can use the following:
$currentCat = $_product->getCategory();
To get all categories to which this product belongs:
$categories = $_product->getCategoryCollection();
foreach($categories as $_category) {
// do something
}
Hope that helps. Thanks,
Joe

Resources