Word replacement for wordpress plugins - woocommerce

I want to import a large quantity of products, the plugin is there and everything works, but I want to replace one word (different) in the product names with another word. 32,000 products, one product cannot be made.
I've tried different options, the only one that doesn't get into the wordpress error is the code below, but it doesn't work, I won't actually import the products.
** part between invalid
protected function generate_product_data($product){
$categories = array_values(array_unique(array_map('trim', explode('>', $product['Category']))));
$sku_prefix = get_option(PREFIX . '_sku_prefix', '');
$images = $this->get_images($product);
$sku_raw = $product['SKU'];
$sku = empty($sku_prefix) ? $sku_raw : "{$sku_prefix}{$sku_raw}";
$webshop_price = empty(Utility::rgar($product, 'Webshop_price')) ? null : Utility::clean_price( $product['Webshop_price'] );
$drop_price = empty(Utility::rgar($product, 'drop_Price')) ? $webshop_price : Utility::clean_price( $product['drop_Price'] );
$price = $this->get_price($product);
**
function replace_text($replace){
$replace = str_replace("badword", "goodword",$replace);
$replace = str_replace("badword1", "goodword1",$replace);
return $replace;}
$data = array(
'title' => $product[replace_text('Product_title')],**
'description' => $product['HTML_description'],
'excerpt' => $product['Properties'],
'categories' => $categories,
'images' => $images,
'attributes' => array(
'Color' => $product['Color'],
'Gender' => $product['Gender'],
'Diameter' => Product::measurements_number( $product['Diameter'] ),
'Size' => Product::measurements_number( $product['Size'] ),
'Shipping_time' => $this->get_shipping_time($product),
'Number_of_packages' => $product['Number_of_packages'],
),
'meta' => array(
'_regular_price' => $price,
'_price' => $price,
'B2B_price' => Utility::clean_price( $product['B2B_price'] ),
'RRP_price' => Utility::clean_price( $product['RRP'] ),
'drop_price' => $drop_price,
'_sku' => $sku,
'_manage_stock' => 'yes',
'_stock' => $product['Stock'],
'_stock_status' => $product['Stock'] == '0' ? 'outofstock' : 'instock',
'_weight' => Product::measurements_number( $product['Weight'] ),
PREFIX . '_ean' => $product['EAN'],
PREFIX . '_sku_raw' => $sku_raw,
PREFIX . '_image_urls' => $images,
PREFIX . '_shipping_time' => $this->get_shipping_time($product),
PREFIX . '_parcel_or_pallet' => $product['Parcel_or_pallet'],
PREFIX . '_number_of_packages' => $product['Number_of_packages'],
)
);
return $data;
}

Related

Add woocommerce variation GTIN to structured data

