permalinks – WooCommerce return 404 in the product page

I am making an Ecommerce website with the plugin: OCEANWP.

I had an issue with links of products, because they aren’t the same so i changed it in Setting > Permalinks, but after when I go to the page and I click on a product I have 404: Page Not Found.
I already tried to fix it with htaccess file but it doesn’t work and I don’t know what to do..
I don’t want to lose all the work i’ve done!

woocommerce – Printing product variation return array to string conversion error

I’m trying to echo product variation data using Woocommerce plugin.

I have the following code that suppose to parse array using foreach loop:

$variableProduct = wc_get_product(get_the_ID());

$allVariations = $variableProduct->get_available_variations();
foreach ($allVariations as $key => $value) 
{ 
    echo $value;
}

But instead I’m receiving

Notice: Array to string conversion

How to print product variations correctly?

Fetch Product information in WooCommerce

I am using below function to display product information in Shop Page.

function woocommerce_template_loop_product_title() {
        echo '<h5 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . ' </h5>'; 
        echo '<p>' . get_the_excerpt() . '</p>';
    }

I can fetch product shot description using get_the_excerpt(). But I would like to fetch all product information.

How can I fetch all product information ?

woocommerce – Trying to run a add_action within a function to run a woocomerce function on a ajax request

I am able to get a ajax request to work properly on a woocommerce checkout form page, now I want to update the price of the checkout in that ajax request. My add_action is in the function that is fired by the ajax request.

This is all done in a custom plugin as well

Here is my code this far, it doesn’t seem like the function request_gift_card ever fires


function gift_card_redeem(){
  
  if(!empty($_SERVER('HTTP_X_REQUESTED_WITH')) && strtolower($_SERVER('HTTP_X_REQUESTED_WITH')) == 'xmlhttprequest') {
    error_log("test !empty");
    
add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
function custom_cart_total() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;

    WC()->cart->total *= 0.25;
    //var_dump( WC()->cart->total);
}

    $result('type') = "success";
    $result = json_encode($result);
    echo $result;
  }
  else {
    error_log("test else");
      header("Location: ".$_SERVER("HTTP_REFERER"));
  }

  die();
}

I can get this to work if I place it outside the gift_card_redeem function but I need it to run after the ajax request

add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
function custom_cart_total() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;

    WC()->cart->total *= 0.25;
    //var_dump( WC()->cart->total);
}

Trying to run a Ajax request from a checkout form in woocommerce via a custom plugin

I have a custom plugin with the following code

add_action( 'init', 'my_script_enqueuer' );

function my_script_enqueuer() {
   wp_register_script( "gift_card_redeem", WP_PLUGIN_URL.'/plugin-folder/gift_card_redeem.js', array('jquery') );
   wp_localize_script( 'gift_card_redeem', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        

   wp_enqueue_script( 'jquery' );
   wp_enqueue_script( 'gift_card_redeem' );

}

add_action("wp_ajax_gift_card_redeem", "gift_card_redeem");

function gift_card_redeem(){

  if(!empty($_SERVER('HTTP_X_REQUESTED_WITH')) && strtolower($_SERVER('HTTP_X_REQUESTED_WITH')) == 'xmlhttprequest') {
    error_log("test !empty");
    $result('type') = "success";
    $result = json_encode($result);
    echo $result;
  }
  else {
    error_log("test else");
      header("Location: ".$_SERVER("HTTP_REFERER"));
  }

  die();
}

and a js file in the plugin called gift_card_redeem.js

jQuery(document).ready(function () {
  jQuery(".redeem_gift_card").click(function (e) {
    e.preventDefault();

    jQuery.ajax({
      type: "post",
      dataType: "json",
      url: myAjax.ajaxurl,
      data: { action: "redeem_gift_card" },
      success: function (response) {
        if (response.type == "success") {
          jQuery("#test").html(response);
        } else {
          alert("something broke");
        }
      },
    });
  });
});

and my php and html on the form-checkout page is as follows

<?php
    $link = admin_url('admin-ajax.php?action=gift_card_redeem');
        echo '<a class="redeem_gift_card" href="' . $link . '" >test</a>';
    ?> 

    <h1 id="test">Test</h1>

I am getting a 400 bad request error for admin-ajax.php and I am not quite sure why. Any help would be greatly appreciated.

Thanks

WooCommerce limited role – WordPress Development Stack Exchange

I’m trying to make a custom Woocommerce role that only has access to the WooCommerce reports. The role needs to be able to see the sales reports, but not edit products or anything else on the website.

I tried creating a custom role, but when logging in with that role, I get this error: Sorry, you are not allowed to access this page.

Here is what I tried:

add_role('woo_limited', 'Woo Limited', array(
  'read' => true,
  'view_woocommerce_reports' => true,
));

Any and all assistance is greatly appreciated!

ajax – Woocommerce custom webhook -> action woocommerce_before_calculate_totals

I want to create a custom webhook in woocommerce. I have done something like this before, but can’t remember the syntax and am having trouble finding it in the docs.

I have a custom plugin with the following code

function add_new_webhook_topics( $topics ) {
    // New topic array to add to the list, must match hooks being created.
    $new_topics = array( 
        'order.gift_card' => __( 'Order Gift Card', 'woocommerce' ),
        );
    return array_merge( $topics, $new_topics );
 }
 add_filter( 'woocommerce_webhook_topics', 'add_new_webhook_topics' );

I want to replace the Order Gift Card Section with a redeem gift card, now I assume the ‘order.gift_card’ is my topic which is supposed to fire on submission of payment. What I am actually wanting to do is fire a ajax function before submitting the payment which updates the checkout price with woocommerce_before_calculate_totals function.

Now I am debating if I am supposed to create a custom webhook which fires on a new form on the checkout page, which updates the total price before paying based on my returning payload from the webhook. Or I just do a Ajax request without tying into woocomerce webhooks since it is not an event based on woocommerce which fires the request I am trying to make.

Feel free to ask questions for clarification.

This is a previous post for context, with what landed me here
Is it possible to update the total price on the checkout page in woocommerce from a custom field

woocommerce deposit Form with AJAX and shortcode won’t update but add to previous output

I am trying to create a product page where selecting a field defines a product and this triggers an AJAX call to display the woocommerce deposit form and add to cart submit button that correspond to the relevant product id.

The form that contains the deposit selection fields and submit button is displayed by using a shortcode which is called on page load with the default product.

Then, zhen clicking on the field that corresponds to the product you want to change to, I use an AJAX request to execute that shortcode again. I then try to replace the old form by the new form with innerhtml in Javascript, however for some reason the new form is always added after the old one. I can’t seem to replace that innerhtml content. Here is the code:

This is contained on the page:

//div to be filled with product deposit and add to cart form
<div id="adddepositsection">(add_product id="17364")</div>

//AJAX call on field click to execute the shortcode and replace the innerhtml
jQuery('#product2').on('click', function(){
document.getElementById("product1").className = "unselectedbox";
document.getElementById("product2").className = "selectedbox";
document.getElementById("product3").className = "unselectedbox";

jQuery.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
        data: {
            action: 'addfunctionshortcode',
            // add your parameters here
            productid: "17372"
        },
        success: function (output) {
document.getElementById("adddepositsection").innerHTML = output; 
}
        });


});

