I use a feed from a manufacturer to upload to a WooCommerce custom field.
The custom field expects a product id as input, but the feed is not aware of this as it's created by WooCommerce.
I use SKU as the key to compare existing products with the feed.
The imported field is called {metaparent_sku[1]} and the content has several values separated by | , for example:
BDCD7 | BDAUX | BDCA1 | ADUP89
These are unique product sku’s referring to products already in WooCommerce.
What I’d like to do is replace the sku value with the corresponding product ID value in as it exists already in WooCommerce.
I’ve been trying to use str_replace but can’t figure out how to set it up, if that's possible at all. Or should I use lookup?
If anyone could help with some sample code I'd be very grateful.
To find the product ID by a sku field.
[wc_get_product_id_by_sku({sku[1]})]
So in your case that would be:
[wc_get_product_id_by_sku({metaparent_sku[1]})]
For when you have multiple sku's in one field make a custom function.
Place the following function in the Function Editor.
/**
* Find product id(s) by sku(s)
* #param string $sku
* #return string with product id(s)
*/
function wp_all_import_get_product_ids_by_sku($sku, $seperator = "|") {
// define empty $product_ids
$product_ids = array();
// remove spaces from sku
$sku = str_replace(" ", "", $sku);
// convert to array
$skus = explode($seperator, $sku);
// find product ids
foreach ($skus as $sku) {
$product_ids[] = wc_get_product_id_by_sku($sku);
}
// return product ids
return implode(" | ", $product_ids);
}
Then use it like this
[wp_all_import_get_product_ids_by_sku({metaparent_sku[1]})]
Related
based off this link Payfast split payments which appears to work. How does one run a multi vendor woocommerce store and then split the payment to the associated merchant based on the product purchased?
Typically, this information is stored at the order item level. So, you need to loop through the items, get the vendor id and do your splitting payment action.
E.g. like this on the admin order page:
global $order;
$items = $order->get_items();
foreach ( $items as $item_key => $item ) {
$vendor_id = wc_get_order_item_meta( $item_key, '_vendor_id', true );
// Now get the merchant id with $vendor_id (=user id) and do split payment.
// You can also create an array with $merchant_id => $item->get_subtotal()
// and do the split payment after the loop.
}
The meta key "_vendor_id" depends on your plugin - I used WCMP (Multivendor Marketplace Solution for WooCommerce – WC Marketplace). You may need to look it up in your database how the field is called.
I want to update WooCommerce products by the WP All import. I have 2 independent xml files (from the first one I'm getting prices, from the second one descriptions). For some reasons product's _sku is {code} from the first one xml not {ean}. So I put {ean} into product attribute pa_ean to match the products from the first one and the second one xml.
Now I don't know how to write the php function to return product_id so WP All import could update right products with right descriptions.
Do you have any unique identifier that are same for the product in both files? Like product_key or something. If you have it you can store it as custom meta for that product and get product by that meta key, something like below ↓
function check_if_product_exist($rawDataFromFile) {
global $products;
// Get WC_Product Objects
$products = wc_get_products([
'meta_key' => 'your_meta_key',
'meta_value' => $rawDataFromFile->your_key
]);
if (!empty($products)) {
return $products[0];
}
return null;
}
I put {ean} to Custom field, not to Attribute and after that I can easily use it in WP All import.
I want to automatically add products to a category if their name contains the category (for example the product name is: Awesome brand baseball bat, the product should automatically be added to the category baseball bat). Is there a plugin that can automatically do this or even better: is it possible to add a rule to WP All Import to do this?
Setting the category like
$product->set_category_ids([ 300, 400 ] );
shouldn't be the problem, but how can I compare the articles names with all my categories so I can automatically add the products to them?
Load all product categories via get_product_categories( $fields );
Use get the product name to find the name of the product
Loop through the categories and compare each of them to the product name. Depending on your situation and what values are in product category title or product names, you may need to use a regex for this
When done, your code should look something like this:
$product_category_list = $product->get_categories();
$product_name = $product->get_name();
$categories_to_put_product_in = array();
foreach($product_category_list as $current_category) {
if (strpos($product_name, $current_category->term_id) !== false) {
$categories_to_put_product_in[] = $current_category;
}
}
$product->set_category_ids($categories_to_put_product_in);
I am trying to get the array for all the shipping forms with a new custom element that I already added. This code is not working:
$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');
Woocommerce WC_Checkout methods:
Visit https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#197-281 for more info!
https://superuser.com?
I get NULL so the object is not existing
In detail:
I'm developing a website, where the total price is calculated according to a delivery area in some city. I decided to create a custom field within the shipping-form:
// Adding districts for the city of Lima on shipping form
add_filter('woocommerce_checkout_fields', 'custom_district_checkout_field');
function custom_district_checkout_field($fields) {
//the list for this example was shortened
$option_cities = array(
'' =>__('Select your district'),
'chorrillos' =>'Chorrillos',
'miraflores' =>'Miraflores'
};
$fields['shipping']['shipping_district']['type'] = 'select';
$fields['shipping']['shipping_district']['options'] = $option_cities;
$fields['shipping']['shipping_district']['class'] = array('update_totals_on_change');
$fields['shipping']['shipping_district']['input_class'] = array('wc-enhanced-select');
$fields['billing']['billing_district']['type'] = 'select';
$fields['billing']['billing_district']['options'] = $option_cities;
$fields['billing']['billing_district']['input_class'] = array('wc-enhanced-select');
wc_enqueue_js("jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {var select2_args = { minimumResultsForSearch:5};jQuery( this ).select2( select2_args ).addClass( 'enhanced' );})");
return $fields;
I can confirm that the custome field is working. For other hand I'm trying to change the way the WooCommerce Advance Shipping plugin works as Jeroen Sormani (who is the developer) explain in his blogs:
How the plugin works!
and WAS Shipping fields conditions
The idea is to add to the condition list the shipping fields, the plugin shows by default this fields: WC Advanced Shipping Fields by Default
The goal is to been able to select the newly created field in the conditions (for example: "districts") so the price would appear in the cart when the user select the correct option, the plugin already has a list of the different districts with their respective prices. However, there is an error in the plugin because this line is not working (check the Github for the WAS Shipping fields conditions inside the first function:
$shipping_fields = WC()->checkout()->checkout_fields['shipping'];
I have been trying to solve this for weeks, hence the original ask in this post.
/**
* WAS all checkout fields
*/
add_filter('was_conditions', 'was_conditions_add_shipping_fields', 10, 1);
function was_conditions_add_shipping_fields($conditions) {
$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');
debugToConsole($shipping_fields);
foreach ($shipping_fields as $key => $values) :
if (isset($values['label'])) :
$conditions['Shipping Fields'][$key] = $values['label'];
endif;
endforeach;
return $conditions;
}
The above results in a NULL with the debugToConsole function.
I'm running Advanced Custom Fields in Wordpress. In ACF, you can group items in Tabs, making it easier to navigate to create a post/page content.
I'm wanting to retrieve the name of the tab, and it's content, programmatically.
Is it possible? I can't find any documentation regarding this.
Not sure if you got an answer for your question,
There is no way through php code to get field/subfield data as there is no relationship between tabs and fields under them.
ACF Tabs are only for admin layout to organise fields(hide/show etc) and is done completely through JS/CSS. If you have ACF PRO check the functions in this folder - plugins/advanced-custom-fields-pro/assets/js/acf-input.js which explains how tabs work in ACF
If you want to get fields which are not in repeater/flexible layouts in an array then, You can get an entire set of fields data, which is explained in this ACF GET FIELDS IN A GROUP
I was faced with this problem today, figured I'd post my solution. Based on my research, there's no way to access the tab field directly except to find it in the field group and iterate backwards until a field of type tab is found.
Class ACFHelper {
/**
* get_field_group_by_name: gets the first matching field group for a given title
*
* #param $title the title of the acf field group
* #return Array the fields of the field group or false if not found
*
*/
public static function get_field_group_by_name($title) {
$field_group_post = get_posts([
'numberposts' => 1,
'post_type' => 'acf-field-group',
's' => $title
]);
return ( !empty($field_group_post[0]) ? acf_get_fields( $field_group_post[0]->ID ):false );
}
/**
* get_tab_by_field_name: gets the tab of a specified field by iterating backwards through the field group
*
* #param $field_group_title the title of the acf field group
* #param $field_name the name of the field for which we want the tab it's under
* #return Array the field group of the tab or false if not found
*
*/
public static function get_tab_by_field_name($field_group_title, $field_name) {
$field_group = Self::get_field_group_by_name($field_group_title);
if($field_group) {
$field_index = array_search($field_name, array_column($field_group, 'name'));
if($field_index) {
for($field_index; $field_index > -1; $field_index--) {
if( $field_group[$field_index]['type'] == 'tab' )
return $field_group[$field_index];
}
}
}
return false;
}
}
So now if I have a field strawberry under the fruits tab, I can find my fruits tab in my foods field group like:
$tab = ACFHelper::get_tab_by_field('foods', 'strawberry');