Remove added restrictions tab in WooCommerce product data - php

I'm using WooCommerce and WooCommerce Catalog Visibility Options plugins.
I want to remove some tabs in product data box, such as General, Inventory, Shipping and Restrictions tabs. I have removed the first three tabs, except for the Restrictions, by using the method below:
add_filter( 'woocommerce_product_data_tabs', 'woo_remove_product_tabs', 10, 1 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['general'] );
unset( $tabs['inventory'] );
unset( $tabs['shipping'] );
unset( $tabs['restrictions'] );
return $tabs;
}
From researching, I know that the Restrictions tab is added by WooCommerce Catalog Visibility Options plugin. I dig round into the source code and I found the way how the Restrictions was added
public function __construct() {
add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'add_tab' ) );
}
public function add_tab() {
?>
<li class="wc_catalog_restrictions_tab wc_catalog_restrictions">
<span><?php _e( 'Restrictions', 'wc_catalog_restrictions' ); ?></span>
</li><?php
}
I also tried this:
add_filter( 'woocommerce_product_write_panel_tabs', 'remove_restrictions_tab' );
function remove_restrictions_tab( $tabs ) {
unset( $tabs['restrictions'] );
return $tabs;
}
But it didn't work as well. I'm kind new to php, can anyone help me to remove the restrictions tab properly?

Related

I can't purchase my custom product type in WordPress

I'm developing a plugin for custom product type. Here's my class that is being registered on plugins_loaded hook:
class WC_Product_Subscription extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'subscription';
$this->purchasable = true;
$this->downloadable = false;
$this->virtual = true;
$this->sold_individually = true;
$this->manage_stock = false;
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
}
public function is_purchasable() {
return true;
}
}
The problem is that I cannot see "Add to Cart" button on the product page which means my product cannot be purchased. I tried adding
public function add_to_cart_url() {
return apply_filters( 'woocommerce_product_add_to_cart_url', get_permalink( $this->get_id() ), $this );
}
public function add_to_cart_text() {
$text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add to cart', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
}
to the class but without success. I'm stuck.
It appears there are some missing steps to make your custom product type work.
Try the steps below:
#1. Make sure that your plugin is active.
#2. Make sure the product is in stock and has a price set. WooCommerce checks both of these conditions before displaying the Add to Cart button.
#3. Check if the custom product type is registered correctly. Use the following code to check:
add_action( 'init', 'check_registered_product_types' );
function check_registered_product_types() {
$product_types = wc_get_product_types();
var_dump( $product_types );
}
#4. Make sure that the WooCommerce product type is supported. Use the following code to check:
add_filter( 'product_type_selector', 'custom_product_type_selector' );
function custom_product_type_selector( $product_types ) {
var_dump( $product_types );
return $product_types;
}
#5. Make sure that the product class is correctly loaded. Use the following code to check:
add_action( 'plugins_loaded', 'check_product_class' );
function check_product_class() {
$product_class = 'WC_Product_Subscription';
var_dump( class_exists( $product_class ) );
}
#6. Ensure you have a product template for your custom product type in your theme's WooCommerce folder (e.g. single-product-subscription.php).
#7. If everything else seems to be working, you might have to override the WooCommerce templates to display the Add to Cart button.
Edit:
You can create a template in your plugin directory by using the following code:
add_filter( 'woocommerce_locate_template', 'wc_subscription_template', 10, 3 );
function wc_subscription_template( $template, $template_name, $template_path ) {
if ( 'single-product-subscription.php' === $template_name ) {
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/single-product-subscription.php';
}
return $template;
}
This will tell WooCommerce to use your custom template in the templates folder within your plugin directory. Make sure you put the code in a file that is included in your plugin, so it will run when the plugin is activated.

Hide WooCommerce Payment Method Function Disables WordPress nav-menus.php

