Adding WooCommerce Attribute via PHP code - attributes

I could see a lots of question on adding woocommerce product attribute. But doing so, I could see those added attributes in this screen
wp-admin/edit.php?post_type=product&page=product_attributes
My question is how can we add the attributes via PHP code to so that it appears in this above specified woocommerce attribute screen ? My intention is to make these attributes visible under 'Layered Navs' widget to filter out.

Following code will create the attribute programmatically which will be visible on the product_attributes page in the backend.
global $wpdb;
$insert = $wpdb->insert(
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
array(
'attribute_label' => 'name',
'attribute_name' => 'slug',
'attribute_type' => 'type',
'attribute_orderby' => 'order_by',
'attribute_public' => 1
),
array( '%s', '%s', '%s', '%s', '%d' )
);
if ( is_wp_error( $insert ) ) {
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_attribute', $insert->get_error_message(), 400 );
}
// Clear transients
delete_transient( 'wc_attribute_taxonomies' );
Change name, slug etc with the relevant values.

Related

How to use select query for creating form in drupal 8?

I have a table in my database. I want to use the data of that table and create form of that data. I created a module and made a form.php page. Wrote select query in function but how to make form from that data?
I believe you want to create listing page of the table data. Here is how you can create a tabular listing with pagination and column sorting.
Suppose you have a table named "students" with the below fields.
id, name, email
1. Create a controller in your module in the below path.
modules/your_module/Src/Controller/StudentsController.php
<?php
namespace Drupal\your_module\Controller;
use Drupal\Core\Controller\ControllerBase;
class StudentsController extends ControllerBase {
public function __construct() {
}
public function list() {
$header = array(
array('data' => t('ID'), 'field' => 'st.id'),
array('data' => t('Name'), 'field' => 'st.name'),
array('data' => t('Email'), 'field' => 'st.email'),
);
$query = db_select('students', 'st')
->fields('st', array('id', 'name', 'email'))
->extend('Drupal\Core\Database\Query\TableSortExtender')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->orderByHeader($header);
$data = $query->execute();
$rows = array();
foreach ($data as $row) {
$rows[] = array('data' => (array) $row);
}
$build['table_pager'][] = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
);
$build['table_pager'][] = array(
'#type' => 'pager',
);
return $build;
}
}
So your controller action is ready, now you have to add routing in order to create path to this listing page.
2. Create routing.yml file inside your module folder as the file name mentioned below, and the code below.
modules/your_module/your_module.routing.yml
students.list:
path: 'admin/config/students/list'
defaults:
_controller: 'Drupal\your_module\Controller\StudentsController::list'
_title: 'Students List'
requirements:
_permission: 'access students list'
3. To create permissions, you can create the followin ing file in your module.
modules/your_module/your_module.permissions.yml
access students list:
title: 'Access students list page'
Clear CMS cache
Go to People => Permissions, Enable the permission for relevant user roles.
Then, browse your page "admin/config/students/list"

Magento - custom product option don't show in order

I'm try to add custom option to product programmatically whyle add him to cart. I'm use:
$a_options = array(
'options' => array(
'label' => 'Glove Size',
'value' => $attr_value ,
)
);
$item->addOption(new Varien_Object(
array(
'product' => $item->getProduct(),
'code' => 'additional_options',
'value' => serialize($a_options)
)
));
$quote->addItem($item);
This is shows option for product in cart and during checkout process, but don't show option in order information.
I also tried:
$item->getProduct()->addCustomOption('additional_options', $attr_value );
Try to show them via attributes - didn't help.
$params = array('product' => '1919','qty' => 1,
'options' => array(
'glove_size' => $gloves_id,
),);
$cart->addProduct('1919', $params);
Magento version is 1.5
I haven't check that in 1.5 version but the below code will work in 1.7.2 version:
For viewing the custom options you need set options in order items.That can be done through by calling an event sales_convert_quote_item_to_order_item
<sales_convert_quote_item_to_order_item>
<observers>
<jrb_setcustomoption_observer>
<type>singleton</type>
<class>jrb_setcustomoption/observer</class>
<method>salesConvertQuoteItemToOrderItem</method>
</jrb_setcustomoption_observer>
</observers>
</sales_convert_quote_item_to_order_item>
Set the details options in your observer
public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
$quoteItem = $observer->getItem();
if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
$orderItem = $observer->getOrderItem();
$options = $orderItem->getProductOptions();
$options['additional_options'] = unserialize($additionalOptions->getValue());
$orderItem->setProductOptions($options);
}
}
For More details you can find in this article:
Magento - custom product option don't show in order
Thanks to Vinai

Yii CGrid View view all after search

