Woocommerce/Wordpress Update Transient Values for Layered Navigation - wordpress

I am writing an addon for the WooCommerce plugin for Wordpress that allows product imports from an excel spreadsheet. All the products import just fine, but I am having trouble updating the layered navigation in the store sidebar. Each item has a manufacturer assigned to it on import and the manufacturer attribute is being set just fine. However, the layered navigation is not being updated - in fact it doesn't even show up unless I manually update an item.
After doing some digging, it looks like the layered navigation uses an option from wp_options called _transient_wc_ln_count_(hash) to store the count/ids of the items with that attribute. I think this is what I need for my script to work properly, but I can't find any documentation/examples about it. I'm hoping there is just a function that I can call to update the transient options. Can someone point me in the right direction?

So I never figured out the exact answer to this question, but I found something that works. After using wp_insert_post to initially add the post, and then inserting the post meta (including the manufacturer meta) with the resulting post ID, I am calling the wp_update_post function which triggers all the automatic update functions for that item, including the transient count issue. Now my product manufacturer count in the sidebar is correct. Hope this helps someone else!

After the upload you can simply call delete_transient( 'wc_attribute_taxonomies' );
That's where the transients are created: http://oik-plugins.eu/woocommerce-a2z/oik_api/wc_get_attribute_taxonomies/
See also: https://wordpress.stackexchange.com/questions/119729/create-attribute-for-woocommerce-on-plugin-activation

Related

Wordpress ACF fields disappeared

I have been working with wp for a while now but just ran into a bug, that I can not fix with the information found in the internet.
I was editing a acf field. When I was finished writing into one of the fields, I mistakenly pressed enter. The page started reloading and all fields were gone. When going to the frontend, everything is displayed properly.
I tried to reactivate my ACF fields via the screen options what did not work. I inserted some code lines into my functions.php. or into the input.css because I thought maybe the right checkbox for the ACF fields to be displayed was missing. Nothing helped. Is there any other way to fix this issue.
thx in advance
greetings
I just come through this problem : for one of my field group, the fields box just disappeared at some point and I had no way no retrieve it thanks to the Wordpress Admin interface.
I don't know the reason why, but it appears that in my case, ACF registered some "unwanted" user meta in my database. (how is the question)
If you look inside your DB at the usermeta table content you will probably find some ACF entries like : metaboxhidden_acf-field-group or closedpostboxes_acf-field-group and maybe others...
The one causing trouble for me was meta-box-order_acf-field-group : I simply deleted the entry (but you can try editing it to remove the reference of your "lost" box) and the problem was solved.
Maybe too late to help you, but hope it can still help someone !
##EDIT##
Seems I may have a clue about how the box disappeared
Steps to reproduce :
Create a field group and associate it with whatever post type you have
Go create a whatever post (note : the Gutenberg editor is active on this edit page)
You should find your fields group at the bottom of the page
Try to move the group meta box above the Gutenberg editor (without success because it seems not possible)
Save your post
Come back to the edit page of the field group and refresh it
Tadaaa ! the fields group box is not available anymore
Go to your database, the usermeata table, you will find the entry causing the trouble again : meta-box-order_acf-field-group = a:3:{s:4:"side";s:32:"submitdiv,acf-field-group-fields";s:6:"normal";s:57:"acf-field-group-locations,acf-field-group-options,slugdiv";s:8:"advanced";s:0:"";}
I solved the problem by adding a custom script for the ACF with delete class closed on #acf-field-group-fields:
document.addEventListener("DOMContentLoaded", function(event) {
document.querySelector('#acf-field-group-fields').classList.remove('closed');
});

How to apply coupons to CoCart plugin?

