Woocommerce Variations Drop-down Empty - wordpress

I have an unique project using woocommerce. I created a form with Formidable Forms to add variable products from the front-end. It's working well with the code below. I have several attributes that are created. If the attributes are Not blank, they show up correctly as a variation drop-down option.
My problem is when the attribute - for example "Color" is blank and not submitted. The variation "Color" attribute is still created and the value is blank and the drop-down is empty. I've been searching for days to solve this issue. How to not create a variation/attribute if it's not submitted.
I also tried to programically change the set_variation to '' if a custom field value is empty. But that didn't work.
The code was found here:
Auto add all product attributes when adding a new product in Woocommerce
add_action( 'save_post', 'create_product_attributes_variations', 80, 3 );
function create_product_attributes_variations( $post_id, $post, $update ) {
## --- Checking --- ##
if ( $post->post_type != 'product') return; // Only products
// Exit if it's an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Exit if it's an update
if( $update )
return $post_id;
// Exit if user is not allowed
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
## -- Set product as variable and set quote option --- ##
update_post_meta( $post_id, 'qwc_enable_quotes', 'on');
wp_set_object_terms( $post_id, 'variable', 'product_type', false );
## --- The Settings for your product attributes --- ##
$visible = ''; // can be: '' or '1'
$variation = '1'; // can be: '' or '1'
## --- The code --- ##
// Get all existing product attributes
global $wpdb;
$attributes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies" );
$position = 0; // Auto incremented position value starting at '0'
$data = array(); // initialising (empty array)
// Loop through each exiting product attribute
foreach( $attributes as $attribute ){
// Get the correct taxonomy for product attributes
$taxonomy = 'pa_'.$attribute->attribute_name;
$attribute_id = $attribute->attribute_id;
$term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids'));
$product_attribute = new WC_Product_Attribute();
// Set the related data in the WC_Product_Attribute object
$product_attribute->set_id( $attribute_id );
$product_attribute->set_name( $taxonomy );
$product_attribute->set_options( $term_ids );
$product_attribute->set_position( $position );
$product_attribute->set_visible( $visible );
$product_attribute->set_variation( $variation );
// Add the product WC_Product_Attribute object in the data array
$data[$taxonomy] = $product_attribute;
$position++; // Incrementing position
}
// Get an instance of the WC_Product object
$product = wc_get_product( $post_id );
// Set the array of WC_Product_Attribute objects in the product
$product->set_attributes( $data );
//Save main product to get its id
$id = $product->save();
///////
$variation = new WC_Product_Variation();
$variation->set_regular_price(5);
$variation->set_parent_id($id);
//Set attributes requires a key/value containing
// tax and term slug
$variation->set_attributes(array(
));
//Save variation, returns variation id
$variation->save();
}

Related

Extending the feed for WooCommerce for PureClarity [duplicate]

We want to show a section called "complete the look" in WooCommerce. We are using PureClarity to do that.
PureClarity asked us to extend the WooCommerce feed by adding a code snippet in functions.php to add related peoducts SKUs under RelatedProducts.
We used the following code:
function product_callbackk($data, $product) {
$data['RelatedProducts'] = $product->get_sku() ?? null;
return $data;
}
add_filter( 'pureclarity_feed_get_product_data', 'product_callbackk', 10, 2 );
But it showing only the current product SKU and not the related products SKUs. Any advice?
I don't use PureClarity so I added an example based on a default hook in WooCommerce.
In this I use wc_get_related_products() which allow to obtain related products based on the current product ID.
The product SKU per product can then be obtained via a foreach loop.
So you get:
function action_woocommerce_single_product_summary() {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Initialize
$skus = array();
// Get product ID
$product_id = $product->get_id();
// Get related products based on product category and tags || second argument = limit of results, default = 5
$related_products = wc_get_related_products( $product_id, 10 );
// Loop through
foreach ( $related_products as $related_product ) {
// Get product
$product = wc_get_product( $related_product );
// Get product SKU
$product_sku = $product->get_sku();
// NOT empty
if ( ! empty( $product_sku ) ) {
// Push to array
$skus[] = $product_sku;
}
}
// Output
echo '<pre>', print_r( $skus, 1 ), '</pre>';
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 10 );
Or in your specific case:
/**
* #param mixed[] $data - the array of data that will be sent to PureClarity
* #param WC_Product $product - the WooCommerce product object
* #return mixed
*/
function filter_pureclarity_feed_get_product_data( $data, $product ) {
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Initialize
$skus = array();
// Get product ID
$product_id = $product->get_id();
// Get related products based on product category and tags || second argument = limit of results, default = 5
$related_products = wc_get_related_products( $product_id, 10 );
// Loop through
foreach ( $related_products as $related_product ) {
// Get product
$product = wc_get_product( $related_product );
// Get product SKU
$product_sku = $product->get_sku();
// NOT empty
if ( ! empty( $product_sku ) ) {
// Push to array
$skus[] = $product_sku;
}
}
$data['RelatedProducts'] = $skus;
}
return $data;
}
add_filter( 'pureclarity_feed_get_product_data', 'filter_pureclarity_feed_get_product_data', 10, 2 );

