"used for variations" switch programmatically - wordpress

There are 860+ products with different variations. The task is to allow user bulk edit "used for variations" field for some attributes. For example, we have size, gender that is still needed for variations, but also we have material, that is needed for filters only, so deleting this attribute is not a solution.

Totally untested and not particularly ideal if you have a lot of products as it could time out, but theoretically you could loop through all the variable products and update the _product_attributes meta value. I highly suggest you have a data backup and/or do this is a staging site.
function so_41978670_run_once(){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'post_type',
'field' => 'slug',
'terms' => 'variable',
),
),
);
$products = new WP_Query( $args );
if( $products->have_posts() ){
while ( $products->have_posts() ) {
$products->the_post();
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
$attributes = $product->get_attributes();
$meta_values = array();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attributes meta.
$attributes[ $attribute_key ]['is_variation'] = 1;
}
}
}
update_post_meta( $product->get_id(), '_product_attributes', $attributes );
}
wp_reset_postdata();
}
}
Potential update for WooCommerce 3.0 CRUD functions, but still untested:
function so_41978670_run_once() {
$args = array(
'limit' => -1,
'type' => 'variable',
);
$products = wc_get_products( $args );
if( ! empty ( $products ) ) {
foreach ( $products as $product ) {
$attributes = $product->get_attributes();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attribute settings.
$attribute->set_variation( true );
}
}
}
$product->set_attributes( $attributes );
$product->save();
}
}
}
add_action( 'admin_init', 'so_41978670_run_once' );

Related

WC Vendors - Custom Taxonomy Checklist Not Saving on Front-End

I'm using WC Vendors and have output the taxonomy named "location" on the products using wp_term_checklist per their suggestion. I've got it saving to the product, but it's not saving the checkbox selection on the front-end.
This is the code I've added to the product-edit.php template
$args = array(
'descendants_and_self' => 0,
'selected_cats' => false,
'popular_cats' => false,
'walker' => null,
'taxonomy' => 'location',
'checked_ontop' => false
);
wp_terms_checklist( $my_postid, $args );
$post_to_edit = array(
'ID' => $my_postid,
'tax_input' => array( 'location' => array($_POST['tax_input']['location']) )
);
$pid = wp_update_post($post_to_edit);
if ( isset($_POST['tax_input']['location']) && is_array( $_POST['tax_input']['location'] ) ) {
$location = array_map( 'intval', $_POST['tax_input']['location'] );
$location = array_unique( $location );
wp_set_post_terms($pid, $location, 'location');
}
This is their code where they use a multi-select, but we need checkboxes:
https://gist.github.com/digitalchild/128033d2d41f682acd4387b595d4f607
We have a custom terms checklist called wcv_terms_checklist() in our form helper. It didn't support custom taxonomies but I have modified it to support that now. You can use the following code from v1.7.10 and above to get working custom terms checklists on our front end.
// Output the location checklist
function form_location( $object_id ) {
$args = array(
'taxonomy' => 'location',
);
$field = array(
'id' => 'product_loc_list',
'label' => __( 'Location', 'wcvendors-pro' ),
'class' => 'product_cat_checklist',
);
WCVendors_Pro_Form_Helper::wcv_terms_checklist( $object_id, $args, $field );
}
add_action( 'wcv_after_product_details', 'form_location' );
// Save the terms
function save_location( $post_id ){
if ( isset( $_POST[ 'location' ] ) && is_array( $_POST[ 'location' ] ) ) {
$location = array_map( 'intval', $_POST[ 'location' ] );
$location = array_unique( $location );
wp_set_post_terms( $post_id, $location, 'location' );
}
}
add_action( 'wcv_save_product', 'save_location' );
Gist available here - https://gist.github.com/digitalchild/09e15649425845fef0b8d3af75c79dd1

Display product category on the WooCommerce checkout page

