Skip to content

Commit

Permalink
Feature: Add the ability to make the Add to Cart button a mere extern…
Browse files Browse the repository at this point in the history
…al link

UI is not implemented yet and with this change it forces the plugin to use this feature at the moment.

Resolves #5
  • Loading branch information
Michael Uno committed Sep 30, 2023
1 parent a811a1b commit 845d86e
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions auto-amazon-links-woocommerce-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class App {
public $aMembers = [
__NAMESPACE__ . '\\Converter\\Loader' => null,
__NAMESPACE__ . '\\Buttons\\Loader' => null,
__NAMESPACE__ . '\\CartToLink\\Loader' => null,
__NAMESPACE__ . '\\Events\\Filters\\ProductThumbnails' => null,
__NAMESPACE__ . '\\Events\\Filters\\PriceHTML' => null,
__NAMESPACE__ . '\\Events\\Redirects\\Checkout' => null,
Expand Down
28 changes: 28 additions & 0 deletions includes/c.cart-to-link/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace AutoAmazonLinks\WooCommerceProducts\CartToLink;

use AutoAmazonLinks\WooCommerceProducts\Commons\LoaderAbstract;

/**
* Converts the cart button to a simple external Amazon link
*
* @since 1.1.0
*/
class Loader extends LoaderAbstract {

/**
* @since 1.1.0
* @var string
*/
static public $sDirPath = __DIR__;

/**
* @since 1.1.0
* @var string[] A list of component members.
*/
public $aMembers = [
__NAMESPACE__ . '\\Events\\CustomCartLinks' => null,
];

}
127 changes: 127 additions & 0 deletions includes/c.cart-to-link/events/CustomCartLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace AutoAmazonLinks\WooCommerceProducts\CartToLink\Events;

use AutoAmazonLinks\WooCommerceProducts\Commons\MemberInterface;

/**
* Provides means to customize the Add-to-Cart button links
*
* Some users might want just link to the product page rather than adding the product to the on-site cart.
*
* @since 1.1.0
*/
class CustomCartLinks implements MemberInterface {

/**
* @since 1.1.0
*/
public function run() {

add_filter( 'woocommerce_loop_add_to_cart_link', [ $this, 'replyToEditAddToCartButtonLinkInLoops' ], 100, 3);

// Add to Cart text
add_filter( 'woocommerce_product_single_add_to_cart_text', [ $this, 'replyToEditAddToCartTextSingle' ], 100, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', [ $this, 'replyToEditAddToCartTextInLoops' ], 100, 2 );

// Replace the Add to Cart form with a button
add_filter( 'woocommerce_before_add_to_cart_form', [ $this, 'replyToAddBeforeCartForm' ] );
add_filter( 'woocommerce_after_add_to_cart_form', [ $this, 'replyToAddAfterCartForm' ] );

// Add to Cart url
add_filter( 'woocommerce_product_add_to_cart_url', [ $this, 'replyToEditAddToCartURL' ], 10, 2 );

}

/**
* @since 1.1.0
* @param \WC_Product $oProduct
* @return string The modified `<a>` tag.
*/
public function replyToEditAddToCartButtonLinkInLoops( $sLink, $oProduct, $aArguments ) {
return "<a href='" . esc_url( $this->___getCartHref( $oProduct ) ) . "' rel='nofollow' target='_blank' class='button product_type_simple add_to_cart_button text_replaceable' title='" . esc_attr( $oProduct->get_title() ) . "' >"
. $this->___getCartButtonLabel()
. "</a>";
}

/**
* @since 1.1.0
* @return string
*/
public function replyToEditAddToCartTextSingle( $sButtonText, $oProduct ) {
return $this->___getCartButtonLabel();
}

/**
* @since 1.1.0
* @return string
*/
public function replyToEditAddToCartTextInLoops( $sButtonText, $oProduct ) {
return $this->___getCartButtonLabel();
}

/**
* @since 1.1.0
* @return void
*/
public function replyToAddBeforeCartForm() {
echo "<div class='add-to-cart-form' style='display:none;'>";
}
/**
* @since 1.1.0
* @return void
*/
public function replyToAddAfterCartForm() {
echo "</div>"; // end hidden

if ( empty( $GLOBALS[ 'product' ] ) ) {
return;
}

// Add the "Add to Cart" link
echo "<form class='cart'>"
. "<a href=" . esc_url( $this->___getCartHref( $GLOBALS[ 'product' ] ) ) . " class='button alt' rel='nofollow' target='_blank' title='" . esc_attr( $GLOBALS[ 'product' ]->get_title() ) . "'>"
. $this->___getCartButtonLabel()
. "</a>"
. "</form>";
}

/**
* Modifies the Add to Cart URL
*
* The Store Front WooCommerce default theme adds a sticky form at the top and this filter is needed for it.
*
* @param string $sPermalink
* @param \WC_Product $oProduct
* @return string
* @sinec 1.1.0
*/
public function replyToEditAddToCartURL( $sPermalink, $oProduct ) {
return $this->___getCartHref( $oProduct );
}

/**
* @since 1.1.0
* @return string
*/
private function ___getCartButtonLabel() {
return __( 'Buy Now', 'amazon-auto-links' );
}

/**
* @since 1.1.0
* @param \WC_Product $oProduct
* @return string
*/
private function ___getCartHref( $oProduct ) {
$_oUtil = new \AmazonAutoLinks_PluginUtility();
$_sSKU = $oProduct->get_sku(); // ASIN|locale|currency|language
$_aItem = explode( '|', $_sSKU );
$_sASIN = $_oUtil->getElement( $_aItem, 0 );
$_sLocale = $_oUtil->getElement( $_aItem, 1 );
$_oOption = \AmazonAutoLinks_Option::getInstance();
$_oLocale = new \AmazonAutoLinks_Locale( $_sLocale );
return $_oLocale->getMarketPlaceURL( 'dp/' . $_sASIN . '/?tag=' . $_oOption->getAssociateID( $_sLocale ) );
}

}
2 changes: 2 additions & 0 deletions includes/class-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"AutoAmazonLinks\\WooCommerceProducts\\Buttons\\Loader" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.buttons/Loader.php",
"AutoAmazonLinks\\WooCommerceProducts\\Buttons\\Events\\DynamicProductCreation" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.buttons/events/DynamicProductCreation.php",
"AutoAmazonLinks\\WooCommerceProducts\\Buttons\\Events\\ItemFormatTags" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.buttons/events/ItemFormatTags.php",
"AutoAmazonLinks\\WooCommerceProducts\\CartToLink\\Loader" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.cart-to-link/Loader.php",
"AutoAmazonLinks\\WooCommerceProducts\\CartToLink\\Events\\CustomCartLinks" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.cart-to-link/events/CustomCartLinks.php",
"AutoAmazonLinks\\WooCommerceProducts\\Converter\\Loader" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.converter/Loader.php",
"AutoAmazonLinks\\WooCommerceProducts\\Converter\\Admin\\FormFields\\Main" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.converter/admin/form-fields/Main.php",
"AutoAmazonLinks\\WooCommerceProducts\\Converter\\Admin\\PostMetaBoxes\\Main" => \AutoAmazonLinks\WooCommerceProducts\App::$sDirPath . "/includes/c.converter/admin/post-meta-boxes/Main.php",
Expand Down

0 comments on commit 845d86e

Please sign in to comment.