If a webshop has variable products, in some cases the variations have unique GTINs. In our case, it is EAN.
How to add these different GTINS to the structured data? I made a function to insert multiple "offer" in "offers", but the GTIN is not recognized here.
This is the function:
//add structured data to the markup
function set_structured_data( $markup, $product ) {
if ($product->is_type('simple')){
$markup['brand'] = array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) );
$markup['gtin8'] = get_post_meta( $product->get_id(), '_EAN', true );
}
if ($product->is_type('variable')){
$available_variations = $product->get_available_variations();
foreach ($available_variations as $variation) {
$variation_id = $variation['variation_id'];
$variproduct = wc_get_product($variation_id);
$i++;
$stock = $product->get_stock_status();
$offers[] = array(
'type' => 'Offer',
'price' => $variproduct->get_price(),
'priceValidUntil' => $priceuntil,
'priceSpecification' => array(
'price' => $variproduct->get_price(),
'priceCurrency' => get_woocommerce_currency(),
'valueAddedTaxIncluded' => 'http://schema.org/True'
),
'priceCurrency' => get_woocommerce_currency(),
'availability' => 'http://schema.org/'.$stock.'',
'url' => get_the_permalink(),
'seller' => array (
'type' => 'Organization',
'name' => 'HeatPerformance®',
'url' => get_the_permalink(),
));
}
$markup['offers'] = $offers;
$markup['brand'] = array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) );
}
return $markup;
}
add_filter( 'woocommerce_structured_data_product', 'set_structured_data', 99, 2 );
Any ideas?
Ok after a couple of days puzzling i found the path to a solution here:
https://support.google.com/merchants/answer/6386198?hl=en
Result is that i corrected the standardized method from woocommerce and made each variation a unique product.
So first remove the markup in case of a variable product like this:
function set_structured_data( $markup, $product ) {
if ($product->is_type('variable')) {
$markup = array();
}
return $markup;
}
add_filter( 'woocommerce_structured_data_product', 'set_structured_data', 99, 2 );
And then build up the ld+json script again, but then the desired way:
function set_structured_data_variable() {
global $product;
if (isset($product)){
if ($product->is_type('variable') && !empty($product)){
$available_variations = $product->get_available_variations();
$date = date("Y-m-d",strtotime(" + 3months"));
$commenttext = '';
$commentauthor = '';
$commentdate = '';
$comments = get_comments(array( 'post_id' => $product->get_id(), 'number' => '1' ));
foreach($comments as $comment) {
$commenttext = $comment->comment_content;
$commentauthor = $comment->comment_author;
$commentdate = $comment->comment_date;
}
foreach ($available_variations as $variation) {
$variation_id = $variation['variation_id'];
$variproduct = wc_get_product($variation_id);
$i++;
$gtin = 0000000000001;
if (!empty(get_post_meta( $variation_id, '_EAN', true ))){
$gtin = get_post_meta( $variation_id, '_EAN', true );
}
$stock = $product->get_stock_status();
$arrays[$i] = array(
'#context' => 'https://schema.org/',
'#type' => 'Product',
'sku' => $variproduct->get_sku(),
'gtin13' => $gtin,
'image' => get_the_post_thumbnail_url($product->get_id()),
'name' => implode(" / ", $variproduct->get_variation_attributes()),
'description' => wp_strip_all_tags(get_the_excerpt($product->get_id())),
'brand' => array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) ),
'review' => array(
'#type' => 'Review',
'reviewRating' => array (
'#type' => 'Rating',
'ratingValue' => $product->get_review_count(),
'bestRating' => '5',
'worstRating' => '1',
),
'author' => array(
'#type' => 'person',
'name' => $commentauthor,
'reviewBody' => $commenttext,
'datePublished' => $commentdate,
)),
'aggregateRating' => array (
'#type' => 'AggregateRating',
'ratingValue'=> $product->get_average_rating(),
'reviewCount' => $product->get_rating_count(),
),
'inProductGroupWithID' => $product->get_sku(),
'offers' => array(
'#type' => 'Offer',
'price' => $variproduct->get_price(),
'priceValidUntil' => $date,
'priceSpecification' => array(
'price' => $variproduct->get_price(),
'priceCurrency' => get_woocommerce_currency(),
'valueAddedTaxIncluded' => 'http://schema.org/True'
),
'priceCurrency' => get_woocommerce_currency(),
'availability' => 'http://schema.org/'.$stock.'',
'url' => get_the_permalink(),
'seller' => array (
'type' => 'Organization',
'name' => 'HeatPerformance®',
'url' => get_the_permalink(),
)));
}
echo '<script type="application/ld+json" class="buronoort">[';
$i = 0;
foreach ($arrays as $array){
$i++;
echo json_encode($array);
if ($i < array_key_last($arrays)){
echo ',';
}
}
echo ']</script>';
}
}
}
add_action('wp_head','set_structured_data_variable', 19);
Tested in the test tool for structured data and it is working.
A little concern is the product review however, i have to look into that what happens after multiple reviews.
Another concern is the EAN, i have set it standard to "0000000000001" as not all EANS are inputted yet. So this is a debatle solution, but if someone has a better idea, keep me posted.

Understanding genesis_markup() in Genesis Framework