I need some help, I've been figuring this for almost an hour and still can't find a way to fix this.
I have a search form to filter data from my CGridView, and I also need a button to view all the records after I finish using search, but I can't figure how to do it. Here's my code for jquery refresh button:
Yii::app()->clientScript->registerScript('initRefresh',<<<JS
$('#refresh-button').on('click',function() {
$('#app-asset-categories-grid').yiiGridView('update');
return false;
});
JS
,CClientScript::POS_READY);
Here's my code for jquery search form:
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#app-asset-categories-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
And finally, my grid:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'id'=>'app-asset-categories-grid',
'columns' => array(
'category_id',
'label',
'description',
'add_date',
'modification_date',
array(
'name'=>'Status',
'filter'=>array('1'=>'Active', '0'=>'Inactive'),
'value'=>'($data->status=="1")?"Active":"Inactive"'
),
array(
'name'=>'Deletion Status',
'filter'=>array('1'=>'Deactivate','0'=>'Active'),
'value'=>'($data->deletion_status=="1")?"Deactivated":"Activated"'
),
array(
'class'=>'CButtonColumn',
),
),
));
I just wanted a refresh button to display all the data again after I use my search form. I need your help guys, thanks.
You need to handle update action in model and set pagination parameter false. An example how to do it:
Add hidden field in search form _search.php
<?php echo CHtml::hiddenField('show_all', 0, array("id"=>"show_all")); ?>
Then handle it in model's search() function (your grid uses it to get data)
like this:
$criteria=new CDbCriteria;
// some criterias
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => (isset($_GET["show_all"]) && $_GET["show_all"])?false:array(), // add this line
));
Then set update hidden field when you need it.
$('#refresh-button').on('click',function() {
$('#show_all').val(1); // set it to show all
$('#app-asset-categories-grid').yiiGridView('update');
$('#show_all').val(0); // set to 0 for searching with pagination
return false;
});
Thats it.

Search through related model in Yii

I have a problem with searching in Yii. I have two models: Teams and Workers. On website there is a page called 'Team Workers' where I want to display CGridView widget with searching that displays Workers from the team (team id is passed as a _GET parameter).
I did this in TeamsController:
public function actionWorkers($id)
{
$model = Teams::model()->findByPk($id);
$workers = Workers::model();
$workers->unsetAttributes();
if(isset($_GET['Workers']))
{
$_GET['Workers']['idTeam'] = $id;
$workers->attributes = $_GET['Workers'];
}
else {
$workers->attributes = array('idTeam' => $id);
}
$teamWorkers = $workers;
$this->render('workers', array(
'model' => $model,
'teamWorkers' => $teamWorkers
));
}
And in the view file:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'team-workers-grid',
'dataProvider'=>$teamWorkers->search(),
'filter' => $teamWorkers,
'columns'=>array(
'name',
'surname',
array(
'id' => 'idWorker',
'class' => 'CCheckBoxColumn',
'checked' => '$data->confirmer',
'selectableRows' => '2',
// 'headerTemplate' => '{item}'
)
),
)); ?>
I got the error:
CDbCommand nie zdołał wykonać instrukcji SQL: SQLSTATE[23000]: Integrity constraint
violation: 1052 Column 'idTeam' in where clause is ambiguous. The SQL statement
executed was: SELECT COUNT(DISTINCT `t`.`idWorker`) FROM `workers` `t` LEFT OUTER JOIN
`teams` `Team` ON (`t`.`idTeam`=`Team`.`idTeam`) WHERE ((idTeam=:ycp0) AND (Team.name
LIKE :ycp1))
When I dont set idTeam attribute - it works fine. It's pretty weird - at the regular CRUD admin page - idTeam attribute is passed and that works fine.
Hot to deal with it?
In Workers::search() you have something like
$criteria->compare('idTeam',$this->idTeam);
Change it to
$criteria->compare('t.idTeam',$this->idTeam);
i.e prefix sql attribute with t. if it is from current model or with relation name if from other table/model
Also instead of:
$workers->attributes = array('idTeam' => $id);
yould could keep it simpler with:
$workers->idTeam = $id;
You have defined the column idTeam in Team and Workers. By joining those tables you would have a duplicate ("ambiguous") column in the result. That's what the error message tells you.
To solve this you have to use an alias for one of the columns.

Infinitescroll to finish after a certain number of posts

I am using Paul Irish's infinitescroll with masonry js on a wordpress site. It is a site with a lot of content. I want infintescroll to stop adding new content when it reaches post number 40 and to give the "No additional items" message at that point. I tried to customize the wordpress loop to only return 40 posts but that did not seem to work.
I thought that maybe one of the options in infinitecroll might do the trick but the infintescroll documentation is very sparse. For example, there is an infinitescroll option in the "loading" init section called "finished: undefined" Is it possible to change that parameter to stop the scrolling after a certain number of content items?
Is there some other obvious way to control when infinitescroll stops loading new content?
Any assistance would be greatly appreciated.
In the Administration -> Settings -> Reading you can set Blog pages show at most to 40.
With code:
Two ways I've done Masonry by numbers like your question I've had success with the following:
limit posts_per_page in your query arguments
$args = array(
'posts_per_page' => 40,
'offset' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'exclude' => 'none',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = new WP_Query( $args );
if ( $posts -> have_posts()) {
while ( $posts -> have_posts() ) : $posts->the_post(); {
//do stuff with the posts returned here
}
}
or by incrementing:
$counts = 0 ;
$posts = new WP_Query( $args );
if ( $posts -> have_posts()) {
while ( $posts -> have_posts() ) : $posts->the_post(); {
//do stuff with the posts returned here
$counts = ++;
if($counts == 40) { return }
}
}

Resources