Shortcode function to display the deposit and add to cart button as well as function to execute that shortcode via AJAX in functions.php:

 //Display deposit choice and add to cart shortcode
    function add_product_display( $atts ) {
      $atts = shortcode_atts( array(
            'id' => null,
        ), $atts, 'bartag' );
    
        
            if( intval( $atts('id') ) > 0 && function_exists( 'wc_get_product' ) ){
             $product = wc_get_product( $atts('id') );
        ?>
    
        <form class="cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
            
            <div class="wc-deposits-wrapper <?php echo WC_Deposits_Product_Manager::deposits_forced( $product->get_id() ) ? 'wc-deposits-forced' : 'wc-deposits-optional'; ?>">
        <?php if ( ! WC_Deposits_Product_Manager::deposits_forced( $product->get_id() ) ) : ?>
            <ul class="wc-deposits-option">
                <li>
                    <input type="radio" name="wc_deposit_option" value="yes" id="wc-option-pay-deposit" <?php checked( $default_selected_type, 'deposit' ); ?> />
                    <label for="wc-option-pay-deposit">
                        <?php esc_html_e( 'Pay Deposit', 'woocommerce-deposits' ); ?>
                    </label>
                </li>
                <li>
                    <input type="radio" name="wc_deposit_option" value="no" id="wc-option-pay-full" <?php checked( $default_selected_type, 'full' ); ?> />
                    <label for="wc-option-pay-full">
                        <?php esc_html_e( 'Pay in Full', 'woocommerce-deposits' ); ?>
                    </label>
                </li>
            </ul>
        <?php endif; ?>
    </div>
            <div class="addcartcontainer">
                
    <?php if ( $product->is_in_stock() ) {  
             if (pll_current_language() == "en"   ) { ?>
                    <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="addtocartsubmit">BUY NOW</button> <?php
                     }
                    elseif (pll_current_language() == "fr"){ ?>
                        <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="addtocartsubmit">ACHETER MAINTENANT</button> <?php
                    }
                    elseif (pll_current_language() == "it"){ ?>
                        <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="addtocartsubmit">ACQUISTA ORA</button> <?php
                    }
             }
                else { 
     if (pll_current_language() == "en"   ) { ?>
                    <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="addtocartsubmit">SOLD OUT</button> <?php
                     }
                    elseif (pll_current_language() == "fr"){ ?>
                        <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="addtocartsubmit">EPUISE</button> <?php
                    }
                    elseif (pll_current_language() == "it"){ ?>
                        <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="addtocartsubmit">SOLD OUT</button> <?php
                    }
                 } ?>
            </div>
    
        </form>
    
    
    <?php
    
    }
    }
    add_shortcode( 'add_product', 'add_product_display' );

//Function called by AJAX to execute shortcode
// register the ajax action for authenticated users
add_action('wp_ajax_addfunctionshortcode', 'addfunctionshortcode');

// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_addfunctionshortcode', 'addfunctionshortcode');

// handle the ajax request
function addfunctionshortcode() {
    $atts('id') = $_REQUEST('productid');
            echo  add_product_display( $atts );
die();
}