I want to change following code from Elementor:
$this->add_render_attribute( 'button',
[
'rel' => 'nofollow',
'href' => $product->add_to_cart_url(),
'data-quantity' => ( isset( $settings['quantity'] ) ? $settings['quantity'] : 1 ),
'data-product_id' => $product->get_id(),
'class' => $class,
]
);
to add new value like this:
foreach (get_the_terms(get_the_ID(), 'producer') as $cat) {
echo $cat->name;
}
for the 'href' part in first code:
'href' => $product->add_to_cart_url(),
above 'href' code adds this value in path ?add-to-cart=2809 and I want to make it look like this ?add-to-cart=2809&wdm_name=MY-CODE-FOR-EACH-ABOVE-CREATE-VALUE-HERE
How do I implement second code inside HREF part so I dont get the error.
Thanks
Actually, just a few days ago I had the need of changing existing URLs inside href for each category on a widget my shop page.
So for my case I ended up creating this code:
var $ = jQuery;
$(document).ready(function(){
var search = window.location.search; // Get all existing search params in URL
var urlOrigin = document.location.origin; // Get current URL (only domain)
//console.log(urlOrigin); // For testing in browser console
$('.widget-area .product-categories li a').each(function(){ // You can mobify this to get your elements where you have them listed
var catSlug = $(this).attr('href').replace(urlOrigin,''); // I am doing this because I need to get the slug for each category to then append to the final URL
url = urlOrigin+catSlug+search; // Final URL mounted, here is where you can put your variable to set the code you want for each URL
$(this).attr('href',url); // Associate this new generated URL to the element
});
});
Since it is not clear where/how you want to use it, I leave my code here in case you can use it to adapt to your case, which I belive you can.
Related
I need to add target="_blank" to links I'm retrieving using this PHP code on a WordPress+WooCommerce installation:
$products = wc_get_products([
'status' => 'publish',
'limit' => -1,
'product_cat' => 'talleres'
]);
$locaciones = array_map(function ($product) {
return [
// more code goes here, I just deleted it for this question
'popup_html' => $product->get_categories() // this is where I need the target="_blank"
];
}, $products);
I tried with jQuery:
$(document).ready( function(){
$('a').attr('target', '_blank');
});
But it is not working, it doesn't add the target="_blank" to the link.
I'm thinking maybe this can be added directly to the PHP code?
You can use a php regex filter like preg_replace and search by "href" and replace the match putting the target="_blank" before the href resulting in "target='_blank' href"
You may use this script. you need to add class or parent div of Anchor tag otherwise it will add to all anchors.
jQuery(document).ready( function($){
$('a').each(function(){
$(this).attr('target', '_blank');
});
});
Since I needed to open all and every link on a new tab, I just used <base target="_blank"> for the entire page.
I have following code:
return array(
ULogt::UPDATE => '
<div>
Navigate
</div>
'
This code locates in the class called links.php.
I need to navigate to ('viewform', array('model'=>$this->loadJson($id)), when the user presses Navigate button. I do not know how to insert this code instead of #link. How can I do it?
CHtml::link(
"Navigate",
"javascript:void(0);", // link for destination or you can handle same with jQuery
array(
'id' => 'navigation-id', // id for handeling in jQuery
'key' => $data, // Required data will be appeared as attributes for this link
)
);
You can create link like
Yii::app()->createUrl(
'/profile/membership/view', // your link
array(
'id'=> 1 // data to be sent
)
)
Check for URL formating
I was trying to add elements dynamically to a yii 1.1 TbActiveForm. I tried two methods to do this, but fails when it comes to validation. Please see my methods below.
Method 1 # Clone elements; modify id's
This is how i create form
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'form-name',
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit'=>true
),
'action' => $this->createUrl('test/manageusers')
));
On clicking on the "Add more" button, this script will duplicate elements
$('#add-comp-user').on('click', function(){
// template
var html = $('.add-comp-users-wrapper').first().clone();
// next element index
var next_index = // find last element's index attribute
// update element id's
html.find(':input').each(function(){
// update name
// update id
});
// insert to DOM
});
Method 2 #Ajax method
Here, on cliking on "Add more" button, i will render form elements with new id and insert to DOM
Both these methods failed in validation. How can i include a newly added element to Yii validation ?
i've been using Yii framework for some time now, and i've been really having a good time especially with these widgets that makes the development easier. I'm using Yii bootsrap for my extensions..but i'm having a little trouble understanding how each widget works.
My question is how do i display the widget say a TbDetailView inside a tab?
i basically want to display contents in tab forms..however some of them are in table forms...some are in lists, detailviews etc.
I have this widget :
$this->widget('bootstrap.widgets.TbDetailView',array(
'data'=>$model,
'attributes'=>$attributes1,
));
that i want to put inside a tab
$this->widget('bootstrap.widgets.TbWizard', array(
'tabs' => $tabs,
'type' => 'tabs', // 'tabs' or 'pills'
'options' => array(
'onTabShow' => 'js:function(tab, navigation, index) {
var $total = navigation.find("li").length;
var $current = index+1;
var $percent = ($current/$total) * 100;
$("#wizard-bar > .bar").css({width:$percent+"%"});
}',
),
and my $tabs array is declared like this :
$tabs = array('studydetails' =>
array(
'id'=>'f1study-create-studydetails',
'label' => 'Study Details',
'content' =>//what do i put here?),
...
...);
when i store the widget inside a variable like a $table = $this->widget('boots....);
and use the $table variable for the 'content' parameter i get an error message like:
Object of class TbDetailView could not be converted to string
I don't quite seem to understand how this works...i need help..Thanks :)
You can use a renderPartial() directly in your content, like this:
'content'=>$this->renderPartial('_tabpage1', [] ,true),
Now yii will try to render a file called '_tabpage1.php' which should be in the same folder as the view rendering the wizard. You must return what renderPartial generates instead of rendering it directly, thus set the 3rd parameter to true.
The third parameter that the widget() function takes is used to capture output into a variable like you are trying to do.
from the docs:
public mixed widget(string $className, array $properties=array ( ), boolean $captureOutput=false)
$this->widget('class', array(options), true)
Right now you are capturing the object itself in the variable trying to echo out an object. Echo only works for things that can be cast to a string.
I'm new to cakephp...and I have a page with a url this:
http://localhost/books/filteredByAuthor/John-Doe
so the controller is ´books´, the action is ´filteredByAuthor´ and ´John-Doe´ is a parameter.. but the url looks ugly so i've added a Route like this:
Router::connect('/author/:name', array( 'controller' => 'books','action' => 'filteredByAuthor'), array('pass'=>array('name'),'name'=>".*"));
and now my link is:
http://localhost/author/John-Doe
the problem is that the view has a paginator and when i change the page (by clicking on the next or prev button).. the paginator won't consider my routing... and will change the url to this
http://localhost/books/filteredByAuthor/John-Doe/page:2
the code on my view is just:
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
the documentation doesn't say anything about avoiding this and i've spent hours reading the paginators source code and api.. and in the end i just want my links to be something like this: (with the sort and direction included on the url)
http://localhost/author/John-Doe/1/name/asc
Is it possible to do that and how?
hate to answer my own question... but this might save some time to another developper =) (is all about getting good karma)
i found out that you can pass an "options" array to the paginator, and inside that array you can specify the url (an array of: controller, action and parameters) that the paginator will use to create the links.. so you have to write all the possible routes in the routes.php file. Basically there are 3 possibilities:
when the "page" is not defined
For example:
http://localhost/author/John-Doe
the paginator will assume that the it's the first page. The corresponding route would be:
Router::connect('/author/:name', array( 'controller' => 'books','action' => 'filteredByAuthor'),array('pass'=>array('name'),'name'=>'[a-zA-Z\-]+'));
when the "page" is defined
For example:
http://localhost/author/John-Doe/3 (page 3)
The route would be:
Router::connect('/author/:name/:page', array( 'controller' => 'books','action' => 'filteredByAuthor'),array('pass'=>array('name','page'),'name'=>'[a-zA-Z\-]+','page'=>'[0-9]+'));
finally when the page and the sort is defined on the url (by clicking on the sort links created by the paginator).
For example:
http://localhost/author/John-Doe/3/title/desc (John Doe's books ordered desc by title)
The route is:
Router::connect('/author/:name/:page/:sort/:direction', array( 'controller' => 'books','action' => 'filteredByAuthor'),
array('pass'=>array('name','page','sort','direction'),
'name'=>"[a-zA-Z\-]+",
'page'=>'[0-9]*',
'sort'=>'[a-zA-Z\.]+',
'direction'=>'[a-z]+',
));
on the view you have to unset the url created by the paginator, cause you'll specify your own url array on the controller:
Controller:
function filteredByAuthor($name = null,$page = null , $sort = null , $direction = null){
$option_url = array('controller'=>'books','action'=>'filteredByAuthor','name'=>$name);
if($sort){
$this->passedArgs['sort'] = $sort;
$options_url['sort'] = $sort;
}
if($direction){
$this->passedArgs['direction'] = $direction;
$options_url['direction'] = $direction;
}
Send the variable $options_url to the view using set()... so in the view you'll need to do this:
View:
unset($this->Paginator->options['url']);
echo $this->Paginator->prev(__('« Précédente', true), array('url'=>$options_url), null, array('class'=>'disabled'));
echo $this->Paginator->numbers(array('separator'=>'','url'=>$options_url));
echo $this->Paginator->next(__('Suivante »', true), array('url'=>$options_url), null, array('class' => 'disabled'));
Now, on the sort links you'll need to unset the variables 'sort' and 'direction'. We already used them to create the links on the paginator, but if we dont delete them, then the sort() function will use them... and we wont be able to sort =)
$options_sort = $options_url;
unset($options_sort['direction']);
unset($options_sort['sort']);
echo $this->Paginator->sort('Produit <span> </span>', 'title',array('escape'=>false,'url'=>$options_sort));
hope this helps =)