I don't understand how the genesis_markup function turns:
array(
'open' => '<aside class="widget-areainfo-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
into
"<div class="widget-area">"
I do not understand why the id is not used in the output at all and the aside class is also not used....I would have thought that it should be.....
When I do this in the code:
$before_markup_args = array(
'open' => '<aside class="widget-areainfo-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
);
d($before_markup_args);
d(genesis_markup( $before_markup_args ));
First, I would like to confirm the missing space:
```
$before_markup_args = array(
'open' => '<aside class="widget-area info-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
);
```
"Onder" is Dutch for "Below". Thus, "info-onder-content" means "info-below-content". Possibly a title for the widget area.
This is a code example that is used for the genesis_widget_area function, typically found in a functions.php file in a WordPress Genesis child theme.
$widget_area_args = array('before' => genesis_markup($before_markup_args),
);
genesis_widget_area($id, $widget_area_args);
We can understand how this works by looking up the definition of the genesis_widget_area function in the /var/www/html/wp-content/themes/genesis/lib/functions/widgetize.php file.
function genesis_widget_area( $id, $args = array() ) {
if ( ! $id ) {
return false;
}
$defaults = apply_filters(
'genesis_widget_area_defaults',
array(
'before' => genesis_markup(
array(
'open' => '<aside class="widget-area">' . genesis_sidebar_title( $id ),
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
),
)
),
'after' => genesis_markup(
array(
'close' => '</aside>',
'context' => 'widget-area-wrap',
'echo' => false,
)
),
'default' => '',
'show_inactive' => 0,
'before_sidebar_hook' => 'genesis_before_' . $id . '_widget_area',
'after_sidebar_hook' => 'genesis_after_' . $id . '_widget_area',
),
$id,
$args
);
$args = wp_parse_args( $args, $defaults );
if ( ! $args['show_inactive'] && ! is_active_sidebar( $id ) ) {
return false;
}

Wordpress / CMB2 Framework / Display data from repeatable fields in frontend

I have a own option-page in the backend-sidebar with this repeatable fields in it. (not a metabox) I cant figure out, how to display the data from the group field in the frontend.
function ww_register_theme_options() {
$ww_prefix = '_ww_';
$ww_contacts = new_cmb2_box( array(
'id' => $ww_prefix . 'ww_option_plugin',
'title' => esc_html__( 'Ansprechpartner', 'ww-contact' ),
'icon_url' => '/wp-content/plugins/ww-contact/assets/images/icons/ww-icon-white.png',
'object_types' => array( 'options-page' ),
'option_key' => 'ww_options',
) );
$ww_group_field = $ww_contacts->add_field(array(
'id' => $ww_prefix . 'contact_repeat_group',
'type' => 'group',
'description' => __('Ansprechparter Liste', 'ww-contact'),
'repeatable' => true,
'options' => array(
'group_title' => 'Ansprechpartner {#}',
'add_button' => __('neuer Ansprechpartner', 'ww-contact'),
'remove_button' => __('Entfernen', 'ww-contact'),
'sortable' => true,
),
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'forname',
'type' => 'text',
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'surname',
'type' => 'text',
));
}
Remove the prefix from your add_group_field ids they aren't needed. Then loop through like so...
$entries = get_post_meta( $parent, 'ww_contact_repeat_group', true );
if($entries){
foreach ( (array) $entries as $key => $entry ) {
if ( isset( $entry['forname'] ) ) {
$forname = $entry['forname'];
}
}
}

Wordpress Divi Custom Module Visual Editor Error

I am developing a custom Divi module and I have a problem with the fb_support my module loads and works perfectly except in the visual editor. When I put the variable $this->fb_support = true my module disappear in visual editor and get this JavaScript error.
bundle.js?ver=3.0.106:formatted:1422 Uncaught TypeError: Cannot read property 'lockedParent' of undefined
at A (bundle.js?ver=3.0.106:formatted:1422)
at Object.ID_2 (bundle.js?ver=3.0.106:formatted:3571)
at e._invokeCallback (bundle.js?ver=3.0.106:formatted:35300)
at e.dispatch (bundle.js?ver=3.0.106:formatted:35288)
at Object.moduleActive (bundle.js?ver=3.0.106:formatted:7203)
at t.value (bundle.js?ver=3.0.106:formatted:8759)
at Object.l (bundle.js?ver=3.0.106:formatted:24713)
at Object.invokeGuardedCallback (bundle.js?ver=3.0.106:formatted:28497)
at Object.invokeGuardedCallbackAndCatchFirstError (bundle.js?ver=3.0.106:formatted:28500)
at h (bundle.js?ver=3.0.106:formatted:24781)
I think this error is because i don´t have registered my plugin in JavaScript part. Exist any method to do this? Is the correct way to do this?
Here is the code of my plugin:
class Price_Manager extends ET_Builder_Module
{
function init ()
{
$this->name = 'Price Manager;
$this->slug = 'et_pb_prices';
$this->fb_support = true;
$this->whitelisted_fields = array(
'widget_title',
'widget_type',
'admin_label',
'module_id',
'module_class',
);
$this->main_css_element = '%%order_class%%';
$this->advanced_options = array(
'custom_margin_padding' => array(
'css' => array(
'important' => 'all',
),
),
'filters' => array(),
);
}
function get_fields ()
{
$fields = array(
'widget_title' => array(
'label' => 'Title',
'type' => 'text',
'option_category' => 'basic_option',
'description' => 'This is your widget title',
),
'widget_type' => array(
'label' => 'Type of Widget',
'type' => 'select',
'option_category' => 'basic_option',
'options' => array(
'company' => 'Company',
'residential' => 'Residential'
),
'description' => 'Here you can choose the type of widget.',
),
'admin_label' => array(
'label' => 'Admin Label',
'type' => 'text',
'description' => 'This will change the label of the module in the builder for easy identification.',
),
'module_id' => array(
'label' => 'CSS ID',
'type' => 'text',
'option_category' => 'configuration',
'tab_slug' => 'custom_css',
'option_class' => 'et_pb_custom_css_regular',
),
'module_class' => array(
'label' => 'CSS Class',
'type' => 'text',
'option_category' => 'configuration',
'tab_slug' => 'custom_css',
'option_class' => 'et_pb_custom_css_regular',
),
);
return $fields;
}
function shortcode_callback ($atts, $content = null, $function_name)
{
$module_id = $this->shortcode_atts['module_id'];
$module_class = $this->shortcode_atts['module_class'];
$widget_title = $this->shortcode_atts['widget_title'];
$widget_type = $this->shortcode_atts['widget_type'];
$module_class = ET_Builder_Element::add_module_order_class($module_class, $function_name);
$module_id = '' !== $module_id ? sprintf('id="%s"', esc_attr($module_id)) : '';
$module_class = '' !== $module_class ? sprintf('%s', esc_attr($module_class)) : '';
$content = 'My title: ' . $widget_title; // TODO: Fetch the item from DB and prepare HTML for output.
$content .= '<br />';
$content .= 'My type of widget: ' . $widget_type;
$output = sprintf(
'<div class="et_pb_prices_module_wrapper et_pb_module">
<div %1$s class="et_pb_prices%2$s et_pb_module">%3$s</div>
</div>',
$module_id,
$module_class,
$content
);
return $output;
}
}
new Price_Manager();
Thanks.

Setting a default option using CMB2 select field type

I am using CMB2's select to pull in a list of posts that a user can choose from in a custom meta box.
I have added a "blank" option to the options array, but I can't figure out how to make that the default option (ie. <option selected="selected" value="">I'm blank</option>).
I need to do this so I can use an if statement that says if the field is blank, don't show the output box on the site. Right now, even if the user hasn't specifically chosen an option, an option with a value is passed through.
Here's the meta box code:
$link_post_types = array('charter', 'page');
$meta_boxes['ms_metabox'] = array(
'id' => 'ms_metabox',
'title' => __( 'Page Links', 'cmb2' ),
'object_types' => array( 'page' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(
array(
'name' => __( 'Page Link', 'cmb2' ),
'desc' => __( 'Choose the page this will link to', 'cmb2' ),
'id' => $prefix . 'page_link',
'type' => 'select',
'options' => ms_get_posttype_options($link_post_types),
),
),
);
function ms_get_posttype_options($argument) {
$get_post_args = array(
'post_type' => $argument,
'posts_per_page' => -1,
'orderby' => 'type',
'order' => ASC
);
$options = array();
foreach ( get_posts( $get_post_args ) as $post ) {
$post_type = get_post_type( $post->ID);
$title = get_the_title( $post->ID );
$permalink = get_permalink( $post->ID);
$options[] = array(
'name' => $title . ' : ' . $post_type,
'value' => $permalink,
);
}
$empty_option[] = array(
'name' => 'Please select an option',
'value' => '',
);
$options = array_merge($empty_option, $options);
return $options;
}
There is a default argument but when I tried to apply it as in the example, it didn't work.
Thanks for any help!
I halfway figured it out. The posts I was having problems with were old ones where I had already been messing with the values before I added the blank option - when I created new posts, the default option was the blank one (since it was the first array in the merge).
If anyone has a more foolproof solution I'd like to hear it though!
You can add the following to the meta box fields array:
show_option_none' => true

Resources