I am using CoCart plugin with WooCommerce. I have a cart with products and the plugin counts me the totals and actually everything. I would like to apply coupons, I made a plugin for this, but I had no idea how to get the cart which had been initialized with CoCart. I tried this: WC()->session->get( 'cart_totals' ) and this: WC()->cart, but in both cases the session and cart is null. So I need a solution to make it possible to get the previously created cart.
a mildly different Solution is discussed in this thread, have a look. below is the excerpt copied from there. the question was initially asked by msargenttrue and answered by Freney
the post suggests that you need to do a manual implementation in PHP which is just copypaste , and call the script to apply on the intended product in database. THREAD <-- the PHP implementation part
FOR the problem
So I need a solution to make it possible to get the previously created
cart.
you must try the js implementation of local storage, the stored item remains on the user device until the cart is cleared explicitly.
localStorage.setItem("cart", "CartID-002");
localStorage.clear() #when you want to remove.
Points to remember
The main features of localStorage are:
Shared between all tabs and windows from the same origin.
The data does not expire. It remains after the browser restart and
even OS reboot.

Why would WooCommerce Product Attributes disappear on save

I have a WooCommerce store that has products with attributes that are associated with variations. When I save products, the attributes disappear (and therefore the associated variations do, too). When I re-add the attributes and save the product again, the variations come back as they were. (Note that this means attributes don’t always disappear - only when they’re not manually added before saving. )
No errors are logged.
I'm looking for some insight as to why this would happen.
I had the same issue since I use Ozisti theme for my site, too.
The workaround was to do NOT USE "Save attributes" button on product page. To save attributes changes USE "Update" (product page - on the top/right of the page).
The problem has been solved with Ozisti ThemeREX support team. They sent me new files:
wp-content/plugins/trx_addons/components/api/woocommerce/woocommerce-extended-attributes.php
wp-content/plugins/trx_addons/includes/plugin.wp.php
and now it works fine.
Regards, Jacek
I think I had a similar problem.
WordPress 5.1.1
WooCommerce 3.5.7
Ozitsi 1.0.2 Theme by ThemeRex (here was the problem)
Solution summary: Add attribute, add options, press SAVE ATTRIBUTES (attribute row folds) -> UNFOLD IT immediately - if you don't see options inserted a second ago into the Comboselect field read on.
In my case plugin ThemeREX Add-ons (shipped with Ozisti theme) was the issue. It added extra attribute types, so I could turn variations between dropdown select, image, color and button types.
After few hours of debugging I noticed that when attribute was set to one of the new types, UPDATING PRODUCT did erase all added attributes -> and by extension variations.
After ADDING ATTRIBUTES again (without SAVE ATTRIBUTES button - just adding attributes and UPDATING product) all was back in place.
Notice: when you SAVE ATTRIBUTES the row with options closes, the variations become available. But when I opened the attribute row after all AJAX saving was done it turned out that there were no OPTIONS placed in this Wordpress combo select of attribute I just edited.
So on UPDATING PRODUCT WooCommerce saved empty attribute options into DB.
This might not be perfect solution but I solved this by modifying WooCommerce's meta-boxes-product.js
I commented out line 465 and added another line to close the row:
$( '.product_attributes' ).html( response.data.html );
$( '.product_attributes .woocommerce_attribute.open .handlediv' ).trigger('click');
I didn't trace everything to the roots but makinkg it work for this single project is good enough for me at this point.
You probably use other plugins then me, but maybe they do something similar to my case.
So try to SAVE ATTRIBUTES and then immediately open attribute row again and check if selected options are inserted.. if not.. try my solution.
This Happen to me This Year. I was debugging my store for 3 days.
Solution:
I load a css file on Wordpress back office with these lines:
.save_attributes, .save-variation-changes {
display:none !important;
}
This makes WooCommerce Hide 2 buttons.
Save Attribute and Save Variation. So clients will be forced to use the update button in the product page. I'm still not sure if this is a Woocommerce bug or other plugin but for some reason, if I pressed one of these 2 buttons I would lose all my variations.
I have similiar solution with #Maciej Dejewski using Woocommerce v3.6.5.
On file /plugins/woocommerce/assets/js/admin/meta-boxes-product.js or /plugins/woocommerce/assets/js/admin/meta-boxes-product.min.js (depends on your admin page calling the javascript - you can check it via inspect element)
But instead adding line
$( '.product_attributes .woocommerce_attribute.open .handlediv' ).trigger('click');
Just commented out this
$( '.product_attributes' ).html( response.data.html );
Working fine for me
Here's the screenshot
Hope this would help!
After debugging for a hour, it turns out that my variations only disappear when not each attribute is selected. However, some of my product's attributes have between 10 and 150 entries..so that is the downside when using this method.

