Woocommerce | Order variations list by attributes order - php

I have created a dynamic ajax add to cart button on the catalog page.
I need the order of the variations to be displayed according to the main attribute orders and not as it's crated on the product page.
This is the Code I'm using:
<?php
global $product;
$variations = $product->get_available_variations();
$variations_ids = wp_list_pluck( $variations, 'variation_id' );
foreach ($variations_ids as $variations_id) {
$variation = wc_get_product($variations_id);
$variationName = implode(" / ", $variation->get_variation_attributes());
$variation_obj = new WC_Product_variation($variations_id);
$stock = $variation_obj->get_stock_quantity();
?>
<?php if( $stock != 0 || ! $variation_obj->get_manage_stock() ){ ?>
<div class="variation variation-<?php echo $variations_id; ?>" data-act="atc" data-variation-id="<?php echo $variations_id; ?>" data-variation="/?add-to-cart=<?php echo $variations_id; ?>"><?php echo $variationName; ?></div>
<?php } else { ?>
<div class="out-of-stock variation"><?php echo $variationName; ?></div>
<?php } ?>
<?php } ?>

Related

How to make ajax adding an item to the cart — woocommerce?

Friends. Help me make ajax adding an item to the woocommerce cart. The code that I use was found on the Internet, it makes it possible to place buttons of variations on the product badge.
enter image description here
The code that outputs the buttons for each variation:
`
function woocommerce_variable_add_to_cart(){
global $product, $post;
$variations = find_valid_variations();
// Check if the special 'price_grid' meta is set, if it is, load the default template:
if ( get_post_meta($post->ID, 'price_grid', true) ) {
// Enqueue variation scripts
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Load the template
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
return;
}
// Cool, lets do our own template!
?>
<?php
foreach ($variations as $key => $value) {
if( !$value['variation_is_visible'] ) continue;
?>
<?php $desc = $value['variation_description'] ?>
<?php if($desc) { ?>
<?php echo $desc ?>
<?php }?>
<?php if( $value['is_in_stock'] ) { ?>
<form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
<?php
if(!empty($value['attributes'])){
foreach ($value['attributes'] as $attr_key => $attr_value) {
?>
<input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
<?php
}
}
?>
<button type="submit" class="single_add_to_cart_button btn btn-primary"><span class="glyphicon glyphicon-tag"></span>add to cart - <?php echo $attr_value?> - <?php echo $value['price_html'];?></button>
<input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />
</form>
<?php } else { ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
<?php } ?>
<?php } ?>
<?php
}
function find_valid_variations() {
global $product;
$variations = $product->get_available_variations();
$attributes = $product->get_attributes();
$new_variants = array();
// Loop through all variations
foreach( $variations as $variation ) {
// Peruse the attributes.
// 1. If both are explicitly set, this is a valid variation
// 2. If one is not set, that means any, and we must 'create' the rest.
$valid = true; // so far
foreach( $attributes as $slug => $args ) {
if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
// Exists
} else {
// Not exists, create
$valid = false; // it contains 'anys'
// loop through all options for the 'ANY' attribute, and add each
foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
$attribute = trim( $attribute );
$new_variant = $variation;
$new_variant['attributes']["attribute_$slug"] = $attribute;
$new_variants[] = $new_variant;
}
}
}
// This contains ALL set attributes, and is itself a 'valid' variation.
if( $valid )
$new_variants[] = $variation;
}
return $new_variants;
}
`
I removed the redirect to the product card using the code, it remains to solve the issue with ajax
add_filter( 'woocommerce_add_to_cart_redirect', 'wp_get_referer' );

Get WooCommerce product variation attribute terms in admin products general box