I'm trying to add the product category to the below (working) code but I'm not sure how to go about it.
I've tried using product_cat but as I'm not that experienced with php, I'm just guessing how to achieve this.
add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
function display_custom_product_field_data( $cart_data, $cart_item ) {
$meta_keys = array('time','date');
$dictionary = array('time'=>'Time:','date'=>'Date:' );
$product_id = $cart_item['product_id'];
foreach($meta_keys as $key=>$meta_key){
$meta_value = get_post_meta( $product_id, $meta_key, true );
if( !empty( $cart_data ) ) $custom_items = $cart_data;
if( !empty($meta_value) ) {
$custom_items[] = array(
'key' => __( $dictionary[$meta_key] , 'woocommerce'), //or user $meta_key
'value' => $meta_value,
'display' => $meta_value,
);
}
}
return $custom_items;
}
Following code will also display the product categories on the WooCommerce checkout page
function display_custom_product_field_data( $cart_item_data, $cart_item ) {
// Product ID
$product_id = $cart_item['product_id'];
// Get post meta
$time = get_post_meta( $product_id, 'time', true );
// NOT empty
if( ! empty( $time ) ) {
$cart_item_data[] = array(
'key' => __( 'Time', 'woocommerce'),
'value' => $time,
);
}
// Get terms
$term_names = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'names' ) );
if( ! empty( $term_names ) ) {
$cart_item_data[] = array(
'key' => __( 'Categories', 'woocommerce'),
'value' => implode( ", ", $term_names )
);
}
// Get post meta
$date = get_post_meta( $product_id, 'date', true );
// NOT empty
if( ! empty( $date ) ) {
$cart_item_data[] = array(
'key' => __( 'Date', 'woocommerce'),
'value' => $date,
);
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );

How to get the first topic in a lesson in learndash

I want to retrieve the first topic in a lesson in learndash and redirect it through it. But for some reason am not sure how to do it.
I checked the API and doesn't see any appropriate filter/hook for it.
Here is my code:
function redirect_to_first_topic() {
// We only want to do this for Topics. But the below code can be adapted to work for Lessons
global $post;
$post_id = $post->ID;
if ( get_post_type( $post_id ) == 'sfwd-lessons' ) {
$progress = learndash_get_course_progress( null, $post_id );
$link = $progress['next'];
$parent_lesson_id = learndash_get_setting( $post, 'topic' );
$parent_lesson = get_post( $parent_lesson_id );
var_dump($parent_lesson);
}
// Always return $link
// return $link;
}
add_action('wp', 'redirect_to_first_topic');
So basically what am doing here is getting the parent which is the lesson.
A bit late, but mb it will help someone else. You can see how to get 1. lesson of a course and go deeper if needed fe. first topic of first lesson and so on :
add_action( 'template_redirect', 'course_overview_redirect' );
function course_overview_redirect() {
global $post;
if ( ! empty( $post ) && $post->post_type == 'sfwd-courses' && ! empty( $post->ID ) ) {
$lessons = learndash_get_course_lessons_list( $post->ID );
if ( ! empty( $lessons ) ) {
$lesson = array_shift( $lessons );
if ( ! empty( $lesson ) ) {
$url = get_permalink( $lesson[ "post" ]->ID );
if ( ! empty( $url ) ) {
wp_redirect( esc_url( $url ) ); // And redirect if needed
exit;
}
}
}
}
}
You can add a button like this;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sfwd-lessons', // you can change here to find topics : 'sfwd-topics'
'suppress_filters' => true,
'fields' => 'ids',
'orderby' => 'menu_order', // ordering by menu_order will show lesson list, in their order in course.
'order' => 'ASC',
// this meta query is used if you want to search a lesson under a course, or if you search for a topic which is in a course but not under a lesson
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $course_id, // this is id of your course
)
// if your topic is under a lesson than you should add lesson meta query
// your meta query should be changed like below
/*
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'course_id',
'value' => $course_id,
),
array(
'key' => 'lesson_id',
'value' => $lesson_id,
),
),
*/
),
);
$all_lessons = get_posts($args);
$first_lesson = $all_lessons[0]; // taking directly first element of array
$button_html = '' . __( 'Start the course', 'custom-text' ) . '';

Database insert in order-details page of WooCommerce is not working

