I want to create a search form in a new search page in WordPress with Custom Fields (ACF) and WPML, and I'm trying it with the code above! The problem is that the search results is great when I use the main language (Greek) but I have no results when I use the secondary language of the site (English).
<?php
/* Template name: Search */
get_header();
$current_lang = apply_filters( 'wpml_current_language', NULL ); ?>
<div class="container inner-content">
<form class="search" method="post" action="<?php echo get_permalink( get_field('amorgos_search_results_page','option')[0] ) ?>">
<?php
if ( $current_lang === 'en') {
if ($_GET['type']=='advanced') {
$title = 'Advanced Search';
}
elseif ($_GET['type']=='simple') {
$title = 'Simple Search';
}
elseif ($_GET['type']=='geographic') {
$title = 'Geographic Search';
}
else{
$title = 'Topic Search';
}
}
else
{
if ($_GET['type']=='advanced') {
$title = 'Σύνθετη Αναζήτηση';
}
elseif ($_GET['type']=='simple') {
$title = 'Απλή Αναζτηση';
}
elseif ($_GET['type']=='geographic') {
$title = 'Γεωγραφική Αναζήτηση';
}
else{
$title = 'Τοπική Αναζήτηση';
}
}
?>
<h1 class="title"><?php echo $title; ?></h1>
<div class="search__fields">
<?php
if ( $current_lang === 'en') {
if( isset($_GET['type']) && $_GET['type'] === 'simple')
{
amorgos_search_field('Place of origin', 'Select Island', 'field_5d51cc71c4d71', 'origin');
amorgos_search_field('Location', 'Select Location', 'field_5d516b8b52c6a', 'location');
amorgos_search_field('Designation', 'Type Name / Title of Song or Instrumental Tune', null, 'post_title');
amorgos_search_field('Instrumentalists', 'Names', 'field_5d543cf799a50', 'relation_musicians' );
amorgos_search_field('Information about the Instrumental Tune', 'field_5d4f401050075', 'Type of Instrumental Tune');
}
elseif( isset($_GET['type']) && $_GET['type'] === 'geographic')
{
amorgos_search_field('Place of origin','Select Island', 'field_5d51cc71c4d71', 'origin');
amorgos_search_field('Location' , 'Select Location', 'field_5d516b8b52c6a', 'location');
}
elseif( isset($_GET['type']) && $_GET['type'] === 'topic')
{
amorgos_search_field('Classifications', 'field_5d4f398135758', 'By content');
amorgos_search_field(null, 'By type', 'field_5d4f38d376a15');
amorgos_search_field(null, 'During Execution', 'field_5d4f392e97a73');
}
else
{
amorgos_search_field('Place of origin', 'Select Island', 'field_5d51cc71c4d71', 'origin');
amorgos_search_field('Location', 'Select Location', 'field_5d516b8b52c6a', 'location');
amorgos_search_field('Name / Title', 'Type Name / Title of Song or Instrumental Tune', null, 'post_title');
amorgos_search_field('Instrumentalists', 'Names', 'field_5d543cf799a50', 'relation_musicians' );
amorgos_search_field('Classification by Occasion', 'Life Cycle', 'field_5d4f35a13c634');
amorgos_search_field(null, 'Seasons Cycle', 'field_5d4f375476b4b');
amorgos_search_field(null, 'Agricultural Cycle', 'field_5d4f37a151267');
amorgos_search_field(null, 'Other Occasions', 'field_5d4f380651268');
amorgos_search_field('Other Classifications', 'By content', 'field_5d4f398135758');
amorgos_search_field(null, 'By Kind', 'field_5d4f38d376a15');
amorgos_search_field(null, 'By Performance Practice', 'field_5d4f392e97a73');
amorgos_search_field('Information about the Dance', 'Type of dance', 'field_5d51d28b3beec', 'dance_kind');
amorgos_search_field(null, 'Genus of Dance', 'field_5d4f3da15b73f');
amorgos_search_field(null, 'Dancers’ Positioning', 'field_5d4f3e068927f');
amorgos_search_field(null, 'Classification by Dancer', 'field_5d4f3e626a68c');
amorgos_search_field('Information about the Song', 'Metric analysis', 'field_5d51cdf695665', 'metre_analysis');
amorgos_search_field('Information about the Instrumental Tune', 'Kind of Instrumental Tune', 'field_5d4f401050075');
amorgos_search_field(null, 'Meter', 'field_5d4f40bd9d858');
amorgos_search_field('Melodic Analysis', 'Genus', 'field_5d4f416a4ed88');
amorgos_search_field(null, 'Mode', 'field_5d4f41d94ed89');
amorgos_search_field(null, 'Number of Notes in the Scale', 'field_5d4f4249e01e2');
}
# code...
}
else
{
if( isset($_GET['type']) && $_GET['type'] === 'simple'){
amorgos_search_field('Τόπος Προέλευσης', 'Επιλέξτε Νησί', 'field_5d51cc71c4d71', 'origin');
amorgos_search_field('Τοποθεσία', 'Επιλέξτε Τοποθεσία', 'field_5d516b8b52c6a', 'location');
amorgos_search_field('Ονομασία / Τίτλος', 'Πληκρολογήστε Ονομασία / Τίτλο Τραγουδιού ή Οργανικού Σκοπού', null, 'post_title');
amorgos_search_field('Οργανοπαίκτες', 'Ονόματα', 'field_5d543cf799a50', 'relation_musicians' );
amorgos_search_field('Στοιχεία σχετικά με τον Οργανικό Σκοπό', 'Είδος Οργανικού Σκοπού', 'field_5d4f401050075');
}
elseif( isset($_GET['type']) && $_GET['type'] === 'geographic'){
amorgos_search_field('Τόπος Προέλευσης','Επιλέξτε Νησί', 'field_5d51cc71c4d71', 'origin');
amorgos_search_field('Τοποθεσία' , 'Επιλέξτε Τοποθεσία', 'field_5d516b8b52c6a', 'location');
}
elseif( isset($_GET['type']) && $_GET['type'] === 'topic'){
amorgos_search_field('Ταξινομήσεις', 'Κατά το Περιεχόμενο', 'field_5d4f398135758');
amorgos_search_field(null, 'Κατά το Είδος', 'field_5d4f38d376a15');
amorgos_search_field(null, 'Κατά την Εκτέλεση', 'field_5d4f392e97a73');
}
else
{
amorgos_search_field('Τόπος Προέλευσης', 'Επιλέξτε Νησί', 'field_5d51cc71c4d71', 'origin');
amorgos_search_field('Τοποθεσία', 'Επιλέξτε Τοποθεσία', 'field_5d516b8b52c6a', 'location');
amorgos_search_field('Ονομασία', 'Πληκρολογήστε Ονομασία / Τίτλο Τραγουδιού ή Οργανικού Σκοπού', null, 'post_title');
amorgos_search_field('Οργανοπαίκτες', 'Ονόματα', 'field_5d543cf799a50', 'relation_musicians' );
amorgos_search_field('Ταξινόµηση κατά την Περίσταση', 'Κύκλος Ζωής', 'field_5d4f35a13c634' );
amorgos_search_field(null, 'Κύκλος Χρόνου', 'field_5d4f375476b4b');
amorgos_search_field(null, 'Αγροτικός Κύκλος', 'field_5d4f37a151267');
amorgos_search_field(null, 'Άλλες Περιστάσεις', 'field_5d4f380651268');
amorgos_search_field('Άλλες Ταξινομήσεις', 'Κατά το Περιεχόμενο', 'field_5d4f398135758');
amorgos_search_field(null, 'Κατά το Είδος', 'field_5d4f38d376a15');
amorgos_search_field(null, 'Κατά την Εκτέλεση', 'field_5d4f392e97a73');
amorgos_search_field('Στοιχεία για το Χορό', 'Είδος Χορού', 'field_5d51d28b3beec', 'dance_kind');
amorgos_search_field(null, 'Γένος Χορού', 'field_5d4f3da15b73f');
amorgos_search_field(null, 'Σύνδεση Χορευτών', 'field_5d4f3e068927f');
amorgos_search_field(null, 'Ταξινόμηση κατά το Χορευτή', 'field_5d4f3e626a68c');
amorgos_search_field('Στοιχεία για το Τραγούδι', 'Μετρική Ανάλυση', 'field_5d51cdf695665', 'metre_analysis');
amorgos_search_field('Στοιχεία σχετικά με τον Οργανικό Σκοπό', 'Είδος Οργανικού Σκοπού', 'field_5d4f401050075');
amorgos_search_field(null, 'Μέτρο', 'field_5d4f40bd9d858');
amorgos_search_field('Μελωδική Ανάλυση', 'Γένος', 'field_5d4f416a4ed88');
amorgos_search_field(null, 'Τρόπος', 'field_5d4f41d94ed89');
amorgos_search_field(null, 'Αριθμός Φθόγγων στην Κλίμακα', 'field_5d4f4249e01e2');
}
}
?>
</div>
<button type="submit" class="results__btn results__btn--search has-brackets has-brackets--active-hover has-brackets--no-top"><?php _e('Αναζητηση' ,'wpml_theme'); ?></button>
</form>
</div>
<?php get_footer(); ?>
Could help me to resolve this problem and to have search results not only in the main language but in secondary too?
Related
I am trying to apply complicated shipping rules in Woocommerce.
I have basic products which is:
Spend
Shipping cost
less than 300
30$
300 or more
Free
But I also have some large products and products with special delivery requirements
product
Shipping Class
cost
large box
Truck with crane
1000$
large Box
Truck only
200$
Gazebo (Choice1: Delivery Only)
Delivery & Assebly
200$
Gazebo (Choice2: Delivery & Assembly)
Delivery & Assebly
350$
Below is my shipping methods
Shipping Methods
Standerd rates
Shipping With Assembly
For basic products everything is working well,
Also for large boxes its well as it shows in Standard shipping.
But when I add the (shipping with assembly) shipping method it still appear as choice even if no product with this class is in the cart.
I just need it to appear only when gazebo is in the cart
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_class_is_in_cart', 100, 2 );
function hide_shipping_when_class_is_in_cart( $rates, $package ) {
$free_shipping_method = 'free_shipping:13';
$shipping_classes = array('truck-crane','large-furniture','furniture-assembly');
$class_exists = false;
$cart_classes = array();
$shipping_methods = $shipping_zone->get_shipping_methods();
foreach( $package['contents'] as $cart_item )
$cart_item_class = $cart_item['data']->get_shipping_class();
if( in_array($cart_item_class , $shipping_classes ) ) {//Check if any shipping class exist
array_push($cart_classes,$cart_item_class); //push cart classes to array
$class_exists = true;
//break;
}
foreach($shipping_methods as $method){
$method_classes = $method->get_shipping_classes();
//Here i want to unset shipping methods which has no avaiable class in cart items
}
if( $class_exists )
unset( $rates[$free_shipping_method] );
return $rates;
}
I ended up adding the service like 'assembly' as a choice that comes with the product
my code below
/**
** Display Fields
*/
add_action('woocommerce_product_options_shipping', 'woocommerce_product_custom_product_service');
function woocommerce_product_custom_product_service()
{
global $woocommerce, $post;
echo '<div class="product_custom_field"><span class="wrap">';
woocommerce_wp_text_input(
array(
'id' => '_custom_product_service_label',
'placeholder' => 'Service Label',
'label' => __('Custom Service', 'woocommerce'),
)
);
woocommerce_wp_text_input(
array(
'id' => '_custom_product_service_fees',
'placeholder' => 'Service Fees',
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '</span></div>';
}
/**
** Save Fields
*/
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_product_service_save');
function woocommerce_product_custom_product_service_save($post_id)
{
$woocommerce_custom_product_service_label = $_POST['_custom_product_service_label'];
$woocommerce_custom_product_service_fees = $_POST['_custom_product_service_fees'];
if (!empty($woocommerce_custom_product_service_label) && !empty($woocommerce_custom_product_service_fees)){
update_post_meta($post_id, '_custom_product_service_label', esc_attr($woocommerce_custom_product_service_label));
update_post_meta($post_id, '_custom_product_service_fees', esc_attr($woocommerce_custom_product_service_fees));
}elseif(empty($woocommerce_custom_product_service_label) && empty($woocommerce_custom_product_service_fees)){
update_post_meta($post_id, '_custom_product_service_label', '');
update_post_meta($post_id, '_custom_product_service_fees', '');
}
}
/**
** Product page form
*/
add_action('woocommerce_single_product_summary','add_custom_product_service_to_single_product', 2 );
function add_custom_product_service_to_single_product(){
global $product;
if( !$product->get_meta( '_custom_product_service_label' ) && !$product->get_meta( '_custom_product_service_fees' )) return;
add_action('woocommerce_before_add_to_cart_button', 'add_custom_product_service_option', 30 );
}
function add_custom_product_service_option(){
global $product;
$active_price = (float) $product->get_price();
$service_label = $product->get_meta( '_custom_product_service_label' );
$service_price = (float) $product->get_meta( '_custom_product_service_fees' );
$service_price_html = strip_tags( wc_price( wc_get_price_to_display( $product, array('price' => $service_price ) ) ) );
$active_price_html = wc_price( wc_get_price_to_display( $product ) );
$disp_price_sum_html = wc_price( wc_get_price_to_display( $product, array('price' => $active_price + $service_price ) ) );
echo '<div class="hidden-field">
<p class="form-row form-row-wide" id="service_option_field" data-priority="">
<span class="woocommerce-input-wrapper"><label class="checkbox"> ' .$service_label.' '.__("Service:", "Woocommerce") .
' <input type="checkbox" class="input-checkbox " name="service_option" id="service_option" value="1"> +' . $service_price_html .
'</label></span></p>
<input type="hidden" name="service_price" value="' . $service_price . '">
<input type="hidden" name="active_price" value="' . $active_price . '"></div>';
// Jquery: Update displayed price
?>
<script type="text/javascript">
jQuery(function($) {
var cb = 'input[name="service_option"]'
pp = 'p.price';
// On change / select a variation
$('form.cart').on( 'change', cb, function(){
if( $(cb).prop('checked') === true )
$(pp).html('<?php echo $disp_price_sum_html.__("(Assembly Included)", "Woocommerce"); ?>');
else
$(pp).html('<?php echo $active_price_html; ?>');
})
});
</script>
<?php
}
/**
** Add to Cart
*/
add_filter('woocommerce_add_cart_item_data', 'add_custom_product_service_data', 10, 3);
function add_custom_product_service_data( $cart_item_data, $product_id, $variation_id ) {
if (isset($_POST['service_option']) && !empty($_POST['service_option'])) {
$cart_item_data['new_price'] = (float) ($_POST['active_price'] + $_POST['service_price']);
$cart_item_data['service_price'] = (float) $_POST['service_price'];
$cart_item_data['active_price'] = (float) $_POST['active_price'];
foreach(WC()->cart->get_cart() as $cart_item){
if(($product_id == $cart_item['product_id']) && !empty($cart_item['service_price'])){
$unique_key = $cart_item['unique_key'];
break;
}
}
if(!empty($unique_key)){
$cart_item_data['unique_key'] = $unique_key;
}else{
$cart_item_data['unique_key'] = md5(microtime().rand());
}
return $cart_item_data;
}}
/**
** Set the new calculated cart item price
*/
add_action('woocommerce_before_calculate_totals', 'add_custom_product_service_custom_price', 20, 1);
function add_custom_product_service_custom_price($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach($cart->get_cart() as $cart_item) {
if (isset($cart_item['new_price']))
$cart_item['data']->set_price((float) $cart_item['new_price']);
}
}
/**
** Display in Cart
*/
add_filter('woocommerce_get_item_data', 'display_custom_product_service_item_data', 10, 2);
function display_custom_product_service_item_data($cart_item_data, $cart_item) {
$_product = new WC_Product( $cart_item['product_id'] );
$service_label = $_product->get_meta( '_custom_product_service_label' );
if (isset($cart_item['service_price'])) {
//$service_label = get_post_meta( $cart_item['product_id'], '_custom_product_service_label' );
$cart_item_data[] = array(
'name' => $service_label .__(" Service Fees", "woocommerce"),
'value' => strip_tags( '+ ' . wc_price( wc_get_price_to_display( $cart_item['data'], array('price' => $cart_item['service_price'] ) ) ) )
);
}elseif(!empty($service_label)){
$cart_item_data[] = array(
'name' => $service_label .__(" Service Not Included", "woocommerce"),
'value' => strip_tags( '+ ' . wc_price( wc_get_price_to_display( $cart_item['data'], array('price' => $cart_item['service_price'] ) ) ) )
);
}
return $cart_item_data;
}
With WooCommerce last version, I need to display on product page the weight of the variation chosen in short description and I would like to display the total weight just beside the add to cart button when you choose more than 1 quantity.
For example: I select 4 quantity. Unit weight is 4 kgs. I need to display: "The total weight is 16kgs".
This code works well if the general product has a weight, but it doesn't works with variations weight:
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>', __('Totale Prodotto:','woocommerce'),'<span class="price">' . $product->get_price() . '</span>');
echo sprintf('<div id="product_total_weight" style="margin-bottom:20px;">%s %s</div>', __('Totale Peso:','woocommerce'),'<span class="weight">' . $product->get_weight() . '</span>');
?>
<script>
jQuery(function($) {
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
var weight = <?php echo $product->get_weight(); ?>;
$('[name=quantity]').change(function() {
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency + product_total.toFixed(2) );
var weight_total = parseFloat(weight * this.value);
$('#product_total_weight .weight').html( weight_total.toFixed(2) + ' kg');
}
});
});
</script>
<?php
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
You can create a javascript object that will contain the necessary values based on the type of product.
The idea is this:
Create a PHP array with the product type and other necessary fields (price, currency and weight);
Convert PHP array to Javascript object to use in script
Based on the type of product, it obtains the price and weight of the product (if the product is variable, it obtains the values of the selected variation)
In the previous function I used the variation slugs to determine which option was chosen. This method works only if the variable product has a single select.
Thanks to #LoicTheAztec's answer you find here:
Get the selected product variation ID in WooCommerce variable products single pages
I figured I could get the value of the chosen variation id from the hidden input with the variation_id class.
It will now also work with variable products that have more than one
select options.
I changed the function by splitting it in two.
The first script adds the content to the product page (under the add to cart form).
The second adds the script in the footer to handle changing values.
So:
// shows the total price and total weight based on the quantity entered
add_action( 'woocommerce_single_product_summary', 'show_total_price_and_total_weight_by_quantity', 31 );
function show_total_price_and_total_weight_by_quantity() {
// create the divs that will contain the total price and total weight
echo sprintf('<div id="product_total_price" style="display:none;margin-bottom:20px;">%s %s</div>', __('Totale Prodotto:','woocommerce'),'<span class="price"></span>');
echo sprintf('<div id="product_total_weight" style="display:none;margin-bottom:20px;">%s %s</div>', __('Totale Peso:','woocommerce'),'<span class="weight"></span>');
}
// change the total price and total weight based on the quantity entered
add_action( 'wp_footer', 'change_total_price_and_total_weight_by_quantity', 99 );
function change_total_price_and_total_weight_by_quantity() {
// only on the product page
if ( ! is_product() ) {
return;
}
global $product;
// initializes the array that will contain the product data
$product_data = array();
// do divs need to be shown?
$show = true;
// gets the currency and unit of weight
$currency = get_woocommerce_currency_symbol();
$weight_unit = get_option('woocommerce_weight_unit');
// if the product is simple
if ( $product->is_type( 'simple' ) ) {
// set the type of product
$product_data['type'] = 'simple';
// get simple product data
$product_data['price'] = $product->get_price();
$product_data['currency'] = $currency;
$product_data['weight'] = $product->get_weight();
$product_data['weight_unit'] = $weight_unit;
}
// if the product is variable
if ( $product->is_type( 'variable' ) ) {
// set the type of product
$product_data['type'] = 'variable';
// get the ids of the product variations
$variation_ids = $product->get_children();
foreach ( $variation_ids as $variation_id ) {
// gets the object of the variation product
$variation = wc_get_product( $variation_id );
// gets product variation data
$product_data['variation'][$variation_id]['price'] = $variation->get_price();
$product_data['variation'][$variation_id]['currency'] = $currency;
$product_data['variation'][$variation_id]['weight'] = $variation->get_weight();
$product_data['variation'][$variation_id]['weight_unit'] = $weight_unit;
}
// hides the div
$show = false;
}
// returns the JSON representation of a data
$product_data_json = json_encode( $product_data );
?>
<script>
jQuery(function($) {
// create a javascript object with product data
<?php
echo "var productData = ". $product_data_json . ";";
if ( $show ) {
?>
var product_total = parseFloat( productData.price * $('[name=quantity]').val());
$('#product_total_price .price').html( productData.currency + product_total.toFixed(2) );
var weight_total = parseFloat(productData.weight * $('[name=quantity]').val());
$('#product_total_weight .weight').html( weight_total.toFixed(2) + ' ' + productData.weight_unit);
$('#product_total_price').show();
$('#product_total_weight').show();
<?php
}
?>
// when the quantity is changed or a product option is selected
jQuery('[name=quantity], table.variations select').on('change',function(){
// shows data based on product type
switch( productData.type ) {
case 'simple':
// update the fields
var product_total = parseFloat(productData.price * $(this).val());
$('#product_total_price .price').html( productData.currency + product_total.toFixed(2) );
var weight_total = parseFloat(productData.weight * $(this).val());
$('#product_total_weight .weight').html( weight_total.toFixed(2) + ' ' + productData.weight_unit);
break;
case 'variable':
// gets the id variation based on the options chosen
var variation_id = $('input.variation_id').val();
// if the variation id is valid and the current option is different from "Choose an option"
if ( parseInt( $('input.variation_id').val() ) > 0 && $('input.variation_id').val() != '' && $(this).find('option').filter(':selected').val() != '' ) {
$('#product_total_price').show();
$('#product_total_weight').show();
// gets the object based on the selected variation
var obj = productData.variation[variation_id];
// update the fields
var product_total = parseFloat(obj.price * $(this).val());
$('#product_total_price .price').html( obj.currency + ' ' + product_total.toFixed(2) );
var weight_total = parseFloat(obj.weight * $(this).val());
$('#product_total_weight .weight').html( weight_total.toFixed(2) + ' ' + obj.weight_unit);
// otherwise it hides the divs
} else {
$('#product_total_price').hide();
$('#product_total_weight').hide();
}
break;
}
});
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.
I see a quantity_input.php file in the theme i use, maybe it's here that quantity input is reset.
if ( $max_value && $min_value === $max_value ) {
?>
<div class="quantity hidden">
<input type="hidden" id="<?php echo esc_attr( $input_id ); ?>" class="qty" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $min_value ); ?>" />
</div>
<?php
} else {
/* translators: %s: Quantity. */
$labelledby = ! empty( $args['product_name'] ) ? sprintf( esc_html__( '%s quantity', 'essentials' ), strip_tags( $args['product_name'] ) ) : '';
?>
<div class="quantity mr-2 pix-px-10 bg-white rounded-lg shadow-sm d-inline-block">
<label class="screen-reader-text sr-only" for="<?php echo esc_attr( $input_id ); ?>"><?php esc_html_e( 'Quantity', 'essentials' ); ?></label>
<a href="#" class="minus d-inline-block text-body-default" >-</a>
<input
type="number"
id="<?php echo esc_attr( $input_id ); ?>"
class="input-text qty text form-control shadow-0 d-inline-block"
step="<?php echo esc_attr( $step ); ?>"
min="<?php echo esc_attr( $min_value ); ?>"
max="<?php echo esc_attr( 0 < $max_value ? $max_value : '' ); ?>"
name="<?php echo esc_attr( $input_name ); ?>"
value="<?php echo esc_attr( $input_value ); ?>"
title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'essentials' ); ?>"
size="4"
pattern="<?php echo esc_attr( $pattern ); ?>"
inputmode="<?php echo esc_attr( $inputmode ); ?>"
aria-labelledby="<?php echo esc_attr( $labelledby ); ?>" />
<a href="#" class="plus d-inline-block text-body-default" >+</a>
</div>
<?php
}
And i also have a frontend.js file in plugin folder :
jQuery(
function ( $ ) {
if ( ywmmq.variations ) {
$( document ).on(
'found_variation',
function () {
var product_id = parseInt( $( '.single_variation_wrap .product_id, .single_variation_wrap input[name="product_id"]' ).val() ),
variation_id = parseInt( $( '.single_variation_wrap .variation_id, .single_variation_wrap input[name="variation_id"]' ).val() );
if ( ! isNaN( product_id ) && ! isNaN( variation_id ) ) {
get_variation_rules( product_id, variation_id );
}
}
);
}
function get_variation_rules( product_id, variation_id ) {
var container = $( '.ywmmq-rules-wrapper' ),
variations_form = $( '.single_variation_wrap' ),
raq_button = $( '.add-request-quote-button' );
if ( variations_form.is( '.processing' ) ) {
return false;
}
variations_form.addClass( 'processing' );
raq_button.addClass( 'disabled' );
variations_form.block(
{
message : null,
overlayCSS: {
background: '#fff',
opacity : 0.6
}
}
);
$.ajax(
{
type : 'POST',
url : ywmmq.ajax_url,
data : {
action : 'ywmmq_get_rules',
product_id : product_id,
variation_id: variation_id
},
success : function ( response ) {
if ( response.status === 'success' ) {
container.html( response.rules );
if ( parseInt( response.limits.max ) !== 0 ) {
$( '.single_variation_wrap .quantity input[name="quantity"]' ).attr( 'max', response.limits.max );
} else {
$( '.single_variation_wrap .quantity input[name="quantity"]' ).removeAttr( 'max' );
}
if ( parseInt( response.limits.min ) !== 0 ) {
$( '.single_variation_wrap .quantity input[name="quantity"]' ).attr( 'min', response.limits.min ).val( response.limits.min );
} else {
$( '.single_variation_wrap .quantity input[name="quantity"]' ).attr( 'min', 1 ).val( 1 );
}
if ( parseInt( response.limits.step ) !== 0 ) {
$( '.single_variation_wrap .quantity input[name="quantity"]' ).attr( 'step', response.limits.step );
} else {
$( '.single_variation_wrap .quantity input[name="quantity"]' ).attr( 'step', 1 ).val( 1 );
}
$( document ).trigger( 'ywmmq_additional_operations', [response.limits.min] );
} else {
container.html();
}
variations_form.removeClass( 'processing' ).unblock();
raq_button.removeClass( 'disabled' );
},
dataType: 'json'
}
);
return false;
}
$( document ).on(
'yith_wcpb_found_variation_after',
function ( event, form, variation ) {
if ( form.is( '.processing' ) ) {
return false;
}
form.addClass( 'processing' );
form.block(
{
message : null,
overlayCSS: {
background: '#fff',
opacity : 0.6
}
}
);
$.ajax(
{
type : 'POST',
url : ywmmq.ajax_url,
data : {
action : 'ywmmq_get_rules',
product_id : form.data( 'product_id' ),
variation_id: form.find( '.variation_id' ).val()
},
success : function ( response ) {
if ( response.status === 'success' ) {
if ( parseInt( response.limits.max ) !== 0 ) {
form.find( '.yith-wcpb-bundled-quantity' ).attr( 'max', response.limits.max );
} else {
form.find( '.yith-wcpb-bundled-quantity' ).removeAttr( 'max' );
}
if ( parseInt( response.limits.min ) !== 0 ) {
form.find( '.yith-wcpb-bundled-quantity' ).attr( 'min', response.limits.min ).val( response.limits.min );
} else {
form.find( '.yith-wcpb-bundled-quantity' ).attr( 'min', 1 ).val( 1 );
}
if ( parseInt( response.limits.step ) !== 0 ) {
form.find( '.yith-wcpb-bundled-quantity' ).attr( 'step', response.limits.step );
} else {
form.find( '.yith-wcpb-bundled-quantity' ).attr( 'step', 1 ).val( 1 );
}
}
form.removeClass( 'processing' ).unblock();
},
dataType: 'json'
}
);
}
)
}
);
In WooCommerce, from "Adding a hidden fields to checkout and process it through order" answer thread, which generates a verification number on checkout for orders,
I am trying to figure out how to turn this into a account number instead by which I mean:
when the customer registers, a unique number is generated
that number is saved to the account
the number can be displayed used echo later on
This is what I have tried so far (without success) =>
OPTION 1:
add_action( 'woocommerce_register_form_start', 'user_account_nr' );
function user_account_nr() {
$billing_account_number = '';
if ( isset( $_POST['billing_account_number'] ) ) {
$billing_account_number = $_POST['billing_account_number'];
}
?>
<p class="form-row form-row-first">
<input type="hidden" class="input-hidden" name="billing_account_number" id="reg_billing_account_number" value="<?php if ( ! empty( $_POST['billing_account_number'] ) ) esc_attr_e( $billing_account_number ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action('woocommerce_created_customer', 'add_account_number');
function add_account_number($user_id)
{
$accountnumber = wp_rand(10000,99999);
update_user_meta($user_id, 'billing_account_number', $accountnumber);
}
OPTION 2:
add_action('user_register','aacount_on_user_register');
function account_on_user_register($user_id){
$last_name = $_POST['last_name'];
if ( isset($last_name) ){
$account_id = wp_rand(10000,99999) . $user_id;
add_user_meta( $user_id, 'account_id', $account_id);
}
}
/* Add user "account_id" meta when user registers */
add_action('woocommerce_created_customer', 'admin_email_on_registration', 10 , 1);
function admin_email_on_registration($user_id) {
$user = wp_get_current_user();
$last_name = $user->last_name;
if ( isset($last_name) ){
$account_id = wp_rand(10000,99999) . $user_id;
add_user_meta( $user_id, 'account_id', $account_id);
}
}
Here is a third option I was trying to make work:
function uuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
add_action('woocommerce_created_customer', 'generate_unique_id_after_user_created', 10, 3);
function generate_unique_id_after_user_created( $user_id, $form_id,$sub_id ) {
if( !empty( $user_id ) ) {
$user_data = array(
'ID' => $user_id,
'user_login' => uuid(),
'user_email' => '',
);
$updated_user_id = wp_update_user( $user_data );
if ( is_wp_error( $updated_user_id ) ) {
// There was an error, probably that user doesn't exist.
} else {
// Success!
}
}
}
I have a list of posts with one term assigned for one post.
I have 5 terms. I generated a list of the term used. I would like to display only post with the same term in my archive page.
archive.php :
$templates = array( 'archive.twig', 'index.twig' );
$context = Timber::get_context();
$context['categories'] = Timber::get_terms('category', array('hide_empty' => true));
$context['title'] = 'Archive';
if ( is_day() ) {
$context['title'] = 'Archive: '.get_the_date( 'D M Y' );
} else if ( is_month() ) {
$context['title'] = 'Archive: '.get_the_date( 'M Y' );
} else if ( is_year() ) {
$context['title'] = 'Archive: '.get_the_date( 'Y' );
} else if ( is_tag() ) {
$context['title'] = single_tag_title( '', false );
} else if ( is_category() ) {
$context['title'] = single_cat_title( '', false );
array_unshift( $templates, 'archive-' . get_query_var( 'cat' ) . '.twig' );
} else if ( is_post_type_archive() ) {
$context['title'] = post_type_archive_title( '', false );
array_unshift( $templates, 'archive-' . get_post_type() . '.twig' );
}
$context['posts'] = Timber::get_posts();
Timber::render( $templates, $context );
In my home.twig file where all post are display :
<ul class="category--list">
{% for cat in categories %}
<li>{{cat.name}}</li>
{% endfor %}
</ul>
What loop must I create in my "archive.twig" file to only display post in a term clicked ?
#Jardon, first step is to get the relevant term...
archive.php
$context['term'] = new Timber\Term();
//this will fetch whichever term is in the scope of that php file
then you just need to send it over to the twig file for display...
archive.twig
<h2>{{ term.name }}</h2>
Using the following code, I'm trying to allow only certain image types to be upload/selected with the WordPress media API. So I'm using add_filter on upload_mimes to restrict the mime types allowed. Using get_allowed_mime_types() I get an array containing only the mime types I want. However, when I click the change image button, I'm still able to upload files of mime types not listed before (such as PDF). What am I doing wrong?
public static function file_uploader( $element_id = null, $multiple = true )
{
add_filter( 'upload_mimes', array( 'JBLAB_Utils', 'images_upload_mimes' ) );
var_dump( get_allowed_mime_types() );
/**
* outputs:
* array(3) {
* ["jpg|jpeg|jpe"]=>
* string(10) "image/jpeg"
* ["gif"]=>
* string(9) "image/gif"
* ["png"]=>
* string(9) "image/png"
* }
*/
$multiple = ( $multiple === true ) ? 'true' : 'false';
wp_enqueue_script('jquery');
wp_enqueue_media();
?>
<div>
<?php
if ( empty( $element_id ) )
{
$element_id = "jblab_uploaded_file_url";
?>
<label for="jblab_uploaded_file_url"><?php _e( 'Image', 'jblab-radionomy' ); ?></label>
<input type="text" name="jblab_uploaded_file_url" id="jblab_uploaded_file_url" class="regular-text">
<?php
}
?>
<input type="button" name="jblab_upload_file_upload_btn" id="jblab_upload_upload_btn" class="button-secondary" value="<?php _e( 'Change Image', 'jblab-radionomy' ); ?>">
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#jblab_upload_upload_btn').click(function(e) {
e.preventDefault();
var image = wp.media({
title: '<?php echo str_replace( "'", "\'", __( 'Change Image', 'jblab-radionomy' ) ); ?>',
multiple: <?php echo $multiple; ?>
}).open()
.on('select', function(e){
var uploaded_image = image.state().get('selection').first();
var image_url = uploaded_image.toJSON().url;
<?php
echo "
if ($('#$element_id').is('img')) {
$('#$element_id').attr('src',image_url);
} else {
$('#$element_id').val(image_url);
}
";
?>
});
});
});
</script>
<?php
}
public static function images_upload_mimes ( $mimes = array() )
{
//unset( $mimes );
$mimes = array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
);
return $mimes;
}
Ok, so I found a solution to my issue.
I had to move the add_filter outside of the function that is calling the upload API. If anybody has the same problem, try to add_filters as soon as possible in the process, that should do the trick.