I have made hidden the other payment methods if selected COD method (which is flat_rate:5 in my case). I. am using Show hide payment methods based on selected shipping method in Woocommerce answer where I have made some minor edits.
The code works exactly as it should be however, I can't edit menus in "Appearance -> Menus" screen. It seems no problem in front-end.
function payment_gateways_based_on_chosen_shipping_method( $gateways ) {
// Get chosen shipping methods
$chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );
if ( in_array( 'flat_rate:5', $chosen_shipping_methods ) )
{
unset( $gateways['bacs'] );
unset( $gateways['iyzico'] );
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateways_based_on_chosen_shipping_method' );
I am totally confused where is the bug and why it only appears on "Appearence -> Menus" page.
Also this guy from WordPress support forum had encounter similar issue with different code but in same filter (woocommerce_available_payment_gateways).
Thank you.
That's because WC()->session is null on backoffice. So you can't call method "get".
You need to check if you are in admin area before execute your code:
function payment_gateways_based_on_chosen_shipping_method( $gateways ) {
if(is_admin()){
return $gateways;
}
// Get chosen shipping methods
$chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );
if ( in_array( 'flat_rate:5', $chosen_shipping_methods ) ){
unset( $gateways['bacs'] );
unset( $gateways['iyzico'] );
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateways_based_on_chosen_shipping_method' );

Hide For Specific Pages WooCommerce Description & Additional information & Reviews Tabs Using Wordpress Custom Fields

I'm attempting to make a system whereby I can remove the product tabs but only on certain single product pages, and I need to define which pages are to have their product tabs hidden using wordpress custom fields. The custom field name I want to call: 'hide_product_page_tabs' and the defining values need to be either '1' or '0' for yes or no.
I created a new Wordpress custom field on my chosen woocommerce product pages.
Custom field name: hide_product_tabs
Custom field value: Defined a '1' in the custom field to trigger the code, or anything else such as '0' to turn it off.
I placed in my child theme's functions.php :
/* WooCommerce hide product page tabs - hide_product_tabs */
/**
* Remove existing tabs from single product pages.
* https://gist.github.com/mikejolley/c75083db7f6110cbdbe4808e3af36fe3
*/
function remove_woocommerce_product_tabs( $tabs ) {
unset( $tabs['description'] );
unset( $tabs['reviews'] );
unset( $tabs['additional_information'] );
return $tabs;
}
function hide_product_page_tabs() {
global $post;
$product_id = $post->ID;
$HideProductTabsValue = get_post_meta($product_id,'hide_product_tabs',true);
if (strpos($HideProductTabsValue, '1') !== false) {
return add_filter( 'woocommerce_product_tabs', 'remove_woocommerce_product_tabs', 98 );
}
}
add_action('woocommerce_single_product_summary','hide_product_page_tabs');
Any tips are welcome!
Based on your description, without seeing the code you use for this, you can simply use the following
function hide_product_tabs( $tabs ) {
// Get the global product object
global $product;
// Get product id
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
$HideProductTabsValue = get_post_meta( $product_id, 'hide_product_tabs', true);
// 1 = true
if( $HideProductTabsValue == true ) {
unset( $tabs['description'] ); // (Description tab)
unset( $tabs['reviews'] ); // (Reviews tab)
unset( $tabs['additional_information'] ); // (Additional information tab)
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'hide_product_tabs', 98 );

Combined WooCommerce Checkout/Cart page and adding cross-sell

I've combined the shortcodes for checkout and cart onto the Checkout page in two columns to reduce the number of clicks to finish payment.
However, I'm finding that hooks seem to act strangely with this arrangement.
For example, I'm trying to place the cross-sell section below the cart section. On the default cart page, it appears by default. Combining cart and checkout together makes it disappear.
The logical approach is to do this:
function add_cart_collaterals() {
if (is_checkout()) {
add_action( 'woocommerce_after_cart_contents', 'woocommerce_cross_sell_display' );
}
}
add_action('wp', 'add_cart_collaterals');
That did nothing.
Out of desperation, I then copied and adapted the cross-sell.php template code and put it directly into my child theme's functions.php file like this:
/* Display Cross-Sells below cart */
function show_cross_sell() {
if ( $cross_sells ) :
echo '<div class="cross-sells"><h2>';
_e( 'You may be interested in…', 'woocommerce' );
echo '</h2>';
woocommerce_product_loop_start();
foreach ( $cross_sells as $cross_sell ) :
$post_object = get_post( $cross_sell->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' );
endforeach;
woocommerce_product_loop_end();
echo '</div>';
else : {
echo 'No cross sells to display';
}
endif;
}
add_action( 'woocommerce_after_cart_table', 'show_cross_sell', 10 );
But, only the else condition runs and displays "No cross sells to display". So maybe it lost scope on the $cross_sells object.
Is there any hope of my being able to achieve this?
WooCommerce support came through with an answer that helped me. I wasn't assigning $cross_sells to anything. I needed to assign it like this $cross_sells = array_filter( array_map( 'wc_get_product', WC()->cart->get_cross_sells() ), 'wc_products_array_filter_visible' );
But I'm still curious to know why the hook action wasn't working.
As to why you couldn't achieve it with the hook, it's probably because of the first line of code in the function that you hooked into that action:
function woocommerce_cross_sell_display( $limit = 2, $columns = 2, $orderby = 'rand', $order = 'desc' ) {
if ( is_checkout() ) {
return;
}

How to remove "Quick edit" option in "register_post_meta" list page

I am using register_post_type in list page automatically it is four actions Edit/Quick Edit/trash/View. I want to remove "Quick edit" option from list page. How I can do this.
/*------------------------------------------------------------------------------------
remove quick edit for custom post type videos just to check if less mem consumption
------------------------------------------------------------------------------------*/
add_filter( 'post_row_actions', 'remove_row_actions', 10, 2 );
function remove_row_actions( $actions, $post )
{
global $current_screen;
if( $current_screen->post_type != 'videos' ) return $actions;
unset( $actions['edit'] );
unset( $actions['view'] );
unset( $actions['trash'] );
unset( $actions['inline hide-if-no-js'] );
//$actions['inline hide-if-no-js'] .= __( 'Quick Edit' );
return $actions;
}
It worked for me check with yours

Categories