I display the id and sku of each variable product in the general tab - of the wp admin product data box with the following code. Any idea how to get the the variable product attribute name too - in this case "test 1" and "test 2"?
The variations
The general tab
The code
<?php
add_action( 'woocommerce_product_options_general_product_data', 'echo_product_id_sku_general_tab' );
function echo_product_id_sku_general_tab() {
$children_ids = wc_get_product()->get_children();
$count = 0;
// Loop through the variations Ids
foreach( $children_ids as $child_id ) {
$count++;
$pr_id_variable = wc_get_product($child_id)->get_id();
$pr_sku_variable = wc_get_product($child_id)->get_sku();
?>
<p class="form-field">
<label><?php _e( 'Variation', 'woocommerce' ); ?> <?php echo $count; ?> ID</label>
<input type="text" value="ID<?php echo $pr_id_variable; ?>"></input>
</p>
<p class="form-field">
<label><?php _e( 'Variation', 'woocommerce' ); ?> <?php echo $count; ?> SKU</label>
<textarea><?php echo $pr_sku_variable; ?></textarea>
</p>
<?php } ?>
<?php } ?>
For variation attribute names (variation Id and sku), you can use the following:
add_action( 'woocommerce_product_options_general_product_data', 'echo_product_id_sku_general_tab' );
function echo_product_id_sku_general_tab() {
global $product_object;
if( $product_object->is_type('variable') ) {
$count = 1;
foreach( $product_object->get_children() as $variation_id ) {
$variation = wc_get_product($variation_id);
$name = array();
foreach( $variation->get_attributes() as $taxonomy => $term_slug ) {
$name[] = get_term_by( 'slug', $term_slug, $taxonomy )->name;
}
echo '<p><strong>'. sprintf( __("Variation %s Name: %s", "woocommerce"), $count, '</strong>' . implode(' - ', $name) ) .'</p>
<p class="form-field">
<label>' . sprintf( __("Variation %s ID", "woocommerce"), $count) . '</label>
<input type="text" value="ID' . $variation_id . '"></input>
</p>
<p class="form-field">
<label>' . sprintf( __("Variation %s SKU", "woocommerce"), $count) . '</label>
<textarea>' . $variation->get_sku() . '</textarea>
</p>';
$count++;
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: this works for product attribute taxonomies only (starting with "pa_"), but not with custom attributes.
I display the id and sku of each variable product in the general tab.
In WooCommerce, the general tab is not available (visible) for a variable product, except when taxes enabled. Under WooCommerce > Settings > General > Enable taxes.
Because it's used to set parent tax class and tax status so if you don’t have taxes enabled, nothing will show there.
To answer your question
Replace this part
// Loop through the variations Ids
foreach( $children_ids as $child_id ) {
$count++;
$pr_id_variable = wc_get_product($child_id)->get_id();
$pr_sku_variable = wc_get_product($child_id)->get_sku();
?>
<p class="form-field">
With
// Loop through the variations Ids
foreach( $children_ids as $child_id ) {
$count++;
$child = wc_get_product( $child_id );
$pr_attribute_name = implode( " / ", $child->get_variation_attributes() );
$pr_id_variable = $child->get_id();
$pr_sku_variable = $child->get_sku();
?>
<p> <?php echo $pr_attribute_name; ?> </p>
<p class="form-field">

How to output ACF PRO value to WordPress shortcode

I would like to create a repeater field that has a WooCommerce shortcode in it, and I would like to change the parameters in that shortcode via ACF PRO to be more customizable and for some reason it does not outputting it to the shortcode, but the rest of the repeater works just fine.
Here is my code:
<?php if( have_rows('product_category') ): ?>
<div class="kategorie">
<?php while( have_rows('product_category') ): the_row();
// Variables
$categoryname = get_sub_field('category_name');
$categoryslug = get_sub_field('category_slug');
$orderby = get_sub_field('order_by');
$onsale = get_sub_field('on_sale');
$columns = get_sub_field('columns');
$rows = get_sub_field('rows');
?>
<span class="category-header"><?php echo $categoryname; ?></span>
<?php echo do_shortcode("[product_category category=\"<?php echo $categoryslug; ?>\" per_page=\"<?php echo $columns; ?>\" columns=\"<?php echo $rows; ?>\" orderby=\"<?php echo $orderby; ?>\" order=\"desc\" operator=\"AND\" on_sale=\"<?php echo $onsale; ?>\"]"); ?>
<?php endwhile; ?>
</div>
<?php endif; ?>

upload categorie to woocommerce

hello I am new to programing. I am creating a simple RSS feed plugin for wordpress to upload some products to WooCommerce and need to upload the category to wordpress . In plugin the category is visible but they are not showing like the url link. I need the category to show like checkbox. Can anybody have any idea ?
File number 1
* Init feed with information from DB
private function load()
{
if ($this->id) {
$post = get_post( $this->id );
if (!$post) {
$this->id = null;
return;
}
$this->title = $post->post_title;
$this->id = $post->ID;
$meta = get_post_meta($post->ID);
foreach ($meta as $key=>$item) {
$newKey = substr($key, 1);
$this->properties[$newKey] = $item[0];
if ($newKey == 'post_category') {
$this->properties[$newKey] = unserialize($item[0]);
}
}
}
}
..................
$fields = array( 'post_category');
..................
// Create post
$post = array('post_category' => $this->post_category);
And the file number 2 have this
<div class="postbox">
<div class="handlediv" title="<?php esc_html_e('Click to toggle', 'rss-autopilot'); ?>"><br></div>
<h3 class="hndle ui-sortable-handle"><span><?php esc_html_e('Categories', 'rss-autopilot'); ?></span></h3>
<div class="inside">
<ul class="rssap-categories-list">
<?php wp_category_checklist( 0, 0, $feed->post_category, false, null, true ); ?>
</ul>
<div class="clear"></div>
</div>
</div>
Here is your code.
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<ul class="product-cats">';
foreach ( $terms as $term ) {
echo '<li class="category">';
echo '<h2>';
echo '<input name="product_category[]" type="checkbox" value="'. $term->term_id.'"';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}

Problems creat plugin woocommerce

I'm trying to make a plugin that will display a list of defined quantity on the article page. I have 2 problems:
1 / I can not seem to include the php file when a product is variable or simple.
2 / I would like to see the dropdown also in the cart page when the product is predefined quantity of
The purpose of the plugin is limited number of mini and maxi product
Here is the code I tried
<?php
/*
Plugin Name: Woo Best Drop Down
Plugin URI: http://www.fr
Description: Drop Down
Version: 1.0
Author: xxx
Author URI: http://xxx.fr
*/
function tab_woo_drop_dwon() {
?>
<li class="woo_best_drop_down_tab"><?php _e('Woo Best Drop Down', 'woo-best-drop-down'); ?></li>
<?php }
add_action('woocommerce_product_write_panel_tabs', 'tab_woo_drop_dwon');
function woo_tab_best_drop_down() {
global $post;
$woo_tab_best_drop_down = array(
'hop' => get_post_meta($post->ID, 'woo_best_drop_down_text', true),
'enabled' => get_post_meta($post->ID, 'woo_best_drop_down_enabled', true),
);
?>
<div id="woo_best_drop_down_tab" class="panel woocommerce_options_panel">
<div class="options_group">
<p class="form-field">
<?php woocommerce_wp_checkbox( array( 'id' => 'woo_best_drop_down_enabled', 'label' => __('Activer le seuil du stock faible?', 'woo-best-drop-down'), 'description' => __('Cochez la case pour activer le stock faible personalisé pour ce produit', 'woo-best-drop-down') ) ); ?>
</p>
</div>
<div class="options_group woo_tab_best_drop_down">
<p class="form-field">
<label><?php _e('Seuil du stock faible pour cet article', 'woo-icon-stock'); ?></label>
<input type="textarea" name="woo_best_drop_down_text" value="<?php echo #$woo_tab_best_drop_down['hop']; ?>" placeholder="<?php _e('Saisissez le seuil de stock', 'woo-best-drop-down'); ?>" />
</p>
</div>
</div>
<?php }
add_action('woocommerce_product_write_panels', 'woo_tab_best_drop_down');
function woo_best_drop_down_custom_tab( $post_id ) {
update_post_meta( $post_id, 'woo_best_drop_down_enabled', ( isset($_POST['woo_best_drop_down_enabled']) && $_POST['woo_best_drop_down_enabled'] ) ? 'yes' : 'no' );
update_post_meta( $post_id, 'woo_best_drop_down_text', $_POST['woo_best_drop_down_text']); }
add_action('woocommerce_process_product_meta', 'woo_best_drop_down_custom_tab');
if ( $product->product_type == 'simple' ){include 'simple_drop_down.php';}
elseif ( $product->product_type == 'variable' ) {include 'variable_drop_down.php';}
?>
Simple drop dwon.php
<?php
add_action( 'woocommerce_after_add_to_cart_button' , 'add_woo_best_drop_dwon');
function add_woo_best_drop_dwon(){
global $post,$product,$woocommerce;
$truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
$values = explode(',',$truc);
$validate = get_post_meta($post->ID, 'woo_best_drop_down_enabled', true);
if (( $validate == 'yes' ) ){
echo "<style>.quantity.buttons_added {
display: none !important;}</style><div class='quantity_select'><select name='quantity'>";
foreach($values as $v){
echo "<option value='$v'>$v</option>";}
echo "</select></div>";
}}
?>
Variable drop dwon.php
<?php
add_action( 'woocommerce_after_single_variation' , 'add_woo_best_drop_dwon');
function add_woo_best_drop_dwon(){
global $post,$product,$woocommerce;
$truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
$values = explode(',',$truc);
$validate = get_post_meta($post->ID, 'woo_best_drop_down_enabled', true);
if (( $validate == 'yes' ) ){
echo "<style>.quantity.buttons_added {
display: none !important;}</style><div class='quantity_select'><select name='quantity'>";
foreach($values as $v){
echo "<option value='$v'>$v</option>";}
echo "</select></div>";
}}
?>
Thanks
I found a solution. i have creat one file for the variable and simple product. I have delete the file drop_down.php and simple_drop_down.php and i've creat drop_down.php and write this :
<?php
add_action( 'woocommerce_after_add_to_cart_button' , 'add_woo_best_drop_dwon');
function add_woo_best_drop_dwon(){
global $post,$product,$woocommerce;
$truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
$values = explode(',',$truc);
$validate = get_post_meta($post->ID, 'woo_best_drop_down_enabled', true);
if (( $validate == 'yes' ) ){
echo "<style>.quantity.buttons_added {
display: none !important;}</style><div class='quantity_select'><select name='quantity'>";
foreach($values as $v){
echo "<option value='$v'>$v</option>";}
echo "</select></div>";
}}
apply_filters( 'woocommerce_quantity_input' , 'add_woo_best_drop_down_variable');
function add_woo_best_drop_down_variable(){
global $post,$product,$woocommerce;
$truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
$values = explode(',',$truc);
$validate = get_post_meta($post->ID, 'woo_best_drop_down_enabled', true);
if (( $validate == 'yes' ) ){
echo "<style>.quantity.buttons_added {
display: none !important;}</style><div class='quantity_select'><select name='quantity'>";
foreach($values as $v){
echo "<option value='$v'>$v</option>";}
echo "</select></div>";
}}
?>
Now my probleme are in the cart.
If you go in the page cart the quantity is not drop down for the product selectioned.
Have an idea for add the drop down quantity in the page cart?
Thanks

Categories