How to edit woocommerce functions trought theme's functions? - wordpress

I'd like to edit public function generate_product_data from woocommerce/includes/class-wc-structured-data.php
Inside this function:
if ( $product->get_sku() ) {
$markup['sku'] = $product->get_sku();
} else {
$markup['sku'] = $product->get_id();
}
I want to change it to:
if ( $product->get_sku() ) {
$markup['sku'] = $product->get_sku();
$markup['mpn'] = 'BG' . $markup['sku'];
} else {
$markup['sku'] = $product->get_id();
$markup['mpn'] = 'BG' . $product->get_id();
}
And i want to add my custom code right before this part:
// Check we have required data.
if ( empty( $markup['aggregateRating'] ) && empty( $markup['offers'] ) && empty( $markup['review'] ) ) {
return;
}
I tried with:
add_filter( 'woocommerce_structured_data_product', 'add_mpn' );
function add_mpn($markup) {
if ( $product->get_sku() ) {
$markup['mpn'] = 'BG' . $markup['sku'];
} else {
$markup['mpn'] = 'BG' . $product->get_id();
}
return $markup;
}
But it doesn't work.
Any help?

You are almost there, you need to change it a bit. The filter gives you two variables to work with, although you need to return only one. This is why I added the priority (50) and the number 2, to accept two variables.
I also recommend to give a more meaningful name to your function. Try this:
add_filter( 'woocommerce_structured_data_product', 'add_mpn', 50, 2 );
function add_mpn($markup, $product) {
if ( $product->get_sku() ) {
$markup['mpn'] = 'BG' . $markup['sku'];
} else {
$markup['mpn'] = 'BG' . $product->get_id();
}
return $markup;
}

Related

Get all tags with specific option in term meta

Some of my tags have some custom term meta and I'm saving that meta with this function:
add_action ( 'edit_term', 'save_termmeta_tag');
// save extra category extra fields callback function
function save_termmeta_tag( $term_id ) {
if ( isset( $_POST['Tag_meta'] ) ) {
$t_id = $term_id;
$tag_meta = get_option( "tag_$t_id");
$tag_keys = array_keys($_POST['Tag_meta']);
foreach ($tag_keys as $key){
if (isset($_POST['Tag_meta'][$key])){
$tag_meta[$key] = $_POST['Tag_meta'][$key];
}
}
//save the option array
update_option( "tag_$t_id", $tag_meta );
}
}
Now, I would like to get all tag with a specific option like 'channel'.
Is this possible, and how?
function get_term_with_option($opt) {
$terms = array();
// load all options
$options = wp_load_alloptions();
foreach($options as $k=>$v) {
// find keys with prefix `tag_`
if (strpos($k, 'tag_') === 0) {
$v = maybe_unserialize($v);
// check if has a meta key $opt
if (is_array($v) && array_key_exists($opt, $v)) {
$term = get_term(intval(str_replace('tag_', '', $k)));
if (!is_wp_error($term)) {
$terms[] = $term;
}
}
}
}
return $terms;
}
print_r(get_term_with_option('channel'));

PHPUnit testing coverage help for WordPress

Below function we need to cover,
function get_assets_list() {
static $assets;
if ( ! isset( $assets ) ) {
$assets = load_asset_file( BUILD_PATH . 'asset-manifest.json' );
if ( empty( $assets ) ) {
$assets = array();
}
}
return $assets;
}
But unfortunately we can't cover below lines
if ( ! isset( $assets ) ) {
$assets = load_asset_file( BUILD_PATH . 'asset-manifest.json' );
if ( empty( $assets ) ) {enter code here
$assets = array();
}
}
Because when we execute the this function always getting $assets this variable set. So how we can set this variable unset or undefined. So that we can cover those line.
I am executed below,
public function test_get_assets_list() {
static $assets = null;
// Replace this with some actual testing code.
$output = get_assets_list();
$this->assertArrayHasKey( 'block.js', $output );
}
but its not covered below lines
if ( ! isset( $assets ) ) {
$assets = load_asset_file( BUILD_PATH . 'asset-manifest.json' );
if ( empty( $assets ) ) {
$assets = array();
}
}

How to disable render HTML code of certain ID of gravity form?

I need to disable render HTML of a certain id in my Gravity Form.
I already found something like this:
add_filter( 'gform_field_content', function ( $field_content, $field, $value ) {
if ( $field->id == 2 ) {
if ( $field->is_entry_detail_edit() ) {
$value = esc_attr( $value );
$name = 'input_' . esc_attr( $field->id );
return "<input type='hidden' name='{$name}' value='{$value}'>";
} elseif ( $field->is_entry_detail() ) {
return '';
}
}
return $field_content;
}, 10, 3 );
That one will hide my id, but HTML is stil rendered.
I suppose I need to use filter => gform_pre_render
Someone has some advice for me, please?
The code you have given prevents the html being output on the entry detail section. Not the main form output.
Try something like this:
add_filter( 'gform_field_content', function ( $field_content, $field, $value ) {
if ( $field->id == 2 ) {
// Show the field in entry_detail and form editor
if ( GFCommon::is_entry_detail_view() || GFCommon::is_form_editor()) {
return $field_content;
}
// Otherwise don't show the field
return '';
}
// Show all other fields
return $field_content;
}, 10, 3 );
If you want to remove the container list-item tag also try this:
add_filter( 'gform_field_container', function ( $field_container, $field, $form, $css_class, $style, $field_content ) {
if ( GFCommon::is_entry_detail_view() || GFCommon::is_form_editor()) {
return $field_container;
}
if ( $field->id == 2 ) {
return '';
}
return $field_container;
}, 10, 3);
Something like this should do it:
function remove_field_by_id($objForm)
{
foreach ($objForm['fields'] as $iIndex => $objField) {
// if its the one you want to remove ...
if ($objField->id == 3) {
// replace that field object with an empty array
array_splice($objForm['fields'], $iIndex, 1, array());
}
}
return $objForm;
}
add_filter('gform_pre_render', 'remove_field_by_id');

