Related
I would like to find a way to override the following file in the plugin Elementor Pro for wordpress to add an "else" in "protected function render()". I want to be able to display a message when there is no upsell available in my carrousel from element pro.
<?php
namespace ElementorPro\Modules\Woocommerce\Widgets;
use Elementor\Controls_Manager;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Group_Control_Typography;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Product_Upsell extends Products_Base {
public function get_name() {
return 'woocommerce-product-upsell';
}
public function get_title() {
return esc_html__( 'Upsells', 'elementor-pro' );
}
public function get_icon() {
return 'eicon-product-upsell';
}
public function get_keywords() {
return [ 'woocommerce', 'shop', 'store', 'upsell', 'product' ];
}
protected function register_controls() {
$this->start_controls_section(
'section_upsell_content',
[
'label' => esc_html__( 'Upsells', 'elementor-pro' ),
]
);
$this->add_columns_responsive_control();
$this->add_control(
'orderby',
[
'label' => esc_html__( 'Order By', 'elementor-pro' ),
'type' => Controls_Manager::SELECT,
'default' => 'date',
'options' => [
'date' => esc_html__( 'Date', 'elementor-pro' ),
'title' => esc_html__( 'Title', 'elementor-pro' ),
'price' => esc_html__( 'Price', 'elementor-pro' ),
'popularity' => esc_html__( 'Popularity', 'elementor-pro' ),
'rating' => esc_html__( 'Rating', 'elementor-pro' ),
'rand' => esc_html__( 'Random', 'elementor-pro' ),
'menu_order' => esc_html__( 'Menu Order', 'elementor-pro' ),
],
]
);
$this->add_control(
'order',
[
'label' => esc_html__( 'Order', 'elementor-pro' ),
'type' => Controls_Manager::SELECT,
'default' => 'desc',
'options' => [
'asc' => esc_html__( 'ASC', 'elementor-pro' ),
'desc' => esc_html__( 'DESC', 'elementor-pro' ),
],
]
);
$this->end_controls_section();
parent::register_controls();
$this->start_injection( [
'at' => 'before',
'of' => 'section_design_box',
] );
$this->start_controls_section(
'section_heading_style',
[
'label' => esc_html__( 'Heading', 'elementor-pro' ),
'tab' => Controls_Manager::TAB_STYLE,
]
);
$this->add_control(
'show_heading',
[
'label' => esc_html__( 'Heading', 'elementor-pro' ),
'type' => Controls_Manager::SWITCHER,
'label_off' => esc_html__( 'Hide', 'elementor-pro' ),
'label_on' => esc_html__( 'Show', 'elementor-pro' ),
'default' => 'yes',
'return_value' => 'yes',
'prefix_class' => 'show-heading-',
]
);
$this->add_control(
'heading_color',
[
'label' => esc_html__( 'Color', 'elementor-pro' ),
'type' => Controls_Manager::COLOR,
'global' => [
'default' => Global_Colors::COLOR_PRIMARY,
],
'selectors' => [
'{{WRAPPER}}.elementor-wc-products .products > h2' => 'color: {{VALUE}}',
],
'condition' => [
'show_heading!' => '',
],
]
);
$this->add_group_control(
Group_Control_Typography::get_type(),
[
'name' => 'heading_typography',
'global' => [
'default' => Global_Typography::TYPOGRAPHY_PRIMARY,
],
'selector' => '{{WRAPPER}}.elementor-wc-products .products > h2',
'condition' => [
'show_heading!' => '',
],
]
);
$this->add_responsive_control(
'heading_text_align',
[
'label' => esc_html__( 'Text Align', 'elementor-pro' ),
'type' => Controls_Manager::CHOOSE,
'options' => [
'left' => [
'title' => esc_html__( 'Left', 'elementor-pro' ),
'icon' => 'eicon-text-align-left',
],
'center' => [
'title' => esc_html__( 'Center', 'elementor-pro' ),
'icon' => 'eicon-text-align-center',
],
'right' => [
'title' => esc_html__( 'Right', 'elementor-pro' ),
'icon' => 'eicon-text-align-right',
],
],
'selectors' => [
'{{WRAPPER}}.elementor-wc-products .products > h2' => 'text-align: {{VALUE}}',
],
'condition' => [
'show_heading!' => '',
],
]
);
$this->add_responsive_control(
'heading_spacing',
[
'label' => esc_html__( 'Spacing', 'elementor-pro' ),
'type' => Controls_Manager::SLIDER,
'size_units' => [ 'px', 'em' ],
'selectors' => [
'{{WRAPPER}}.elementor-wc-products .products > h2' => 'margin-bottom: {{SIZE}}{{UNIT}}',
],
'condition' => [
'show_heading!' => '',
],
]
);
$this->end_controls_section();
$this->end_injection();
}
protected function render() {
$settings = $this->get_settings_for_display();
// Add a wrapper class to the Add to Cart & View Items elements if the automically_align_buttons switch has been selected.
if ( 'yes' === $settings['automatically_align_buttons'] ) {
add_filter( 'woocommerce_loop_add_to_cart_link', [ $this, 'add_to_cart_wrapper' ], 10, 1 );
}
$limit = '-1';
$columns = 4;
$orderby = 'rand';
$order = 'desc';
if ( ! empty( $settings['columns'] ) ) {
$columns = $settings['columns'];
}
if ( ! empty( $settings['orderby'] ) ) {
$orderby = $settings['orderby'];
}
if ( ! empty( $settings['order'] ) ) {
$order = $settings['order'];
}
ob_start();
woocommerce_upsell_display(
sanitize_text_field( $limit ),
sanitize_text_field( $columns ),
sanitize_text_field( $orderby ),
sanitize_text_field( $order )
);
$upsells_html = ob_get_clean();
if ( $upsells_html ) {
$upsells_html = str_replace( '<ul class="products', '<ul class="products elementor-grid', $upsells_html );
// PHPCS - Doesn't need to be escaped since it's a WooCommerce template, and 3rd party plugins might hook into it.
echo $upsells_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
///////// The code I want to add ///////
else {
echo "no product available";
}
///////// The code I want to add ///////
if ( 'yes' === $settings['automatically_align_buttons'] ) {
remove_filter( 'woocommerce_loop_add_to_cart_link', [ $this, 'add_to_cart_wrapper' ] );
}
}
public function render_plain_content() {}
public function get_group_name() {
return 'woocommerce';
}
}
I tried many things but with no success. For example, put the same file in child theme with same path or calling it with the namespace. I can't use the filters and I have no idea how to do it properly. Right now it is working but if I update the plugin I loose the modification.
I am trying to allow customers to change their role on their WordPress account page and all works very well functionality wise except what is being shown on the front end. When they come to change their role, the option selected is always "Select an option..." rather than the actual role that they are designated to (such as Example 1). I have excluded some roles (such as Administrator) as I only want to show them certain roles. What do i change in the below code to fix this and show their current role?
function iconic_get_account_fields() {
return apply_filters( 'iconic_account_fields', array(
'first_name' => array(
'type' => 'text',
'label' => __( 'First Name', 'iconic' ),
'hide_in_account' => true,
'hide_in_admin' => true,
'hide_in_checkout' => true,
'hide_in_registration' => false,
'required' => true,
),
'last_name' => array(
'type' => 'text',
'label' => __( 'Last Name', 'iconic' ),
'hide_in_account' => true,
'hide_in_admin' => true,
'hide_in_checkout' => true,
'hide_in_registration' => false,
'required' => true,
),
'role' => array(
'type' => 'select',
'label' => __( 'Franking Machine Model', 'iconic' ),
'hide_in_account' => false,
'hide_in_admin' => true,
'hide_in_registration' => true,
'hide_in_checkout' => true,
'options' => array(
'' => __( 'Select an option...', 'iconic' ),
'example_1_customer' => __( 'Example 1', 'iconic' ),
'example_2_customer' => __( 'Example 2', 'iconic' ),
'example_3_customer' => __( 'Example 3', 'iconic' ),
'other_customer' => __( 'Other', 'iconic' ),
),
),
) );
}
Get current role of user using below function
function get_current_user_role(){
$current_user = wp_get_current_user();
$roles = $current_user->roles;
return $roles[0];
}
SOLUTION #1
Pass it to woocommerce_form_field($key, $args, $value) function , consider below example ,
$fields = iconic_get_account_fields();
$role = get_current_user_role();
foreach ($fields as $key => $field_args) {
if ($key == 'role') {
woocommerce_form_field($key, $field_args, $role);
} else {
woocommerce_form_field($key, $field_args);
}
}
SOLUTION #2
Make modification of function you shared as below , I have added 'value' argument.
function iconic_get_account_fields() {
return apply_filters('iconic_account_fields', array(
'first_name' => array(
'type' => 'text',
'label' => __('First Name', 'iconic'),
'hide_in_account' => true,
'hide_in_admin' => true,
'hide_in_checkout' => true,
'hide_in_registration' => false,
'required' => true,
),
'last_name' => array(
'type' => 'text',
'label' => __('Last Name', 'iconic'),
'hide_in_account' => true,
'hide_in_admin' => true,
'hide_in_checkout' => true,
'hide_in_registration' => false,
'required' => true,
),
'role' => array(
'type' => 'select',
'label' => __('Franking Machine Model', 'iconic'),
'hide_in_account' => false,
'hide_in_admin' => true,
'hide_in_registration' => true,
'hide_in_checkout' => true,
'options' => array(
'' => __('Select an option...', 'iconic'),
'example_1_customer' => __('Example 1', 'iconic'),
'example_2_customer' => __('Example 2', 'iconic'),
'example_3_customer' => __('Example 3', 'iconic'),
'other_customer' => __('Other', 'iconic'),
),
'value' => apply_filters('selectedrole','') // added new arg
),
));
add_filter('selectedrole', 'get_current_user_role');
Let me know which work best for you.
I am learning ZF3, i am trying to add InfoController to the Album module. would my URL be ....../album/info? I am getting 404 error occurred. I have seen John Deck's post and implemented exactly the same, but still not working
This is my module.config.php
<?php
namespace Album;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return array(
'controllers' => [
'factories' => [
Controller\InfoController::class => function($container) {
return new Controller\InfoController(
$container->get(\Album\Model\InfoTable::class)
);
},
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(\Album\Model\AlbumTable::class)
);
},
],
'aliases' => [
'index' => AlbumController::class,
'info' => InfoController::class,
]
],
'router' => array(
'routes' => array(
'album' => array(
'type' => Segment::class,
'options' => array(
'route' => '/album[/:action[/:id]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\AlbumController',//Controller::class,
'action' => 'index',
),
),
),
'info' => array(
'type' => Segment::class,
'options' => array(
//'route' => 'Album/Controller/Info[/:action[/:id]]',
'route' => '/InfoController[/:action[/:id]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Info',//::class
'action' => 'index',
),
),
),
),
),
'view_manager' => [
'display_not_found_reason' => false,
'display_exceptions' => false,
'doctype' => 'HTML5',
'template_map' => [
'layout/album' => __DIR__ . '/../view/layout/album_layout.phtml',
'album/album/index' => __DIR__ . '/../view/album/album/index.phtml',
'layout/album' => __DIR__ . '/../view/layout/info_layout.phtml',
'info/info/index' => __DIR__ . '/../view/info/info/index.phtml',
],
'template_path_stack' => [
'Album' => __DIR__ . '/../view',
'Info' => __DIR__ . '/../view',
],
],
);
Here's my Module.php
<?php
namespace Album;
use Album\Model\InfoTable;
use Album\Model\Info;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
Model\AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new Model\AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
Model\InfoTable::class => function($container) {
$tableGateway = $container->get(Model\InfoTableGateway::class);
return new Model\InfoTable($tableGateway);
},
Model\InfoTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Info());
return new TableGateway('info', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
public function getControllerConfig()
{
return array(
'factories' => array(
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(Model\AlbumTable::class)
);
},
Controller\InfoController::class => function($container) {
return new Controller\InfoController(
$container->get(Model\InfoTable::class)
);
},
),
);
}
}
I am trying to create a view programmatically in Drupal 8. Something similar to the crud log module in Drupal 7. I couldn't find any references on the internet either.
I was successful in creating a view programatically----I did the following--
1. Create a folder--C:\xampp\htdocs\Drupal Instance\modules
2. Create a YML info file --first_view.info and add the following code to it----
name: First View
type: module
description: My First Drupal 8 View
package: Custom
core: 8.x
3. Create an install file---first_view.install
And add the following code to it
<?php
/**
* #file
* Install, schema, and uninstall functions for the First View module.
*/
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Term;
/**
* Implements hook_install().
*/
function first_view_install() {
}
/**
* Implements hook_uninstall().
*/
function first_view_uninstall() {
}
/**
* Implements hook_schema().
*/
function first_view_schema() {
$schema['first_view_table'] = [
// Example (partial) specification for table "node".
'description' => 'The base table for first_view.',
'fields' => [
'id' => [
'description' => 'The primary identifier for a node.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'name' => [
'description' => 'The name of Employee.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
],
'age' => [
'description' => 'The age of employee.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'is_active' => [
'description' => 'The activity of employee.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'timestamp' => [
'description' => 'The timestamp of employee.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'project_code' => [
'description' => 'The project code of employee.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => 0,
],
],
'unique keys' => [
'id' => ['id'],
],
// For documentation purposes only; foreign keys are not created in the
// database.
'primary key' => ['id'],
];
return $schema;
}
4. Create a file---first_view.views.inc
And add the following code to it--
<?php
/**
* Implements hook_views_data().
*/
function first_view_views_data() {
$data = [];
$data['first_view_table'] = [];
$data['first_view_table']['table'] = [];
$data['first_view_table']['table']['group'] = t('First View table');
$data['first_view_table']['table']['provider'] = 'first_view_module';
$data['first_view_table']['table']['base'] = [
'field' => 'id',
'title' => t('First View table'),
'help' => t('First View table contains example content and can be related to nodes.'),
'weight' => -10,
];
$data['first_view']['table']['join'] = [
'node_field_data' => [
'left_field' => 'id',
'field' => 'id',
'extra' => [
0 => [
'field' => 'published',
'value' => TRUE,
],
1 => [
'left_field' => 'age',
'value' => 1,
'numeric' => TRUE,
],
2 => [
'field' => 'published',
'left_field' => 'is_active',
'operator' => '!=',
],
],
],
];
$data['first_view_table']['table']['join']['node_field_data'] = [
'left_table' => 'foo',
'left_field' => 'id',
'field' => 'id',
'extra' => [
['left_field' => 'project_code', 'field' => 'project_code'],
['field' => 'age', 'value' => 0, 'numeric' => TRUE, 'operator' => '>'],
],
];
$data['first_view_table']['id'] = [
'title' => t('Example content'),
'help' => t('Relate example content to the node content'),
'relationship' => [
'base' => 'node_field_data',
'base field' => 'id',
'id' => 'standard',
'label' => t('Example node'),
],
];
$data['first_view_table']['name'] = [
'title' => t('Name'),
'help' => t('Just a Name field.'),
'field' => [
'id' => 'standard',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'string',
],
'argument' => [
'id' => 'string',
],
];
$data['first_view_table']['project_code'] = [
'title' => t('Project Code'),
'help' => t('Just a Project code field.'),
'field' => [
'id' => 'standard',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'string',
],
'argument' => [
'id' => 'string',
],
];
$data['first_view_table']['age'] = [
'title' => t('Age'),
'help' => t('Just a numeric field.'),
'field' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
];
$data['first_view_table']['is_active'] = [
'title' => t('Is Active'),
'help' => t('Just an on/off field.'),
'field' => [
'id' => 'boolean',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'boolean',
'label' => t('Published'),
'type' => 'yes-no',
'use_equal' => TRUE,
],
];
$data['first_view_table']['timestamp'] = [
'title' => t('Timestamp'),
'help' => t('Just a timestamp field.'),
'field' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
];
$data['views']['area'] = [
'title' => t('Text area'),
'help' => t('Provide markup text for the area.'),
'area' => [
'id' => 'text',
],
];
return $data;
}
By following the above steps....when we install and enable the module a table gets created in the database...You will have to populate it in order to see some data in the view...don't forget to add dummy data in the table.....After that go to Structure/views--- And click on "Add View"----Give a name to your View---and then you will be able to see the table name in "Show" Drop Down box---In this case the table name is "First View Table"...I hope this article would be helpful....
Although it is not created from scratch programatically, here is a simple method:
create a view using the back office
export that single view using the configuration manager module
copy that yml file and place it in a basic custom module /config/install folder
remove the first line containing the uuid
(optional )modify the file manually if needed and if understood how it work
activating your custom module will import the view
You can create a new View through the ConfigEntityStorage.
Create the View in the UI. Export the View's YAML config file to a path in your module, removing the UUID.
// view config is in `my_module/config/install/views.view.my_view.yml`
// (uuid removed!)
$dir = drupal_get_path('module', 'my_module');
$fileStorage = new FileStorage($dir);
$config = $fileStorage->read('views.view.my_view');
/** #var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
$storage = \Drupal::entityTypeManager()
->getStorage('view');
/** #var \Drupal\views\Entity\View $view */
$view = $storage->create($config);
$view->save();
I am trying to create my first drupal module that creates a simple node type wcNames. This node type has two fields, firstname & lastname.
I could get my module create the DB table for node type and add it in node_type table, but when I open node/add/wc-name fields firstname and lastname don't appear.
Below is my code. Please suggest.
<?php
// wc_name.install file
/*
* hook_schema Implementation: Creates the DB and it's fields
*/
function wc_name_schema(){
$schema['wc_name_names'] = array(
'fields' => array(
'nmid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
'firstname' => array('type' => 'varchar', 'length' => 50, 'not null' => FALSE),
'lastname' => array('type' => 'varchar', 'length' => 50, 'not null' => FALSE),
),
'primary key' => array('nmid'),
'unique keys' => array(),
'indexes' => array(
'nmid' => array('nmid'),
),
);
return $schema;
}
//function wc_name_install(){ } // Use this function to set variables for the module.
function wc_name_uninstall(){
//unset variables if any set
// Delete all wc_name nodes
db_delete('node')->condition('type', 'wc_name')->execute();
// Delete all wc_name tables
db_drop_table('wc_name_names');
}
?>
<?php
// wc_name.module
/*
* Implementation of _node_info(): define node
*/
function wc_name_node_info(){
return array(
'wc_name' => array(
'name' => t('wcName'),
'base' => 'wc_name',
'base' => 'page',
'description' => t("A smaple module to save names."),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => FALSE,
)
);
}
function wc_name_menu() {
$items = array();
$items['wc_name'] = array(
'title' => 'wcNames',
'page callback' => 'wc_name',
// 'access arguments' => array('access wcNames'),
);
}
?>
<?php
// wc_name_form.inc
function wc_name_form($node, &$form_state){
// Add fields
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => FALSE,
'#default_value' => $node->wc_name['firstname'],
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => FALSE,
'#default_value' => $node->wc_name['lastname'],
);
return $form;
}
?>
What am I missing?
Try this:
function createTextFieldByName($type, $fieldname, $fieldlabel)
{
$field = field_info_field($fieldname);
if (empty($field)) {
$field = array(
'field_name' => $fieldname,
'type' => 'text',
'entity_types' => array('node'),
'translatable' => TRUE,
);
$field = field_create_field($field);
}
$instance = field_info_instance('node', $fieldname, $type);
if (empty($instance)) {
$instance = array(
'field_name' => $fieldname,
'entity_type' => 'node',
'bundle' => $type,
'label' => $fieldlabel,
);
$instance = field_create_instance($instance);
return $instance;
}
return null;
}
$type =
array(
'type' => 'MY_TYPE',
'name' => t('NAME'),
'base' => 'node_content',
'description' => t("DESCRIPTION"),
'custom' => 1,
'modified' => 1,
'locked' => 0,
);
$type = node_type_set_defaults($type);
node_type_save($type);
createTextFieldByName($type,'firstname','First name');
createTextFieldByName($type,'lastname','Last name');