I am trying to do an insert to a MySQL database in WordPress using $wpdb->insert. It is just a simple log, but it is not working.
I am trying to save some basic items from an order I received after a purchase made in WooCommerce. The page to which I added the code is order-details.php and belongs to WooCommerce.
The code:
<?php
do_action( 'woocommerce_order_details_before_order_table_items', $order );
$fecha = date('Y-m-d H:i:s');
$creditos =0;
foreach ( $order_items as $item_id => $item ) {
$item_quantity = $item->get_quantity();
$product = $item->get_product();
$product_name = $product->get_name();
if( $product_name == "orián (paquete mínimo)"){
$creditos=15;
}
if( $product_name == "Andrómeda (paquete medio)"){
$creditos=30;
}
if( $product_name == "Lira (paquete básico)"){
$creditos=50;
}
if( $product_name == "Cisne (paquete real + 15 créditos)"){
$creditos=65;
}
if( $product_name == "Perseo (Paquete Premium + 30 créditos)"){
$creditos=120;
}
echo $item_quantity." ".$product_name." ".$creditos." ".$fecha." ".get_current_user_id();
$wpdb->insert(
'registrorden',
array(
'idusuario' => get_current_user_id(),
'paquete' => $product_name,
'cantidad' => $item_quantity,
'creditos' => $creditos
),
array(
'%d',
'%s',
'%d',
'%d'
)
);
wc_get_template( 'order/order-details-item.php', array(
'order' => $order,
'item_id' => $item_id,
'item' => $item,
'show_purchase_note' => $show_purchase_note,
'purchase_note' => $product ? $product->get_purchase_note() : '',
'product' => $product,
) );
}
do_action( 'woocommerce_order_details_after_order_table_items', $order );
?>
The code I added:
$wpdb->insert(
'registrorden',
array(
'idusuario' => get_current_user_id(),
'paquete' => $product_name,
'cantidad' => $item_quantity,
'creditos' => $creditos
),
array(
'%d',
'%s',
'%d',
'%d'
)
);
Here is a snapshot of the table, and here is the error.
What can I do to fix this?

Dynamically tax_query terms

To display all the categories name with checkbox from custom taxonomy called ct_category I have the following code:
$terms = get_terms('ct_category');
foreach ( $terms as $term ) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
}
I would like to display content only from checked categories. I tried following that didn't work:
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => $ct_category,
'field' => 'term_id',
'terms' => $_POST['taxonomy_category']
)
)
);
$loop = new WP_Query( $args );
I can guess that the issue is in 'terms' => $_POST['taxonomy_category']. If name attribute taxonomy_category['.$term->term_id.'] can be displayed as array in 'terms' =>, the issue would be fixed. Spent plenty of times searching google but couldn't find any solution.
Here is the full code
<?php
function add_meta_box() {
add_meta_box( 'ct_metabox', 'CT', 'meta_box_content_output', 'cpt', 'normal' );
}
add_action( 'add_meta_boxes', 'add_meta_box' );
function meta_box_content_output ( $post ) {
wp_nonce_field( 'save_meta_box_data', 'meta_box_nonce' );
$taxonomy_category = get_post_meta( $post->ID, 'taxonomy_category', true );
function categories_checkbox(){
$terms = get_terms('ct_category');
foreach ( $terms as $term ) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
}
}
<?
<ul>
<li>
<?php categories_checkbox() ?>
<li>
</ul>
<?php
}
function save_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['meta_box_nonce'], 'save_meta_box_data' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
$taxonomy_category_value = "";
if(isset($_POST["logo_taxonomy_category"])) {
$taxonomy_category_value = sanitize_text_field( $_POST["logo_taxonomy_category"] );
}
update_post_meta($post_id, "logo_taxonomy_category", $taxonomy_category_value);
}
add_action( 'save_post', 'save_meta_box_data' );
?>
<?php
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => $ct_category,
'field' => 'term_id',
'terms' => $taxonomy_category
)
)
);
$loop = new WP_Query( $args );
?>
Print the checkboxes as this
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category[]" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
This will collect all the IDs in an array $_POST['taxonomy_category']
And then write the wp_query as below:
remove $ from taxonomy name and pass the value of terms as below
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => 'ct_category',
'field' => 'term_id',
'terms' => $_POST['taxonomy_category'],
'operator'=> 'IN'
)
)
);
$loop = new WP_Query( $args );

Resources