WooCommerce - Updating Product through API / HTTP Requests - woocommerce

I am attempting to update WooCommerce products through notifications sent by a third party application. I have read the documentation on doing this and it seems easy enough, but it looks like the only way to use the API to update a product is by Product ID. Unfortunately, these are/were created incrementally while uploading the rather large product catalog into WooCommerce.
While making the .csv's and uploading to WooCommerce, we set the SKU's to be the same ID that the products have in the third-party software (book ISBN's). So I am able to send that number (the number that corresponds with SKU in WC) with the HTTP request, but that does not allow me to perform the product update.
It seems to me, the best way to accomplish this integration would be to change the product ID's of all my products in WooCommerce to be the same as this unique number that is currently the SKU. It doesn't appear as though this is possible to do in the dashboard or user interface, so I imagine I'll have to do it on a database level.
Does anyone know what tables will need to be modified or if there is a way to do this in the dashboard? Maybe even a tweak to my HTTP request to the API that will allow me to update by SKU?

Modifiy the Product IDs in the database directly is not a good choices.
This action is required to do every time when creating new products.
The most simplest and safe appoach is creating a new API,
which purpose for searching the product ID by SKU.
Here is a part of sample code.
function get_product_by_sku( $sku ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $product_id ) return new WC_Product( $product_id );
return null;
}

Related

Trigger email to both customer and store owner when woocommerce order status is set to 'processing'

I am currently using a code similar to what I need, from LoicTheAztec ( who should really be called "DaMan"). That code triggers Woo to send an email to a custom email address when order status is changed to "Processing". Without that code, a notification email only goes to the customer.
For my case I want to send a notification to both the customer, and the store owner. I was able to tweak previous code to do this - but the result was that both the customer and owner get the same email ( as in, they are both copied on exact same notice from Woo).
Here is what I am currently using in my functions.php:
// notify when order status set to processing
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'processing_order_replacement_email_recipient', 10, 2 );
function processing_order_replacement_email_recipient( $recipient, $order ) {
// Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
$recipient .= ',example#owneremail.com';
return $recipient;
}
How could I trigger this so that the customer gets their copy, and the owner gets her copy separately? Also typically the stock emails generated by WOO have slightly different text versions between customer and store owner.
If what I am asking can't be done, I'll stick with what I have. Just wondering if there is a cleaner way to achieve it. Thanks in advance for any assistance!

Checking a guest customer's email address before order is processed to prevent them from purchasing multiple sample products

Hopefully there's a woocommerce veteran familiar with the checkout process that can help me with this. My store offers a sample product at a discounted price, but I want to limit this to a one-time order per customer. I've found a solution that works for registered users but I was wondering if there's another way to make this work without forcing guests to set up an account before ordering the product.
What I was thinking was if there's a way to check if the customer's billing email address has ordered this particular product at the checkout stage. So they'd fill out the checkout form and click "Proceed" but before the customer is taken through to the payment gateway their email address would be checked for ordering this particular product before, if so, direct them to a "order failed" page rather than through to the payment gateway.
Thanks in advance!
You have two of ways to achieve this functionality:
You can validate email on checkout page itself. Scan for orders with customer/guest's email address and if found with free product, then validate. This way an unnecessary order will not be recorded in the system.
Here is how you can have custom checkout validation.
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ( $_POST['billing_country'] == "NO" )
if (!$_POST['notes'])
$woocommerce->add_error( __('Please add the required information in "Order notes"') );
}
Alternatively, you can let customer place order and when order is placed, then scan for it if it has already ordered a sample product. And if found, then you can make status of that order as failed. - But this seems a bit illogical.

How to delete woo commerce order pragmatically?

I am trying to delete an order from database i was tried using bellow function but this is not working.can any body tell me how to delete an order from db using order id.
<?php wc_delete_order_item( absint( $order_id ) ); ?>
WooCommerce Orders is custom post type, therefore you can use wordpress functions for performing any operation like,
wp_delete_post($order_id);

How to find hacked Wordpress pages containing spam

No sooner is my website fully functional that it gets hacked. If you do a Google search with the link below, the results show spam keywords in the pages throughout such as "casino", "blackjack", "slot", "deposit", etc. But if I look through the pages, I don't see any of the spam.
Here is the Google search result:
https://www.google.ca/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site%3Arichardrosenman.com%20richard%20rosenman
I am not very advanced with Wordpress or PHP and I have tried searching. Can anyone help me figure out where it was hacked and how I can clean and protect it?
Since 'social.png' is included and there are spam keywords you probably are experiencing a CryptoPHP hack. There's much to find about it (in combination with Wordpress) and, fortunately, how to clear it.
Same happend with one of my client, he has only 15 posts in backend but google shows some wierd keywords and post which is crawed. To check this kind of posts/pages you have to look in the database. wp_posts table holds all the custom post types, pages etc, and display only the post that has publish status in post_status section.
To retrive all the published pages or post use the following query.
// will display all posts/pages etc.
SELECT * FROM `wp_posts` WHERE `post_status` = "publish"
// Display only Pages
SELECT * FROM `wp_posts` WHERE `post_status` = "publish" and `post_type` = "page"
// Display only posts
SELECT * FROM `wp_posts` WHERE `post_status` = "publish" and `post_type` = "post"
Now you know how to filter data, every record from this filtered data has a column post_author that holds the ID of the user that published that post/page.
Go to the wp_users table and check your user_login name and get the ID of your administrator account. And again filter the run a query in wp_posts table to filter the anonymous users data.
// Make sure you have only one administrator, replace 1 with your user id
SELECT * FROM `wp_posts` WHERE `post_author` not in (1)
// If you have multiple administrator then use the following, replace 1,5,6 with admin ID's
SELECT * FROM `wp_posts` WHERE `post_author` not in (1,5,6)
And final step is to delete that.
Important: Before Apply this action make sure to take backup first, so just in case you can retrive that data later.
Hope this help you.

Code to manually apply an Email restriction on a coupon in woocommerce

So I have an e-commerce site using wordpress and woo-commerce, now using the smart-coupon woocommerce plugin extension is there a way for me to create a credit coupon/gift-card (that initially has no email restriction) and manually assign the email of the current user as the email restriction for it the first time the gift-card is being used.
I'm trying to create a vooucher system using the giftcard as vouchers to purchase products in my store.
Since you don't have any code to show what you were using, try this.
$coupon_code = 'some-code-I-created';
$user = wp_get_current_user();//Get the current user object
$coupon = new WC_Coupon( $coupon_code );//Get the coupon object
$emails = $coupon->get_email_restrictions();//Returns an empty array or array of emails
$emails[] = strtolower($user->billing_email);//Add user's billing email address to the array
$emails = array_filter($emails);//Remove empty values
array_unique($emails);//Remove any duplicate values
$coupon->set_email_restrictions($emails);//Set the coupon's array with the updated array
$coupon->save();//Save the coupon
In case I'm adding email addresses in bulk (like using array_merge() instead of adding one address at a time), it's always good to make sure the array is clean by removing any empty values or duplicate values before updating the coupon object with it.
Also, WooCommerce compares the billing address in all lowercase but not the restricted emails in the coupon, so be sure when adding it to your coupon that you add it as all lowercase so they validate correctly. Source. This bug may be fixed in the future, but until then...

Resources