How to create Child Plugin for wordpress

Actually I have changed some code in WordPress Store Locator. I want it to remain when plugin will update. So I want to create a child plugin for that. Any ideas on how I could manage it?
This varies plugin to plugin, and it sometimes isn't even possible, other times plugins have documentation to extend them easily (such as WooCommerce and Gravity Forms). Some of them create Action Hooks with do_action() that let you extend the functionality easily. A common example is updating a post after a Gravity Form is submitted with their gform_after_submission hook.
Effectively, it depends on what you want to do, and how the plugin implements the functionality you want to change. If they add text with a Closure or Anonymous Function, it will be harder to modify said text, and you may have to look at something strange like doing a run-time find and replace using Output Buffering, typically on the template_redirect hook.
If you want to remove something a plugin does, you can often unhook it with remove_action. This can be a bit tricky depending on how the plugin is instantiated, sometimes its as simple as:
remove_action( 'some_hook', 'function_to_remove' );
Other times it's more complicated like:
global $plugin_class_var;
remove_action( 'some_hook', array($plugin_class_var, 'function_to_remove') );
Those are the basics of extending (or even 'shrinking'?) a plugin's functionality, and it's not always doable appropriately. Unfortunately the narrow answer to your question is outside of the scope of what we can provide from StackOverflow.
From here, you'll need to figure out exactly what you want to do with the plugin, and dig through the plugin's files to see if there's an appropriate hook or function you can use. If you're still stuck, you'll need to post a new question (don't update this one) with your exact desired result and anything you've tried, and the relevant code that goes along with it. "I want to change a plugin without editing core files" isn't nearly specific enough. "I want to replace an icon with a custom icon in this plugin, here's what I've tried" is specific enough to possibly answer.
Good luck!
I just went through myself and I had so many changes that I couldn't just override the actions.
I created this tool that allows you to create a child plugin like a child theme. You can make updates to the plugin and still update it without losing your changes.
I'm posting this here because it relates and hopefully becomes useful to the next person who runs into this issue.
https://github.com/ThomasDepole/wordpress-child-plugin-tool
As per WordPress standard, it's called plugin's addon.
if the plugin has provided any action to update that functionality then you can use it with your addon (child plugin).
Here I am sending a link for reference.
https://developer.wordpress.org/reference/functions/add_action/

Wordpress - Prevent Post Submission unless valid data

I'm building a custom wordpress theme.
When the user is attempting to update a custom post type ( Product ), I want to perform some validation before letting them save/upload.
Here is what's going on.
I have a series of "Things".
Each "Thing" has an advanced custom field for img and for text.
So my goal here is, if they upload an image but don't fill in the textbox, to not allow them to save/upload the post. This also works in reverse ( if they provide text but no image ).
So right now I have hooks in 'save_post' that perform the validation and set notices at the top of the screen letting them know what they did wrong. However, this really doesn't work because 'save_post' is called AFTER the post is saved. Even if it wasn't, I still don't have any way of stopping the post from being saved anyways. The most my implementation does it let them know in hindsight that they messed up.
So, how do I accomplish this task?
I think you can do this with the content_save_pre filter.
http://codex.wordpress.org/Plugin_API/Filter_Reference/content_save_pre
The answer ended up being to use Advanced Custom Field's provided filters.

Resources