Get related products SKUs in WooCommerce

We want to show a section called "complete the look" in WooCommerce. We are using PureClarity to do that.
PureClarity asked us to extend the WooCommerce feed by adding a code snippet in functions.php to add related peoducts SKUs under RelatedProducts.
We used the following code:
function product_callbackk($data, $product) {
$data['RelatedProducts'] = $product->get_sku() ?? null;
return $data;
}
add_filter( 'pureclarity_feed_get_product_data', 'product_callbackk', 10, 2 );
But it showing only the current product SKU and not the related products SKUs. Any advice?
I don't use PureClarity so I added an example based on a default hook in WooCommerce.
In this I use wc_get_related_products() which allow to obtain related products based on the current product ID.
The product SKU per product can then be obtained via a foreach loop.
So you get:
function action_woocommerce_single_product_summary() {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Initialize
$skus = array();
// Get product ID
$product_id = $product->get_id();
// Get related products based on product category and tags || second argument = limit of results, default = 5
$related_products = wc_get_related_products( $product_id, 10 );
// Loop through
foreach ( $related_products as $related_product ) {
// Get product
$product = wc_get_product( $related_product );
// Get product SKU
$product_sku = $product->get_sku();
// NOT empty
if ( ! empty( $product_sku ) ) {
// Push to array
$skus[] = $product_sku;
}
}
// Output
echo '<pre>', print_r( $skus, 1 ), '</pre>';
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 10 );
Or in your specific case:
/**
* #param mixed[] $data - the array of data that will be sent to PureClarity
* #param WC_Product $product - the WooCommerce product object
* #return mixed
*/
function filter_pureclarity_feed_get_product_data( $data, $product ) {
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Initialize
$skus = array();
// Get product ID
$product_id = $product->get_id();
// Get related products based on product category and tags || second argument = limit of results, default = 5
$related_products = wc_get_related_products( $product_id, 10 );
// Loop through
foreach ( $related_products as $related_product ) {
// Get product
$product = wc_get_product( $related_product );
// Get product SKU
$product_sku = $product->get_sku();
// NOT empty
if ( ! empty( $product_sku ) ) {
// Push to array
$skus[] = $product_sku;
}
}
$data['RelatedProducts'] = $skus;
}
return $data;
}
add_filter( 'pureclarity_feed_get_product_data', 'filter_pureclarity_feed_get_product_data', 10, 2 );

Woocommerce product with custom price based on custom fields [duplicate]