how to count only selected options in dropdown list using javascript

How to Implement custom validation in multiselect list in contact form 7 wordpress?
add_filter( 'wpcf7_validate_select', 'custom_select_validation_filter', 20, 2 );
function custom_select_validation_filter( $result, $tag ) {
if ( 'classtype-1' == $tag->name ) {
if($_POST['menu-123'] =='Nursery to III'){
$selected_options = isset( $_POST['classtype-1'] ) ? $_POST['classtype-1'] : '';
if(count($selected_options) < 2){
$result->invalidate( $tag, "Select Atleast 2 options." );
}
}
}
if ( 'classtype-2' == $tag->name ) {
if($_POST['menu-123'] =='IV to VIII'){
$selected_options = isset( $_POST['classtype-2'] ) ? $_POST['classtype-2'] : '';
if(count($selected_options) < 2){
$result->invalidate( $tag, "Select Atleast 2 options." );
}
}
}
return $result;
}
Put the below code in your functions.php file to validate the select input
add_filter( 'wpcf7_validate_select', 'custom_select_validation_filter', 20, 2 );
function custom_select_validation_filter( $result, $tag ) {
if ( 'classtype' == $tag->name ) {
$selected_options = isset( $_POST['classtype'] ) ? $_POST['classtype'] : '';
if(count($selected_option) < 2){
$result->invalidate( $tag, "Select Atleast 2 options." );
}
}
return $result;
}

Wordpress Adding numbers in urls

I have a problem in wordpress urls. I have the url below
http://www.example.com/sample-post/
but in search engine
http://www.example.com/sample-post/
http://www.example.com/sample-post/500012
http://www.example.com/sample-post/323392
http://www.example.com/sample-post/5
Please give me an idea to tackle this problem.
The answer I found on
https://wordpress.stackexchange.com/questions/70992/appending-numbers-to-url-do-not-break-the-link
http://toscho.de/2010/wordpress-plugin-canonical-permalink/
Description: Removes illegal numeric suffixes from the request URI.
Version: 0.3 Author: Thomas Scholz Author URI: http://toscho.de
Created: 04.04.2010
*/
add_action('wp', 't5_canonical_request'); /** * WordPress allows URIs
with any numeric suffix, e.g.: * /canonical-page-or-postname/12345/
* This functions performs a simple check and redirects * to the canonical URI if neccessary. * * #return void */ function
t5_canonical_request() {
global $page, $post;
// post, page, attachment, preview
if ( ! is_singular() or is_preview() )
{
return;
}
$permalink = get_permalink();
// We don't have access to the number of sub pages here.
// So we have to hack.
$max_pages = substr_count(
$post->post_content, '<!--nextpage-->') + 1;
if ( 1 < $page and $page <= $max_pages )
{
/*
* Handle different permalink settings, eg:
* /%year%/%postname%.html or
* /%year%/%postname%/
*/
$rev_perma_struct = strrev(get_option('permalink_structure'));
if ( '/' != $rev_perma_struct[0] )
{
$permalink .= "/$page";
}
else
{
$permalink .= "$page/";
}
}
$host_uri = 'http'
. ( empty ( $_SERVER['HTTPS'] ) ? '' : 's' )
. '://' . $_SERVER['HTTP_HOST'];
$canonical_path = str_replace($host_uri, '', $permalink);
if ( ! empty ( $_GET ) )
{
global $wp;
// Array
$allowed = $wp->public_query_vars;
$out_arr = array();
foreach ( $_GET as $k => $v )
{
if ( in_array($k, $allowed ) )
{
$out_arr[] = $k . ( empty ( $v ) ? '' : "=$v" );
}
}
if ( ! empty ( $out_arr ) )
{
$canonical_path .= '?' . implode('&', $out_arr);
}
}
if ( $canonical_path == $_SERVER['REQUEST_URI'] )
{
return;
}
// Debug current result:
#print '<pre>' . var_export($canonical_path, TRUE) . '</pre>';
// Change it or return 'false' to stop the redirect.
$canonical_path = apply_filters(
't5_canonical_path',
$canonical_path
);
if ( FALSE != $canonical_path )
{
header('Location: ' . $permalink, true, 301);
die("<a href='$permalink'>$permalink</a>");
}
return; }

Resources