post_status in Woocommerce default CSV exporter/importer - wordpress

This is what I have tried to export/import the post_status with the default Woocommerce CSV importer/exporter.
I have managed to get the post_status field to show up mapped in the importer/exporter.
It also exports the column to csv. However there is no data.
How could I get the post_status to export and import correctly?
// ADD CUSTOM IMPORT COLUMN TO CSV IMPORTER
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );
function add_column_to_importer ( $options ) {
$options['post_status'] = 'post_status';
return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_column_to_mapping_screen' );
function add_column_to_mapping_screen ( $columns ) {
$columns['post_status'] = 'post_status';
return $columns;
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );
function process_import( $object, $data ) {
if (!empty($data['post_status'])) {
$object->post_status($data['post_status']);
}
return $object;
}
// ADD CUSTOM EXPORT COLUMN TO CSV EXPORTER
add_filter( 'woocommerce_product_export_column_names', 'add_export_column' );
add_filter( 'woocommerce_product_export_product_default_columns', 'add_export_column' );
function add_export_column( $columns ) {
$columns['post_status'] = 'post_status';
return $columns;
}
add_filter( 'woocommerce_product_export_product_column_custom_column', 'add_export_data', 10, 2 );
function add_export_data( $value, $product ) {
$post_status = get_post_status();
return $post_status;
}

You have an issue inside your hooks. Check the last hook, for example. You need to replace ...._custom_column with your actual column which is in your case post_status:
add_filter( 'woocommerce_product_export_product_column_post_status', 'add_export_data', 10, 2 );
function add_export_data( $value, $product ) {
return $product->get_status();
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );
function process_import( $product, $data ) {
if ( ! empty( $data['post_status'] ) ) {
$product->set_status( $data['post_status'] );
}
return $product;
}
This hook is used to add some data to your column and because the hook is incorrect, no data is visible.

Related

WooCommerce, disable shipping based on an acf field

I added a true/false field inside the user profile through acf.
I need to hide a specific shipment if this field is selected or not.
If selected, the shipment must be hidden.
This is my code:
function filter_woocommerce_package_rates( $rates, $package ) {
$user_id = get_current_user_id();
$verified=false;
$acf_field=get_user_meta( $user_id, 'fast_shipping', true );
if($acf_field) $verified=true;
// Loop trough
foreach ( $rates as $rate_id => $rate ) {
if ( $verified ) {
unset( $rates['flat_rate:8'] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
But nothing is hidden.
Can you please try below code:
function filter_woocommerce_package_rates( $rates, $package ) {
$user_id = get_current_user_id();
$verified=false;
$acf_field=get_user_meta( $user_id, 'fast_shipping', true );
if(is_bool($acf_field) == 1) $verified=true;
if ( is_bool($verified) == 1 ) {
unset( $rates['flat_rate:8'] );
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
Please let me know if you find any issues.

replace existing product in cart when adding new but excluding specific products. Those specific product will be added and not gonna replaced

replace existing product in cart when adding new but excluding specific products. Those specific product will be added and not gonna replaced.
My code:
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
function woo_custom_add_to_cart($cart_item_data) {
$product_categories = array('mid-career-package-deal','advanced-career-package-deal');
$excluded_ids = array(1105,1108);
if(! in_array( $product_categories,$excluded_ids ) ) {
WC()->cart->add_to_cart();;
}
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
add_filter( 'woocommerce_add_to_cart_validation',
'remove_cart_item_before_add_to_cart', 20, 3 );
function remove_cart_item_before_add_to_cart( $passed, $product_id,
$quantity )
{
$excluded_ids = array(1105, 1108);
if( ! WC()->cart->is_empty()&& ! in_array( $product_id, $excluded_ids
))
WC()->cart->empty_cart();
return $passed;
}

Any way to Overwrite get_stock_quantity in my functions.php?

I am working in Wordpress Multisite and trying to ensure that all the stock info is fetched from the base site tables. I am trying to overwrite get_stock_quantity() woocomerce function in my theme's functions.php. What i found was
public function get_stock_quantity( $context = 'view' ) {
return $this->get_prop( 'stock_quantity', $context );
}
Now this is neither a filter nor an action. I overwrote one filter that is
add_filter( 'woocommerce_product_get_stock_quantity', 'wcs_custom_get_stock_quantity', 1, 2);
function wcs_custom_get_stock_quantity( $availability, $_product ) {
global $wpdb;
$productQuantity = $wpdb->get_results( 'SELECT * FROM '.$wpdb->base_prefix.'postmeta WHERE post_id = '.$_product->get_id() ." AND meta_key = '_stock'", OBJECT );
return $productQuantity[0]->meta_value;
}
But woocommerce_product_get_stock_quantity only works on the Products List page. The individual product-edit page uses get_stock_quantity.
How can I overwrite it without changing Core files?
Thanks
The correct way to alter get_stock_quantity() method depends on the product. AS you have already notice looking at the source code you see that:
return $this->get_prop( 'stock_quantity', $context );
Now get_prop() method is a part of WC_Data class and has a generic filter hook:
$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
And if you look to get_hook_prefix() method you have this:
return 'woocommerce_' . $this->object_type . '_get_';
For product variations the object_type is: product_variation
For other products the object_type is: product
The $prop argument is stock_quantity
So know you can built the related filter hooks. Try this:
add_filter( 'woocommerce_product_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
function custom_get_stock_quantity( $value, $product ) {
$value = 15; // <== Just for testing
return $value;
}
So for product variations you use woocommerce_product_variation_get_stock_quantity filter hook.
And for the other cases woocommerce_product_get_stock_quantity filter hook
You can update the database, but remember this code is executed one time each time you list your products, and when you update your product
add_filter( 'woocommerce_product_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
function custom_get_stock_quantity( $value, $product ) {
$value = 15; // <== Just for testing
//update dabatabase:
update_post_meta( $product->get_id(), '_stock', $value );
return $value;
}
From what I understand you can use $product->set_stock($value); as well to set the value in database
add_filter( 'woocommerce_product_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
function custom_get_stock_quantity( $value, $product ) {
$value = 15; // <== Just for testing
//update dabatabase:
$product->set_stock($value);
return $value;
}

Reveal custom post ID in wordpress dashboard

I am trying to create a new column for custom post types that reveals the post ID in the Wordpress DB. The custom posts are Recipes from the WP Ultimate Recipe Plugin. The code below works if I change the hook for just posts, but even with the Plugin Developers advice on which hooks of his to use, it won't work...
add_filter( 'manage_recipe_posts_columns', 'revealid_add_id_column', 5 );
add_action( 'manage_recipe_posts_custom_column', 'revealid_id_column_content', 5, 2 );
function revealid_add_id_column( $columns ) {
$columns['revealid_id'] = 'ID';
return $columns;
}
function revealid_id_column_content( $column, $id ) {
if( 'revealid_id' == $column ) {
echo $id;
}
}
Any idea on how to get this to work?
Try this code,
function add_cpt_columns( $columns ) {
$column_meta = array( 'your-column-slug' => 'your column name' );
//column rearrange
$columns = array_slice( $columns, 0, 3, true ) + $column_meta + array_slice( $columns, 3, null, true );
return $columns;
}
function custom_custom_cpt_column( $column, $post_id ) {
switch ( $column ) {
case 'your-column-slug' :
// your process
break;
}
}
add_filter( 'manage_edit-custom-post-type_columns', 'add_cpt_columns' );
add_action( 'manage_custom-post-type_posts_custom_column' , 'custom_custom_cpt_column', 10, 2 );

How to save meta value from custom form to Woocommere order

I defined those two functions which should save form value to session variable.
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data_vase', 10, 2 );
function add_cart_item_custom_data_vase( $cart_item_meta, $product_id ) {
global $woocommerce;
$cart_item_meta['colour'] = $_POST['colour'][$product_id];
return $cart_item_meta;
}
function get_cart_items_from_session( $item, $values, $key ) {
if ( array_key_exists( 'colour', $values ) )
$item[ 'colour' ] = $values['colour'];
return $item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );
But I don't know how to save that value to Woocommerce order as a meta of item. I tried it the way bellow but my value (colour) didn't show in items and also not in $_SESSION or $_COOKIE.
add_action('woocommerce_thankyou', 'add_my_meta', 1, 1);
function add_my_meta($order_id) {
$order = new WC_Order($order_id);
$items = $order->get_items(); /* this doesn't include colour item */
foreach($items as $idx => $val){
wc_update_order_item_meta( $idx, "colour",$what_variable); // here is my problem, what value use instead of $what_variable?
}
}

Resources