In Woocommerce, I used jQuery to calculate a custom price on a single product pages, and now need to pass this value to the cart.
The desired behavior is to pass the new price retrieved from the hidden field to the cart item price.
Here is my actual code:
// Hidden input field in single product page
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11, 0 );
function custom_hidden_product_field() {
echo '<input type="hidden" id="hidden_field" name="custom_price" class="custom_price" value="">';
}
// The code to pass this data to the cart:
add_action( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( ! empty( $_REQUEST['custom_price'] ) ) {
// Set the custom data in the cart item
$cart_item_data['custom_data']['custom_price'] = $_REQUEST['custom_price'];
$data = array( 'custom_price' => $_REQUEST['custom_price'] );
// below statement make sure every add to cart action as unique line item
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $data );
}
return $cart_item_data;
}
And check both $data and $cart_item_data to see that they both return the custom_price data that is calculated on the page.
However, I go to view cart, and the value of the line item is still 0.
I set a var equal to the WC()->session->set( 'custom_data', $data ); and then var_dump to check it, but this returns NULL which might just be what it returns, I'm not entirely sure because I've never used it.
I should also add that I have the regular_price in the product backend set to 0. When I erase this (and leave it blank) I get back the error:
Warning: A non-numeric value encountered in
C:\xampp\htdocs\my-transfer-source\wp-content\plugins\woocommerce\includes\class-wc-discounts.php on line 85
I'm wondering if I've missed something here, and if someone could lend some light onto this? Thanks
Update 2021 - Handling custom price item in mini cart
First for testing purpose we add a price in the hidden input field as you don't give the code that calculate the price:
// Add a hidden input field (With a value of 20 for testing purpose)
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11 );
function custom_hidden_product_field() {
echo '<input type="hidden" id="hidden_field" name="custom_price" class="custom_price" value="20">'; // Price is 20 for testing
}
Then you will use the following to change the cart item price (WC_Session is not needed):
// Save custom calculated price as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( isset( $_POST['custom_price'] ) && ! empty( $_POST['custom_price'] ) ) {
// Set the custom data in the cart item
$cart_item_data['custom_price'] = (float) sanitize_text_field( $_POST['custom_price'] );
// Make each item as a unique separated cart item
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
// For mini cart
add_action( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 2 );
function filter_cart_item_price( $price, $cart_item ) {
if ( isset($cart_item['custom_price']) ) {
$args = array( 'price' => floatval( $cart_item['custom_price'] ) );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price;
}
// Updating cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price', 30, 1 );
function change_cart_item_price( $cart ) {
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Set the new price
if( isset($cart_item['custom_price']) ){
$cart_item['data']->set_price($cart_item['custom_price']);
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Auto update product custom field based on WooCommerce product stock

I have an ACF field "DeliverySpeed" attached to Products with the values 'Fast' and 'Slow'
I would like to update this field to change to value 'Slow' every time the product stock is zero or less (product on backorder)
I am still learning PHP so far this is what I got to but I am sure different things are missing, I just don't know in which direction to go:
based on acf update field documentation
function automatically_change_delivery( ) {
global $product;
$fieldkey = "DeliverySpeed";
$valueslow = "Slow";
if($product->get_stock_quantity()<0) { update_field( $field_key, $valueslow, $post_id );
} }
Thank you in advance for the attention and advice.
The following code will update automatically your product custom field once order has reduced product stock levels. So when product has stock the custom field value will be 'Fast' otherwise 'Slow'.
The code:
add_action( 'woocommerce_payment_complete', 'update_product_custom_field_after_reduced_stock_levels', 20, 2 );
add_action( 'woocommerce_order_status_completed', 'update_product_custom_field_after_reduced_stock_levels', 20, 2 );
add_action( 'woocommerce_order_status_processing', 'update_product_custom_field_after_reduced_stock_levels', 20, 2 );
add_action( 'woocommerce_order_status_on-hold', 'update_product_custom_field_after_reduced_stock_levels', 20, 2 );
function update_product_custom_field_( $order_id, $order = '' ) {
// Continue only when order has reduced product stock levels
if ( wc_string_to_bool( get_post_meta( $order_id, '_order_stock_reduced', true ) ) )
return $order_id; // Exit
if( ! $order || ! is_a( $order, 'WC_Order') ) {
$order = wc_get_order( $order_id ); // Get the WC_Order object if it's empty
}
$field_key = 'DeliverySpeed';
// Loop through order items
foreach ( $order->get_items() as $item ) {
$product = $cart_item['data'];
$product_id = $product->get_id();
$stock_qty = $product->get_stock_quantity();
$field_value = get_field( $field_key, $product_id ); // Get ACF field value
if ( $stock_qty <= 0 && $field_value === 'Fast' ) {
update_field( $field_key, 'Slow', $product_id );
}
elseif ( $stock_qty > 0 && $field_value === 'Slow' ) {
update_field( $field_key, 'Fast', $product_id );
}
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Disable users from adding to cart if a specific attribute is selected

In Woocommerce, I want to prevent users from adding to cart if a specific attribute is selected.
The attribute name is "Production Time" and the specific value we want to disable from purchasing is "Urgent".
The code below is from this link - Hide Add to Cart button in Woocommerce product variations for a specific attribute value
However, it only works with numbers. When the attribute is a text, it doesn't work.
add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
function conditional_variation_is_purchasable( $purchasable, $product ) {
## ---- Your settings ---- ##
$taxonomy = 'production-time';
$term_name = 'urgent';
## ---- The active code ---- ##
$found = false;
// Loop through all product attributes in the variation
foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
$attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
$term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
// Searching for attribute 'pa_size' with value 'XL'
if($attribute_taxonomy == $taxonomy && $term->name == $term_name ){
$found = true;
break;
}
}
if( $found )
$purchasable = false;
return $purchasable;
}

Resources